Rudiksz / couchbase_lite_dart

Dart implementation of the Couchbase Lite, an embedded, NoSQL JSON Document Style database.
BSD 3-Clause "New" or "Revised" License
24 stars 3 forks source link

Can not get Live Query to work! #5

Closed richard457 closed 3 years ago

richard457 commented 3 years ago

First, thank you for your kind support and I really appreciate this work, I tested all function of the plugin and I can not get a live query to work, it crashes the app completely. please help me, I thank you in advance.

Directory appDocDir = await getApplicationDocumentsDirectory();
    String appDocPath = appDocDir.path;
    db = Database("fff",directory:appDocPath);

     final doc = Document('wer', data: {
      'id': '12',
      'name': 'richard',
      'channels': ['1'],
      'table': 'users'
    });
    db.saveDocument(doc);

    // var doc1 = db.getMutableDocument('wer');
    // print(doc1);
    final q = Query(db, 'SELECT * WHERE table=\$VALUE');

    q.setParameters = {'VALUE': 'users'};

    q.addChangeListener((List results) {
      print('New query results: ');
      print(results);
    });

    q.execute();
Rudiksz commented 3 years ago

Parameters don't seem to work with with live queries, probably by design. Just use standard query string.

final q = Query(db, 'SELECT * WHERE table="users"');

Also when you add a change listener the query is run automatically, so there's no need to call q.execute()

Edit: I'll do some test and see if I can add a warning when parameters are used in live queries.

Rudiksz commented 3 years ago

I checked on windows and paramaters do work with live queries. It might be an Android specific issue.

richard457 commented 3 years ago

I got the query to at least log propper like this Compiling N1QL query: SELECT * WHERE table="users" but still can not get it to work.

Rudiksz commented 3 years ago

It's actually Flutter. Add WidgetsFlutterBinding.ensureInitialized(); as the first line of your main() function.

Rudiksz commented 3 years ago

Just a heads up, I renamed the query.setParameters method to query.paramaters (which I released as v0.3.1), because it's a setter not a method.

Parameters do work and I could get your code running on Android, the thing with parameters and live queries is that setting the parameters on query once it's being listened to does nothing to the listeners. You need to remove the listener and add it again, I believe to get the new query.

richard457 commented 3 years ago

Changed the query to

final q = Query(db, 'SELECT * WHERE table=\$VALUE');

    q.parameters = {'VALUE': 'users'};

and added this WidgetsFlutterBinding.ensureInitialized(); as first line in main file Adding change lister crash the app

  q.addChangeListener((List results) {
      print('New query results: ');
      print(results);
    });

I think it might be related to android version we are using.

compileSdkVersion 29
minSdkVersion 22
targetSdkVersion 29
Rudiksz commented 3 years ago

I have the same sdk version, I don't think Android is the issue. Your original code works fine with the Flutter bindings initialized.

richard457 commented 3 years ago

I really tried the whole day, I can not get the live query to work here is my project I test on COUCHBASE_LITE_DART EXAMPLE APP by any chance if you can take a look at it, would appreciate, I want to use this plugin for our project that we want to run on mobiles and desktop as future.

richard457 commented 3 years ago

Also, I am committed to testing the plugin and share the test code on the above codebase so other people can reference it.

Rudiksz commented 3 years ago

You must call Cbl.init(); to set up the ports between Dart and C, so callbacks can work.

richard457 commented 3 years ago

Thank you so much @Rudiksz I got it working and I updated the sample application with Crud operation, will keep updating the example app for anyone to cross reference.