Replication and App Backgrounding on iOS

58 views
Skip to first unread message

Len Kawell

unread,
Jul 16, 2014, 8:59:05 PM7/16/14
to mobile-c...@googlegroups.com
Is there a recommended way to handle an iOS app resigning active state / entering the background (e.g., pressing the Home button) during a large replication (e.g., pulling 1,000s of documents)? 

CBLReplication.h says "On iOS a replication will suspend itself when the app goes into the background, and resume when the app is re-activated." But this does not seem to happen with our app running on iOS 7. According to the log, the replicator keeps pulling documents from the server even while the app is no longer active and it's in the background. In my testing, it seems to keep going for at least 8 minutes. According to Xcode it's using 30-50% CPU while in the background on WiFi, which might not be what the user is expecting.

Looking through the CBL iOS source code, it looks like the comment in CBLReplication.h has been superseded and there is now support in CBL_Replicator+Backgrounding.m to handle background replication on iOS. I see the"APP BACKGROUNDING" in the log, so I'm assuming that's enabling replication to continue running in the background.

I added my own code to our app delegate applicationWillResignActive method to stop all of the replications by invoking CBLReplication stop. [I tried setting CBLReplication.suspended = YES instead of invoking stop, but the replication continued to run (probably because of the backgrounding code resetting suspended).] However, invoking stop sometimes results in the following error:

WARNING*** : CBL_Puller[https:...]: Unable to save remote checkpoint: Error Domain=NSURLErrorDomain Code=-999 "The operation couldn’t be completed. (NSURLErrorDomain error -999.)"

Thanks very much.

Jens Alfke

unread,
Jul 16, 2014, 9:34:09 PM7/16/14
to mobile-c...@googlegroups.com
On Jul 16, 2014, at 5:59 PM, Len Kawell <len.k...@gmail.com> wrote:

CBLReplication.h says "On iOS a replication will suspend itself when the app goes into the background, and resume when the app is re-activated." But this does not seem to happen with our app running on iOS 7.

It’s probably more accurate to say “…will suspend itself when the app is in the background and the OS tells it to stop…”.

According to the log, the replicator keeps pulling documents from the server even while the app is no longer active and it's in the background. In my testing, it seems to keep going for at least 8 minutes. According to Xcode it's using 30-50% CPU while in the background on WiFi, which might not be what the user is expecting.

It’s playing by the rules, registering a background session and suspending when the OS calls it back and says it’s time to stop. Eight minutes in the background is longer than I would expect it to get, but that’s really the OS’s decision. The OS was probably giving background apps more time than normal because the device was charging (via the USB cable connected to your Mac.)

As a user, I prefer it if an app can keep doing its work in the background — I don’t like it when I’m held hostage by an app that’s doing a lengthy task (like a download) but cancels it if I background it. Of course I also want my devices to have good battery life, though. But I assume the OS is doing a decent job balancing those goals.

I added my own code to our app delegate applicationWillResignActive method to stop all of the replications by invoking CBLReplication stop. [I tried setting CBLReplication.suspended = YES instead of invoking stop, but the replication continued to run (probably because of the backgrounding code resetting suspended).]

Huh, that should work. The replicator won’t set suspended=NO when it goes into the background; that only happens when it returns to the foreground. It’s unpredictable whether your notification handler or the replicator’s will get called first, but either way the replication should end up being suspended.

However, invoking stop sometimes results in the following error:
WARNING*** : CBL_Puller[https:...]: Unable to save remote checkpoint: Error Domain=NSURLErrorDomain Code=-999 "The operation couldn’t be completed. (NSURLErrorDomain error -999.)"

-999 is kCFURLErrorCancelled. Maybe the replicator was in the process of saving the checkpoint at the moment it was told to stop; stopping a replication cancels all of its in-flight NSURLConnections.

—Jens
Reply all
Reply to author
Forward
0 new messages