KVO notification - dealloc race condition.

57 views
Skip to first unread message

Jordan Wood

unread,
Jan 18, 2016, 5:24:27 PM1/18/16
to Couchbase Mobile
Apple has a known problem with KVO where, if you are receiving a notification on a different dispatch queue than they are triggered on, then the call to `observeValueForKeyPath` can happen while the object is deallocating.  I caught my code doing this with live query observation (yay, unit tests!), and I'm trying to figure out how to prevent this situation from coming up in the future.  

Am I correct in thinking that the live query is being updated on Couchbase-lite's queue, as apposed to my own serial dispatch queue where the live query was created?  If so, then is it either possible or a good idea to run my call to addObserver on the same dispatch queue as the one where the live query is being updated?

Is there a better way to deal with this issue???

Thanks for the help!

Jordan

Jens Alfke

unread,
Jan 18, 2016, 7:35:39 PM1/18/16
to mobile-c...@googlegroups.com

On Jan 18, 2016, at 2:24 PM, Jordan Wood <wood.p...@gmail.com> wrote:

Am I correct in thinking that the live query is being updated on Couchbase-lite's queue, as apposed to my own serial dispatch queue where the live query was created?

No (I think, if I understand the question correctly.) The LiveQuery runs on the dispatch queue that you assigned the CBLManager to initially. It should only be posting notifications on that queue, unless there’s a bug I’m not aware of.

Couchbase Lite does have its own private background thread, which is used by the replicator and for doing async view queries. LiveQuery does use this thread to run the actual query, but that should be invisible to you; your code should never get invoked on that queue.

—Jens

Jordan Wood

unread,
Jan 31, 2016, 8:28:33 AM1/31/16
to Couchbase Mobile
Unfortunately, I can no longer replicate this issue.  I was seeing a case where observeValueForKeyPath was being called after dealloc for that object.  I'll keep my eye out for this problem in the future, but I checked, and that object is allocated and used all in the same dispatch queue, so I don't know how I got in that state.

Jordan
Reply all
Reply to author
Forward
0 new messages