Question

Trying out the sample couchbase lite sample app, it works great until I put the app in background by hitting the home button. When I return to the app and it enters the foreground, the sync stops working and the nsnotifications stop firing. What's the best way to have couchbase lite continue to sync when it enters the foreground again? Why does it stop receiving those notifications in the first place?

Here's the example.

https://github.com/couchbaselabs/Grocery-Sync-iOS

- (void)updateSyncURL {
    ...

    // Tell the database to use this URL for bidirectional sync.
    // This call returns an array of the pull and push replication objects:
    NSArray* repls = [self.database replicateWithURL: newRemoteURL exclusively: YES];
    if (repls) {
        _pull = repls[0];
        _push = repls[1];
        _pull.continuous = _push.continuous = YES;
        // Observe replication progress changes, in both directions:
        NSNotificationCenter* nctr = [NSNotificationCenter defaultCenter];
        [nctr addObserver: self selector: @selector(replicationProgress:)
                     name: kCBLReplicationChangeNotification object: _pull];
        [nctr addObserver: self selector: @selector(replicationProgress:)
                     name: kCBLReplicationChangeNotification object: _push];
    }
}

Should I just set an observer on application will enter foreground and run updateSyncURL again? If so is it best practice to add that observer only when the app is entering background and remove after it comes into foreground? Or does it not matter from an efficiency/best practice perspective?

[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(updateSyncURL) name:UIApplicationDidBecomeActiveNotification object:[UIApplication sharedApplication]];
Was it helpful?

Solution

Looks like if I set the replication to "persistent" it resumes on app backgrounding

e.g.

_pull.persistent = YES;

OTHER TIPS

I call it from application:didFinishLaunchingWithOptions. A quick test of my app showed it was successfully replicating after going to the Home screen and back.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top