How to synchronize documents and views between two CBLDatabase instances properly?

23 views
Skip to first unread message

Shen Kevin

unread,
Jul 2, 2017, 12:25:40 PM7/2/17
to Couchbase Mobile
Hi,

I want to read, save and query data in both main thread and background thread, but I don't know how to
implement this properly. 

It seems that documents and views are not synchronized between two CBLDatabase instances
which share one database file.

Here are what I have so far:

@property (nonatomic, strong) CBLManager *manager;


@property (nonatomic, strong) dispatch_queue_t backgroundTaskQueue;

@property (nonatomic, strong) CBLManager *backgroundManager;

@property (nonatomic, strong) CBLDatabase *backgroundDatabase;


- (
id)init {


    self = [super init];


    if (self) {


        NSError *error;


        _manager = [[CBLManager alloc] init];


        if (!_manager) {


            NSLog(@"Cannot create CBLManager");


            return nil;


        }


        _database = [_manager databaseNamed:@"datastore" error:&error];


        if (!_database) {


            NSLog(@"Cannot create database. Error message: %@", error.localizedDescription);


            return nil;


        }

       _backgroundTaskQueue = dispatch_queue_create("wapos.database.queue", DISPATCH_QUEUE_SERIAL);

       _backgroundManager = [self.manager copy];


       _backgroundManager.dispatchQueue = _backgroundTaskQueue;

       [self setupBackgroundDatabase];

   }


    return self;


}

- (void)setupBackgroundDatabase {

   @weakify(self);

   NSString *databaseName = self.database.name;

   dispatch_sync(_backgroundTaskQueue, ^{

       @strongify(self);

       NSError *error;

       self.backgroundDatabase = [self.backgroundManager databaseNamed:databaseName error:&error];

   });

}



I use gcd to dispatch async task 

dispatch_async(self.dbManager.backgroundTaskQueue, ^{

}


The workaround I use now is like

// Called in response to replication-change notifications.


- (void)replicationProgress: (NSNotification *)notification {
   
NSNotificationCenter *defaultCenter = [NSNotificationCenter defaultCenter];

   

   CBLReplication *replication = (CBLReplication *)notification.object;

   BOOL active = _pull.status == kCBLReplicationActive || _push.status == kCBLReplicationActive;

   self.syncing = active;

   if (active) {


   
} else {
     
[self setupBackgroundDatabase];

  }
}


- (
void)setupBackgroundDatabase {


    @weakify(self);


    NSString *databaseName = self.database.name;


    dispatch_sync(_backgroundTaskQueue, ^{


        @strongify(self);


        NSError *error;


        self.backgroundDatabase = [self.backgroundManager databaseNamed:databaseName error:&error];


        [DBManager setupDatabase:self.backgroundDatabase];


    });


}


This will reload background database instance after new document is saved to main database.
It solves document synchronization problem between main and background CBLDatabases, but not including CBLView problem.

Is there any better approach to achieve the same goal?


Jens Alfke

unread,
Jul 2, 2017, 2:04:08 PM7/2/17
to mobile-c...@googlegroups.com

On Jul 2, 2017, at 3:58 AM, Shen Kevin <vampi...@gmail.com> wrote:

It seems that documents and views are not synchronized between two CBLDatabase instances
which share one database file.

No, they are, and I’m not aware of any bugs in this functionality. Multi-threading is always tricky, and you do need to create separate manager and database objects for each thread/queue, but your code looks correct to me.

You haven’t said what problems you get without your workaround. Can you describe them in detail?

—Jens

Shen Kevin

unread,
Jul 5, 2017, 11:14:27 PM7/5/17
to Couchbase Mobile
After I completely reviewed my code, I found that the main database is always recreated in my flow. 
But I forgot recreating background database, therefore this silly mistake results in the background database 
stop functioning.

Anyway, it is working fine now. Thank you for your help.


Jens Alfke於 2017年7月3日星期一 UTC+8上午2時04分08秒寫道:
Reply all
Reply to author
Forward
0 new messages