iOS: Observer stops working (likely after app in background state for a while).

1,204 views
Skip to first unread message

Dave Price

unread,
Apr 26, 2018, 2:53:24 PM4/26/18
to Firebase Google Group
I have an iOS App (in objective-c). I'm using [ref observeEventType:FIRDataEventTypeChildAdded withBlock:...].   I'm using a particular node as a mailbox, the observer block collects the "mail" and deletes it.  So, the user's "mailbox" should stay empty while the app has an Internet connection. 

This is working just fine most of the time.  However, after "a while", the Firebase observer stops observing (block never gets called).  I'm only adding the one observer in the entire app and it is protected from any double-observing by checking a flag inside of a thread-safe block. I've tried removing the observer and re-adding it when the app goes from background to foreground; but that didn't resolve the issue.  So, I've just now added code to log out of Firebase and log back in when the app goes to foreground.  I don't yet know if this will work, but I'm wondering why I'm even having to go though these gyrations (not to mention this adds a several second delay when starting the app before Firebase can start observing again).

Is this a common issue that folks just cope with?  I'm using Firebase for authentication, FCM, and the one observer.  The app is working perfectly in all other aspects, it's just the Firebase observer not firing.

Any ideas would be great, thanks!
-Dave

Kato Richardson

unread,
Apr 26, 2018, 3:09:31 PM4/26/18
to Firebase Google Group
Hi Dave,

When the app is backgrounded, the OS can judiciously sever socket connections (in reality it does this whenever it pleases without the app's permission). So naturally the observers would be offline until the app is focused and the OS decides to give back socket access.

FCM is a great use for this scenario since you can push to the device while an app is backgrounded.

☼, Kato

--
You received this message because you are subscribed to the Google Groups "Firebase Google Group" group.
To unsubscribe from this group and stop receiving emails from it, send an email to firebase-tal...@googlegroups.com.
To post to this group, send email to fireba...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/firebase-talk/e5843271-cf84-4989-bcee-12dacd493bc9%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.


--

Kato Richardson | Developer Programs Eng | kato...@google.com | 775-235-8398

Dave Price

unread,
Apr 28, 2018, 12:20:10 AM4/28/18
to Firebase Google Group
Hi, thank you for the reply.  

The issue I'm having isn't that the connection is severed when the app is in the background; it's that it doesn't seem to get restored upon the app returning to the foreground.  Shouldn't Firebase realize that its sockets are invalid and restore them?  And not after "some time", but, like... immediately?  Essentially, this is what I'm trying to "force" by removing the observer and re-adding it or re-logging-in upon [applicationWillEnterForeground:]... but I'm unsure if I've found the right combination of tricks to tell Firebase to start working again... it's also baffling to me that this would be acceptable behavior.

Kato Richardson

unread,
Apr 28, 2018, 1:28:37 PM4/28/18
to Firebase Google Group
Hi Dave,

Firebase will indeed sync once the sockets are restored. There are a myriad of moving parts that could be failing there, including the OS itself not properly restoring the connections or triggering the events to notify the app they are restored.

Recommend you try:
  • Latest versions of the SDKs
  • Different OS (and physical device if you're using emulator)
  • Ensuring that all your listeners/auth methods are capturing errors and logging them (it's likely you're missing a useful error message incidating why the connection isn't restored)
Assuming we're talking about Realtime Database ops (keep in mind Firebase is a suite of about 15 products and you haven't really indicated which ones are failing here), you can turn on debug logging to see what's going on at the socket level for DB ops:

JS:  firebase.database.enableLogging(true);
iOS:  [FIRDatabase setLoggingEnabled:YES];
Android:  FirebaseDatabase.getInstance().setLogLevel(Logger.Level.DEBUG);

☼, Kato

On Fri, Apr 27, 2018 at 9:20 PM Dave Price <davep...@gmail.com> wrote:
Hi, thank you for the reply.  

The issue I'm having isn't that the connection is severed when the app is in the background; it's that it doesn't seem to get restored upon the app returning to the foreground.  Shouldn't Firebase realize that its sockets are invalid and restore them?  And not after "some time", but, like... immediately?  Essentially, this is what I'm trying to "force" by removing the observer and re-adding it or re-logging-in upon [applicationWillEnterForeground:]... but I'm unsure if I've found the right combination of tricks to tell Firebase to start working again... it's also baffling to me that this would be acceptable behavior.

--
You received this message because you are subscribed to the Google Groups "Firebase Google Group" group.
To unsubscribe from this group and stop receiving emails from it, send an email to firebase-tal...@googlegroups.com.
To post to this group, send email to fireba...@googlegroups.com.

For more options, visit https://groups.google.com/d/optout.
Reply all
Reply to author
Forward
0 new messages