Hi All,
The app I’m developing (OSX, iOS) uses CBLLiveQuery objects to monitor for changes. I have a set of views and pull different types of data based on a “type” field.
Things work well - the UI updates as changes occur in the data. But quite regularly, the CBLLiveQuery object returns no rows when I know there to be rows. When I get no rows, I’ve added a hack to create a new CBLQuery check the results - and that always returns the right data. Is there anything I am doing that could cause CBLLiveQuery to be unreliable?
The code looks like this:
First establishing the live query:
let query = gDB.viewNamed("objectsByType").createQuery()
query.keys = ["book"]
liveQuery = query.asLiveQuery()
liveQuery!.addObserver(self, forKeyPath: "rows", options: [.New, .Old, .Initial], context: nil)
liveQuery!.start()
In my observer code - wait for the object to be equal to my liveQuery object, and if it does, call reloadLive
override func observeValueForKeyPath(keyPath: String?, ofObject object: AnyObject?, change: [String : AnyObject]?, context: UnsafeMutablePointer<Void>) {
if object! as? NSObject == liveQuery {
reloadLive()
}
}
The reloadLive function repopulates a cache of objects built from the query results:
func reloadLive() {
if var rows = liveQuery!.rows {
cache.removeAll()
// Walk the rows creating new Book objects from the property maps, adding them to the cache.
while let row = rows.nextRow() {
if let props = row.document!.properties {
let data = Book(fromMap:props)
cache.append(data)
}
}
sortCache()
}
My hack to work around the problem is: at the end of the function check for 0 rows, and if there are none I load the data from a regular query to confirm:
if cache.count == 0 {
let query = gDB.viewNamed("objectsByType").createQuery()
query.keys = ["book"]
if let rows = try? query.run() {
// Walk the rows creating new Book objects from the property maps, adding them to the cache.
for let row = rows.nextRow() {
if let props = row.document!.properties {
let data = Book(fromMap:props)
cache.append(data)
}
}
}
This correctly returns the right data, every time.
For the moment, I’ve turned syncing off to make sure that is not causing any issues, but that has not helped.
I think my views are created ok because regular queries work just fine.
I’ve checked the DB contents using Couchbase Lite Viewer, and I can see my data there.
I’m not touching the liveQuery object (CBLLiveQuery) anywhere else in the app.
Does anyone have some insight as to why this might be happening?
Thanks for your help.
Paul.