Shielding concurrency issues from callers

45 views
Skip to first unread message

Phil Mitchell

unread,
Jun 10, 2016, 6:07:10 PM6/10/16
to Couchbase Mobile
I'm trying to set up a CBL-backed store so that callers don't need to know what thread they're accessing CBL on. I have a few points of confusion. (This is on iOS.)

1. The docs seem to indicate that dispatchQueue is no longer available as a property on CBLManager (1.2). I'm on 1.1 and dispatchQueue seems to solve my problem quite nicely, so I'm hoping this is a documentation bug (?). http://developer.couchbase.com/documentation/mobile/current/develop/references/couchbase-lite/couchbase-lite/manager/manager/index.html

2. When I set [CBLManager sharedInstance].dispatchQueue equal to a serial dispatch queue that I create, I seem to be able to then call CBL from any queue I want (including syncronously on main queue, and asynchronously on global queues as well as the serial queue itself). Is this correct or will this bite me later?

It's especially nice to be able to call it on global queues where one can set a priority.

Jens Alfke

unread,
Jun 10, 2016, 6:35:42 PM6/10/16
to mobile-c...@googlegroups.com

> On Jun 10, 2016, at 3:07 PM, Phil Mitchell <seasp...@gmail.com> wrote:
>
> 1. The docs seem to indicate that dispatchQueue is no longer available as a property on CBLManager (1.2). I'm on 1.1 and dispatchQueue seems to solve my problem quite nicely, so I'm hoping this is a documentation bug (?). http://developer.couchbase.com/documentation/mobile/current/develop/references/couchbase-lite/couchbase-lite/manager/manager/index.html

Those docs are cross-platform and only describe the standard API. They won’t include platform-specific functionality like access to dispatch queues. To see what’s available, look at the headers. CBLManager.h includes a dispatchQueue property.

> 2. When I set [CBLManager sharedInstance].dispatchQueue equal to a serial dispatch queue that I create, I seem to be able to then call CBL from any queue I want (including syncronously on main queue, and asynchronously on global queues as well as the serial queue itself). Is this correct or will this bite me later?

By “call” do you mean direct method calls, or are you using dispatch_sync and dispatch_async?

Direct method calls from other queues/threads will not work correctly. Dispatching blocks to the CBLManager’s queue will work; that’s exactly what it’s for.

—Jens
Message has been deleted

Phil Mitchell

unread,
Jun 10, 2016, 8:16:02 PM6/10/16
to Couchbase Mobile
On Friday, June 10, 2016 at 3:35:42 PM UTC-7, Jens Alfke wrote:
> On Jun 10, 2016, at 3:07 PM, Phil Mitchell <seasp...@gmail.com> wrote:
>
> 1. The docs seem to indicate that dispatchQueue is no longer available as a property on CBLManager (1.2). I'm on 1.1 and dispatchQueue seems to solve my problem quite nicely, so I'm hoping this is a documentation bug (?). http://developer.couchbase.com/documentation/mobile/current/develop/references/couchbase-lite/couchbase-lite/manager/manager/index.html

Those docs are cross-platform and only describe the standard API. They won’t include platform-specific functionality like access to dispatch queues. To see what’s available, look at the headers. CBLManager.h includes a dispatchQueue property.

Good to know!
 

> 2. When I set [CBLManager sharedInstance].dispatchQueue equal to a serial dispatch queue that I create, I seem to be able to then call CBL from any queue I want (including syncronously on main queue, and asynchronously on global queues as well as the serial queue itself). Is this correct or will this bite me later?

By “call” do you mean direct method calls, or are you using dispatch_sync and dispatch_async?

Direct method calls from other queues/threads will not work correctly. Dispatching blocks to the CBLManager’s queue will work; that’s exactly what it’s for.

Sorry, I guess I still don't really understand this. What I'm trying to support is two use cases:

1. Blocking calls from main queue: most classes call into the data store (on the main queue) and wait for a response;
2. Async calls dispatched to background queue: occasionally a class needs to run a longer job on a background thread.

But the caller may be quite removed from the store (many intervening classes), so I don't want the call into the store to have to specify which it is.

So if I'm understanding you, once I set dispatchQueue, my async calls (explicit dispatch_async()) will work fine, as long as they are dispatched to the same queue. But dispatching to a global queue OR my blocking calls from main queue may be unreliable.

Is that correct? And if so, would you advise (internally to the store) wrapping all CBL calls in an explicit dispatch_sync() onto the given dispatchQueue, which would take care of the blocking calls. And the async calls would also work, since they would simply wrap around the sync calls? That would hide everything from callers from the main queue.






 

—Jens

Jens Alfke

unread,
Jun 11, 2016, 4:10:49 PM6/11/16
to mobile-c...@googlegroups.com

On Jun 10, 2016, at 5:16 PM, Phil Mitchell <seasp...@gmail.com> wrote:

So if I'm understanding you, once I set dispatchQueue, my async calls (explicit dispatch_async()) will work fine, as long as they are dispatched to the same queue. But dispatching to a global queue OR my blocking calls from main queue may be unreliable.

I’m not quite following your questions.

The restriction is that you can only call a CBL object (I’m talking about direct method calls / property accesses) if you’re on the CBLManager’s dispatch queue. That’s it.

How you get your code to run on that queue is up to you. Generally you’d use dispatch_async or dispatch_sync to do it. You can also call -[CBLDatabase doAsync:] — this is basically a shortcut for using dispatch_async, and can be called from any thread (yes it’s an exception to the above rule.)

—Jens

Phil Mitchell

unread,
Jun 11, 2016, 7:11:49 PM6/11/16
to Couchbase Mobile
Alright, thanks Jens. I'm still unclear about how to proceed, but it's not your job to teach me about concurrency! I'll post back here if I need specifics about CBL.
Reply all
Reply to author
Forward
0 new messages