Android端CouchBase Lite 1.4 setExpirationDate()失效问题排查
Hey there, let's break down why your documents aren't auto-deleting after 15 minutes—this is a common gotcha with Couchbase Lite's expiration feature, so here are the most likely issues and fixes:
1. You Forgot to Save the Document After Setting Expiration
This is the #1 mistake I see! Setting the expiration date with setExpirationDate() doesn't persist the change unless you explicitly save the document. Make sure your code looks like this:
// Calculate 15 minutes from now Date fifteenMinutesLater = new Date(System.currentTimeMillis() + 15 * 60 * 1000); // Get your document (ensure it's a mutable instance) Document doc = database.getDocument("your-doc-id"); // Set expiration doc.setExpirationDate(fifteenMinutesLater); // Critical: Save the change! try { doc.save(); Log.d("CouchbaseDebug", "Expiration set and document saved"); } catch (CouchbaseLiteException e) { Log.e("CouchbaseDebug", "Failed to save document: " + e.getMessage()); }
Even if you don't see errors, silent failures can happen—adding the try-catch will help you spot if the save is failing for some reason.
2. The Background Cleaner Isn't Running (or Being Blocked)
Couchbase Lite 1.4 uses a background task to automatically purge expired documents, but this task doesn't run constantly. It has a default interval, and on Android, system restrictions like Doze Mode can prevent it from triggering on time.
To test if this is the issue, manually trigger the cleanup:
Database db = manager.getDatabase("your-db-name"); try { db.purgeExpiredDocuments(); Log.d("CouchbaseDebug", "Manually purged expired documents"); } catch (CouchbaseLiteException e) { Log.e("CouchbaseDebug", "Purge failed: " + e.getMessage()); }
If the documents disappear after running this, the auto-cleanup is being blocked. You can work around this by scheduling periodic calls to purgeExpiredDocuments() in your app's background service (just don't overdo it—keep intervals reasonable to avoid draining battery).
3. Your Expiration Date Calculation Is Wrong
Double-check that you're calculating the expiration correctly. It's easy to mix up seconds and milliseconds, or accidentally set a date in the past. Verify the date after setting it:
Date setExpiry = doc.getExpirationDate(); if (setExpiry != null) { Log.d("CouchbaseDebug", "Document expires at: " + setExpiry.toString()); } else { Log.w("CouchbaseDebug", "Expiration date was not set!"); }
If setExpiry is null, the expiration wasn't applied—make sure you're working with a mutable Document instance (not a read-only one from a query result, for example).
4. Your Query Isn't Fetching Fresh Results
If your getWorkdocbytypecompany() method is still showing documents that should be deleted, check if it's using cached results. Expired documents are purged completely from the database, so they shouldn't appear in fresh query results. Ensure you're running a new query every time you fetch the list:
// Example fresh query for company-type documents Query query = QueryBuilder.select(SelectResult.all()) .from(DataSource.database(db)) .where(Expression.property("type").equalTo(Expression.string("company"))); ResultSet results = query.run(); // Iterate through the latest results for (Result result : results) { Document doc = result.getDocument(); // Process the document }
5. Database Instance Mismatch
It sounds obvious, but double-check that you're setting expiration on documents in the same database you're querying with getWorkdocbytypecompany(). A tiny typo in the database name could mean you're modifying one DB but querying another entirely.
内容的提问来源于stack exchange,提问作者JoCuTo

