Swift: Performing operation in background using GCD

32 views
Skip to first unread message

parvez....@decurtis.com

unread,
Jul 26, 2017, 10:40:35 AM7/26/17
to Couchbase Mobile
For an iOS application using Couchbase lite version 1.3.1 (build 6), I need to move all things related to couchbase on a background queue as UI is not directly consuming the data from database. I tried below code snippet to start the things in background but it is printing warning in console as:


-[CBLDatabase waitFor:] cannot be used with dispatch queues, only runloops {at -[CBLDatabase waitFor:]:183}


The code for dispatchQueue setup is as below:

var manager:CBLManager! //global variable
var serialQueue: DispatchQueue! //global variable

manager = CBLManager.sharedInstance().copy()
serialQueue = DispatchQueue(label: "com.background.process")
manager.dispatchQueue = serialQueue

After above setup, I call a function from main thread:

public func setupDatabase(){
        
        var databaseError: Error?
        
        serialQueue.async {
            do{
                self.database = try self.manager.databaseNamed(name)
                self.setupDatabaseReplication()
                self.registerModelClasses()
                self.setupViews()                
            }catch{
                print("Encountered error: \(error) while setting up database")
            }
        }        
}

All the function called inside the do block like database replication, register model classes etc are not dispatching on other queues but are instead simple functions. Why am I getting warning in this case?

What is wrong with above?
Is their any other way to accomplish things in background? I want to maintain a single queue to perform all database operations.

Thanks

Jens Alfke

unread,
Jul 26, 2017, 12:06:47 PM7/26/17
to mobile-c...@googlegroups.com

On Jul 26, 2017, at 7:40 AM, parvez....@decurtis.com wrote:

What is wrong with above?

You’re calling -[CBLLiveQuery waitForRows]. This isn’t supported on GCD queues (for complicated reasons.) Instead of blocking, use KVO to observe the .rows property and do your stuff when it changes.

—Jens
Reply all
Reply to author
Forward
0 new messages