couchbaselabs / react-native-couchbase-lite

Couchbase Lite binding for React Native Android & iOS
MIT License
111 stars 54 forks source link

Getting CBL iOS working in the background #90

Open npomfret opened 7 years ago

npomfret commented 7 years ago

I'm seeing lots of errors when my app is backgrounded. It seems that after a while neither background fetch or push notifications adequately wake the app up. I understand that the solution might be to set the file protection mode. But I don't really understand how to do this. I tried making the following change (a completed guess) to ReactCBLite.m

        //CBLManager* dbmgr = [CBLManager sharedInstance];
        NSString* dir = [CBLManager defaultDirectory];
        CBLManagerOptions options = {};
        options.fileProtection = NSDataWritingFileProtectionCompleteUntilFirstUserAuthentication;
        NSError* error;
        CBLManager* dbmgr =   [[CBLManager alloc] initWithDirectory: dir options: &options error: &error];

But it didn't work. My databases aren't available to the listener. Any ideas?

npomfret commented 7 years ago

@yonahforst @jamiltz - i may have answered my own question here. It not tested properly yet, but after a bit scratching around for a solution I was advised to change the CBLManagerOptions and set fileProtection. Here's the change on my fork https://github.com/npomfret/react-native-couchbase-lite/commit/d67b48b34894040bf1ce46959666d6c782b519a9 .

If it works, is this something we want in master? I'm hoping it will allow the app to remain responsive while backgrounded for long periods of time. At the moment it seems to work for a bit when backgrounded, but after a while my push notification seem to stop working.

If we do put it in, should it be the default? Or should it be configurable from JS code?

https://github.com/couchbase/couchbase-lite-ios/issues/1405#issuecomment-249940618

https://groups.google.com/forum/?utm_medium=email&utm_source=footer#!msg/mobile-couchbase/X9TIHtNTf1Y/l9TzPwmAAAAJ

https://developer.apple.com/library/content/documentation/iPhone/Conceptual/iPhoneOSProgrammingGuide/StrategiesforImplementingYourApp/StrategiesforImplementingYourApp.html

npomfret commented 7 years ago

Any chance you guys could upvote https://github.com/couchbase/couchbase-lite-ios/issues/1410 (not sure it will help but worth a try!)

npomfret commented 7 years ago

@yonahforst @jamiltz - any thoughts on this? I've been using the suggested CBLManagerOptions for a couple of weeks now and they seem to be working well.

Is this something we should add to master?

danlannz commented 7 years ago

hey guys, any update / workaround for this? I'm experiencing what seems like the same issue on iOS.

Once the device is backgrounded then reopened again React Native can no longer access the local database with an error like [Error: Request has been terminated Possible causes: the network is offline, Origin is not allowed by Access-Control-Allow-Origin, the page is being unloaded, etc.]

The weirdest thing is that it works fine in the Simulator, it's only when run on a device that the issue occurs

danlannz commented 7 years ago

@npomfret I implemented your suggested changes to ReactCBLite.m and ReactCBLite.h but it still doesn't seem to work

npomfret commented 7 years ago

What did you implement? The options.fileProtection stuff?

danlannz commented 7 years ago

yea exactly, just this patch https://github.com/npomfret/react-native-couchbase-lite/commit/d67b48b34894040bf1ce46959666d6c782b519a9

danlannz commented 7 years ago

got to the bottom of it, was related to your couchbase/couchbase-lite-ios#1410 @npomfret

added a react native AppState event listener to control the Couchbase listener's state https://github.com/Keepsite/react-native-couchbase-lite/commit/e3722beed8c03bffee436e3ad992431d7212b820, is this something useful to others? can open a PR if so

jamesnocentini commented 7 years ago

Awesome work! 👍 It looks like there are two patches here so 2 different PRs.

https://github.com/Keepsite/react-native-couchbase-lite/commit/e3722beed8c03bffee436e3ad992431d7212b820 makes total sense. @DanlanNZ if you could open a PR to master and assign it to me that would be great.

Regarding https://github.com/npomfret/react-native-couchbase-lite/commit/d67b48b34894040bf1ce46959666d6c782b519a9, not sure what it is trying to fix because I haven't read much on the file protection options. If someone can explain it again (sorry if I missed an earlier comment) that would be great.

danlannz commented 7 years ago

cheers @jamiltz just opened three PR's, one is from https://github.com/EthanRTF/react-native-couchbase-lite to update CBL dependency for android these PR's probably relate to #103 also

re. npomfret/react-native-couchbase-lite@d67b48b @snej recommends it here, I didn't test it enough to confirm the iOS locked behaviour but I trust it makes a difference