I need some help figuring out why my iOS application is crashing due to concurrency issues when my program goes into the the on resume state. Inside the application it is running fine. Do I need to save the manager ID for it to be on the same thread always? or how would I fix it as it is saying the thread is not the same thread started on, and please don’t point me to the documentation for concurrency as it is not helpful.
Are you calling Couchbase Lite from a background thread or dispatch queue? If so, then you need to make another CBLManager to use on that queue. I’m guessing that on wake your app is being called by UIKit and accidentally using the background CBLManager instead of the foreground one.
It would be useful if you could post a backtrace of the thread that gets the error about calling on the wrong thread.
I do have a background refresher going in the background which updates the local database, but I have a method in the AppDelegate that creates the manager, creates the databases I need; and when I need to access the database it is called from the app delegate. My issue is that it is returning
***** THREAD-SAFETY VIOLATION: This database is being used on a thread it wasn’t created on! Please see the concurrency guidelines in the Couchbase Lite documentation. *****
when you go back into the app after a while it will throw this error.
Sounds like your app delegate is getting called from a background thread. When you get the exception, look at the thread’s backtrace to see what thread you’re on and how you got there. If you need help, capture a backtrace (type “bt” in the debugger console) and paste it here and I’ll look at it.
Well, that’s an anonymous background thread used by GCD, so it looks like you’re definitely not running on the main thread.
However, the backtrace doesn’t show any CBL or app code, just system stuff, so this must be after the exception’s been thrown. Set an exception breakpoint so you can drop into the debugger when the exception is thrown; that way your code and/or CBL code will be on the stack. (Show the breakpoint navigator, then press the “+” button at the bottom left.)
OK, it looks like you’ve used dispatch_async or something like that to call your CBL on a background dispatch queue.
But it sounds like you’re normally using CBL on the main thread. If so, you cannot use the same objects on both threads. You have to create a separate CBLManager to use on that dispatch queue.
please don’t point me to the documentation for concurrency as it is not helpful.
Why is it unhelpful? Did you just not understand it?
Ok, I think I’m understanding it now, so I need to create a new manager when in the background basically and when the app goes on resume it’ll have its the other manager basically?
No, it has nothing to do with whether the app is in the background.
CBL objects are (like most Cocoa objects, including Core Data’s) not thread-safe. They can’t be used on multiple threads simultaneously. So if you want to run some of your code on other threads/queues, you have to create a second set of objects (that use the same database file).
Are you experienced with multithreaded programming? If not, it would really be safer to do all of your work on the main thread and not use dispatch queues. (I don’t mean to sound patronizing! But from this thread I get the impression you’re not really aware of the ramifications of running multiple threads/queues.)