Please help me understand and implement live queries (iOS)

58 views
Skip to first unread message

Sandra

unread,
Oct 30, 2014, 11:25:44 AM10/30/14
to mobile-c...@googlegroups.com
Hello,
   I appreciate your help to understand and implement liveQueries in iOS. I've read couch base documentation and made the below piece of code, but I'm getting "Assertion failed: Cannot reindex view 'tableChange' which has no map block set'"
   Would you tell me what I'm doing wrong?
   I have some more questions:
1) Is there something like CBLUITableSource  but for collections? 
2) I also tried to set a start key and end key in my query with no success. I want to filter by tableId which is numeric, so let's say I'd like to read documents between tableId = 5 and 10. How can this be done?
3) I need to use this live query in only one of several UIViewControllers. Shall I turn off the live query each time this view is closed and re-opened when the view is loaded again? 

Any info is very well appreciated, thanks.
Sandra


- (void) initializeQuery {

    // Set up my live query during view initialization:

    CBLQuery* query = [[database viewNamed: @"tableChange"] createQuery];

    query.limit = 100;

    liveQuery = query.asLiveQuery;

    [liveQuery addObserver: self forKeyPath: @"rows"

                        options: 0 context: NULL];

    [liveQuery start];

}

- (void)observeValueForKeyPath:(NSString *)keyPath

                      ofObject:(id)object

                        change:(NSDictionary *)change

                       context:(void *)context

{

    if (object == liveQuery) {

        NSLog(@"%@",@"Rows have changed");

       

        // update the UI

        

    }

}



- (void)tablesCBView {

    NSString *table = @"table";

    [[database viewNamed: @"tableChange"] setMapBlock: MAPBLOCK({

        id DocType = [doc objectForKey: @"docType"];

        if ([DocType isEqualToString:table])

            emit (doc,nil);

    }) version: @"1"];

}



Jens Alfke

unread,
Oct 30, 2014, 1:48:27 PM10/30/14
to mobile-c...@googlegroups.com
On Oct 30, 2014, at 8:25 AM, Sandra <txm...@gmail.com> wrote:

I'm getting "Assertion failed: Cannot reindex view 'tableChange' which has no map block set'"
   Would you tell me what I'm doing wrong?

Most likely it's exactly what it says in the error message: you haven't set the map block of the view yet. Your code shows -tablesCBView as a separate method. Are you sure you called that method before creating the query? Set a breakpoint to make sure.

   I have some more questions:
1) Is there something like CBLUITableSource  but for collections? 

Yes, there's a CBLUICollectionSource class. Since it's not as commonly used we didn't build it into the library itself, but it's in the Extras folder and you can add the source files to your project.

2) I also tried to set a start key and end key in my query with no success. I want to filter by tableId which is numeric, so let's say I'd like to read documents between tableId = 5 and 10. How can this be done?

That should work. Just make sure the startKey and endKey you set are NSNumbers not NSStrings.

3) I need to use this live query in only one of several UIViewControllers. Shall I turn off the live query each time this view is closed and re-opened when the view is loaded again? 

Yes, that would be good for performance. Use the -stop and -start methods. (Or release the query object entirely, and create a new one the next time the view is shown; this is slightly slower but will save memory since the query rows won't stay around.)

—Jens

Sandra

unread,
Oct 30, 2014, 4:11:54 PM10/30/14
to mobile-c...@googlegroups.com
Thank you very much for your quick response, you are right, I didn't call the view method before making it liveQuery , now it's working  :)
Now I'll try the CBLUICollectionSource.
Thanks again  :)
BR, Sandra
Reply all
Reply to author
Forward
0 new messages