Live Query notifications and Background Database actions

Hi,

I’m using Couchbase Lite 1.0.4 with the iOS 8.3 SDK and I’m using backgroundTellDatabaseNamed to execute my own synchronisation with a custom api. When I start a Live Query and observe its rows kvo path, the observer only gets notified at the beginning. When I update my data with a background database, the live query doesn’t fire a notification, although the data itself changed.

If you use an UITableView for example, manually reloading the view or scrolling the view, will result in updated items, but I can’t update it automatically after synchronisation without registering observers on each seperate document. Which results in a massive overhead in nsnotifications.

Is this the normal behaviour? Running my synchronisation code on the main database has no problems with dispatching the live query notifications, but it makes the app + GUI slow because it’s taking up the main thread.

hey, @jamiltz can you help here please?

Hello @thiver,

Can you give more details about what is running in the backgroundTellDatabaseNamed method? Is it networking code making requests to your own back-end to fetch json data and save it in Couchbase Lite?

I would expect that changing the database content from the background thread by following the guidelines outlined here would trigger kvo observers like Live Queries.

It looks like it could be a bug. Can you open a ticket on github describing what happens and what you would expect to happen? (an xcode proj reproducing the issue would be great!).

James

Hi,

The sync methods fetch a particular json feed (async) with AFNetworking and call backgroundTellDatabaseNamed in their completion block to save the items from the feed.

[operation setCompletionBlockWithSuccess:^(AFHTTPRequestOperation *_operation, id responseObject) {
[[CBLManager sharedInstance] backgroundTellDatabaseNamed: @“app-db” to: ^(CBLDatabase *backgroundDatabase) {
// Iterating json objects and handling them with backgroundDatabase
}];
} failure: …];

The data gets succesfully updated (cf. manually reloading a tableview or scrolling a tableview), but there is no live query notification.
If you replace backgroundTellDatabaseNamed with the main database, there is no problem with the notifications.

I will create an issue on GitHub.

Thanks