Sync and delete removed documents from CouchDB and Couchbase-lite
Asked Answered
T

2

9

I'm currently using couchbase-lite inside my iOS and android application to sync down files from a database running CouchDB.

Every so often I remove files that are not longer needed, and I would like the same files to be removed from the mobile app as well, but any pull replication only pulls updates or new files, and doesnt trigger a delete on the mobile app.

Is there any way to delete documents from the mobile app that are no longer on the server DB without doing a full purge on the mobile application, and then resyncing the whole database?

Tonita answered 17/6, 2018 at 18:20 Comment(4)
Are you removing them on the server side or the mobile side? Replication is designed to pull deletes as well but I can't remember if CouchDB follows the correct procedure. If you delete them on the mobile side, though, the deletes will sync to the server.Shake
@Shake So its a one way replication. The users syncs the DB to the phone, and doesnt edit anything. Then when I update the info(add info, and remove info) I want that deleted info removed from the users phone as well, as its time dated, so its useless after a certain amount of time. But if I delete data, when the app pulls, it keeps the old info after the pull replication.Tonita
Do you happen to know if CouchDB keeps the revision history intact when you delete it? This would show up as a new revision with the _deleted flag. If you are just simply removing the value from the server then it will not be replicated. Only 'new revisions' will be replicated, which is the reason for this so-called 'tombstone revision.'Shake
@Shake I am unsure, I will check and find out. From my understanding, I only overwrite docs, leading to a increased rev number. What I really want is for the mobile app to just stay perfectly synced with the server.Tonita
B
0

From the database class there is a purgeDocument() method. This removes the target document from the local database - server copies of target documents are left unchanged. If the document is later updated then the document will return to the local client on next replication.

http://docs.couchbase.com/mobile/2.1/couchbase-lite-swift/Classes/Database.html

Broadleaf answered 20/11, 2018 at 22:30 Comment(0)
H
0

You can delete documents from database based on id as following:

try {
        for (Result result : docList) {
            String id = result.getString(0);
            Document doc = database.getDocument(id);
            database.delete(doc);
        }
    } catch (CouchbaseLiteException e) {
        e.printStackTrace();
    } 

To get the document id you have to query something like this:

Query query = QueryBuilder
    .select(SelectResult.expression(Meta.id), SelectResult.all())
    .from(DataSource.database(database))

ResultSet rs = query.execute();
    for (Result result : rs) {
        Dictionary data = result.getDictionary("db_name");
        Log.e("CouchbaseLite ", "document: " + data);
        Log.e("CouchbaseLite ", "id: " + result.getString(0));
    }
Henning answered 3/3, 2020 at 12:11 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.