JoCuTo
JoCuTo

Reputation: 2483

Can not delete documents in local database CouchBase lite

I am using CouchBase Lite 1.3 with Android over CouchDB 1.6 .

If I delete a document on CouchDB the document is not deleted on my device local replication DB.

This is my code:

  private Database master;
    private Manager manager;
    public Replication pullmaster;

    manager = new Manager(new AndroidContext(c), Manager.DEFAULT_OPTIONS);
    master = manager.getDatabase("master");
    pullmaster = master.createPullReplication(url);

     master.addChangeListener(new Database.ChangeListener() {
                    @Override
                    public void changed(Database.ChangeEvent event) {
                        if (event.isExternal()) {
                            for (DocumentChange dc : event.getChanges()) {

                                if (dc.isDeletion()) {
                                    Document doc = event.getSource().getDocument(dc.getDocumentId());
                                    try {
                                        doc.purge();
                                    } catch (CouchbaseLiteException e) {
                                        e.printStackTrace();
                                    }
                                }
                            }
                        }
                    }
                });

  startReplicators();

And here I start the replication

public void startReplicators() {
        if (pullmaster != null) {
            pullmaster.start();
        }
        if (pullwork != null) {
            pullwork.start();
        }
        if (pushwork != null) {
            pushwork.start();
        }
    }

It does not work, I mean It does not delete the document I am deleting in CouchDb, no errors are getting.

I have put a break point on this line

master.addChangeListener(new Database.ChangeListener() {....}

If I create a document in CouchDB addChangeListener() is called but if I deleted the document the method is not called.

Any clue about what I am doing wrong?

Upvotes: 1

Views: 978

Answers (1)

Uniruddh
Uniruddh

Reputation: 4436

In Couchbase Lite version 2.7.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));
    }

Upvotes: 1

Related Questions