Performing sync on push notification

86 views
Skip to first unread message

Nick Pomfret

unread,
Jun 20, 2016, 8:32:15 AM6/20/16
to Couchbase Mobile
I'm trying to get couchbase lite (on iOS for now) to sync documents when its awoken in the background from a push notification.  So far I can accept and process a notification while the app is in the background.  And I have given it 10 seconds before the completionHandler gets called.  But during that 10 seconds I can't see any activity.

Any pointers on what I've missed would be appreciated. 

Jens Alfke

unread,
Jun 25, 2016, 5:15:08 PM6/25/16
to mobile-c...@googlegroups.com

On Jun 20, 2016, at 8:32 AM, Nick Pomfret <npom...@gmail.com> wrote:

I'm trying to get couchbase lite (on iOS for now) to sync documents when its awoken in the background from a push notification.  So far I can accept and process a notification while the app is in the background.  And I have given it 10 seconds before the completionHandler gets called.  But during that 10 seconds I can't see any activity.

Did you set the CBLReplication's `suspended` property to NO?

/** Suspends/resumes a replication.
    On iOS a replication will suspend itself when the app goes into the background, and resume
    when the app is re-activated. If your app receives a push notification while suspended and needs
    to run the replication to download new data, your handler should set suspended to NO to resume
    replication, and then set the property back to YES when it's done. */
@property BOOL suspended;

—Jens

Nick Pomfret

unread,
Jun 26, 2016, 2:33:01 AM6/26/16
to mobile-c...@googlegroups.com
Thanks for the reply.  Could you point me to the source of these docs you're quoting and I'll try and figure it all out.

--
You received this message because you are subscribed to a topic in the Google Groups "Couchbase Mobile" group.
To unsubscribe from this topic, visit https://groups.google.com/d/topic/mobile-couchbase/EOkrycvMnas/unsubscribe.
To unsubscribe from this group and all its topics, send an email to mobile-couchba...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/mobile-couchbase/08713E41-25DD-4203-8647-A49A4E3F7407%40couchbase.com.

For more options, visit https://groups.google.com/d/optout.

Jens Alfke

unread,
Jun 26, 2016, 7:17:39 PM6/26/16
to mobile-c...@googlegroups.com

On Jun 25, 2016, at 11:32 PM, Nick Pomfret <npom...@snowmonkey.co.uk> wrote:

Thanks for the reply.  Could you point me to the source of these docs you're quoting and I'll try and figure it all out.

It’s in the header file in the framework, CBLReplication.h. Almost all of the public API has such documentation comments.

In Xcode you can jump to the header by pressing Cmd-Shift-O and typing “CBLReplication”, or by Command-clicking the word CBLReplication in source. Or you can view the documentation for any class/method/property name by option-clicking it in your source.

—Jens

Nick Pomfret

unread,
Aug 19, 2016, 5:57:26 AM8/19/16
to Couchbase Mobile
Sorry, I'm struggling with this a bit (not a iOS programmer!).  I have a method in my AppDelegate

-(void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)notification fetchCompletionHandler:(void (^)(UIBackgroundFetchResult))completionHandler {


in which I'd like to switch on the replicator temporarily.  But I don't know how to get access to it in order to switch it on.

Nick Pomfret

unread,
Aug 19, 2016, 6:38:40 AM8/19/16
to Couchbase Mobile
I tried a basic brute force approach and just try and turn them all on:

  CBLManager *manager = [CBLManager sharedInstance];

  NSArray* dbNames = [manager allDatabaseNames];

  for (id name in dbNames) {

    CBLDatabase* db = [manager databaseNamed:name error:nil];

    NSArray* replications = [db allReplications];

    for(CBLReplication* rep in replications) {

      rep.suspended = false;

    }

  }


But the list of replications returned by the call to allReplications is empty.

James Nocentini

unread,
Aug 19, 2016, 8:17:16 AM8/19/16
to Couchbase Mobile
[db allReplications] returns all the running replications so it will be empty when there are no replications running. Did you try to use [database createPush/PullReplication:] to resume it? The replication will pick up from where it last stopped.

James

Nick Pomfret

unread,
Aug 19, 2016, 8:27:48 AM8/19/16
to mobile-c...@googlegroups.com
 The replicators were created using the REST api (from a react-native app).  I want to resume them so tried writing a bit of objective-c to do it as there doesn't seem to be a way using the REST API.

--
You received this message because you are subscribed to a topic in the Google Groups "Couchbase Mobile" group.
To unsubscribe from this topic, visit https://groups.google.com/d/topic/mobile-couchbase/EOkrycvMnas/unsubscribe.
To unsubscribe from this group and all its topics, send an email to mobile-couchbase+unsubscribe@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/mobile-couchbase/783cd947-3f74-4926-b3ef-100dc24342dd%40googlegroups.com.

Nick Pomfret

unread,
Aug 19, 2016, 8:40:09 AM8/19/16
to Couchbase Mobile
Strangely I also noticed that even when the app is running in the foreground that method always returns and empty array. 

Jens Alfke

unread,
Aug 19, 2016, 9:44:34 AM8/19/16
to mobile-c...@googlegroups.com

On Aug 19, 2016, at 8:27 AM, Nick Pomfret <npom...@snowmonkey.co.uk> wrote:

 The replicators were created using the REST api (from a react-native app).  I want to resume them so tried writing a bit of objective-c to do it as there doesn't seem to be a way using the REST API.

The implementation of the REST API doesn’t create CBLReplication objects — it calls into the lower level internal replicator class (CBL_Replicator). That means the public Obj-C API won’t see replications created by the REST API.

That doesn’t sound like a great design, now that I write it down like this, but it’s never come up as a problem before … :-/

I think we’ll need to add something to the REST API to allow you to unsuspend replications. Is there an issue covering this?

—Jens

Nick Pomfret

unread,
Aug 19, 2016, 10:00:54 AM8/19/16
to mobile-c...@googlegroups.com
Thanks Jens.  This would be a life saver.


--
You received this message because you are subscribed to a topic in the Google Groups "Couchbase Mobile" group.
To unsubscribe from this topic, visit https://groups.google.com/d/topic/mobile-couchbase/EOkrycvMnas/unsubscribe.
To unsubscribe from this group and all its topics, send an email to mobile-couchbase+unsubscribe@googlegroups.com.

Nick Pomfret

unread,
Aug 19, 2016, 10:57:51 AM8/19/16
to mobile-c...@googlegroups.com
I'm trying a workaround for now, which is to fire off a one-shot (non-continuous) replication when a push notification is received.  But its not working either, my method:

...{

    CBLManager *manager = [CBLManager sharedInstance];

    CBLDatabase* database = [manager databaseNamed:dbName error:nil];


    CBLReplication* pull = [database createPullReplication:syncURL];    

    CBLReplication* push = [database createPushReplication:syncURL];

    

    id<CBLAuthenticator> auth = [CBLAuthenticator basicAuthenticatorWithName: userId password: password];

    push.authenticator = pull.authenticator = auth;


    [pull start];

    [push start];

}

is blowing up when it reaches createPullReplication:syncURL it fails with:

Exception '-[__NSCFString user]: unrecognized selector sent to instance 0x148a7db20' was thrown while invoking startReplication on target ReactCBLite with params (

    "http://192.168.1.185:4984/sync_gateway",

    "pc_macmini_0_9133f13f-c7d9-41ae-a09f-37cdf906160b",

    "9133f13f-c7d9-41ae-a09f-37cdf906160b",

    "9fe7d83b-f6b7-4632-b293-a77356b76655"

)


And doesn't even get as far as the 'auth' line.  Any ideas?


Jens Alfke

unread,
Aug 19, 2016, 11:24:46 AM8/19/16
to mobile-c...@googlegroups.com

On Aug 19, 2016, at 10:57 AM, Nick Pomfret <npom...@snowmonkey.co.uk> wrote:

Exception '-[__NSCFString user]: unrecognized selector sent to instance 0x148a7db20’

Oh, it looks like your `syncURL` variable points to an NSString not an NSURL. I assume you’re setting it from a value bridged from JavaScript? You can set it like this:
NSURL* syncURL = [NSURL URLWithString: urlFromJS];

—Jens

Nick Pomfret

unread,
Aug 19, 2016, 11:30:02 AM8/19/16
to mobile-c...@googlegroups.com
Of course. Ugh, face-palm.  Thank you.

--
You received this message because you are subscribed to a topic in the Google Groups "Couchbase Mobile" group.
To unsubscribe from this topic, visit https://groups.google.com/d/topic/mobile-couchbase/EOkrycvMnas/unsubscribe.
To unsubscribe from this group and all its topics, send an email to mobile-couchbase+unsubscribe@googlegroups.com.
Reply all
Reply to author
Forward
0 new messages