WorryFree Computers   »   [go: up one dir, main page]

Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Using FirebaseApp.delete() results in unusable Messaging singleton #3411

Closed
cooksimo opened this issue Jul 22, 2019 · 5 comments · Fixed by #3579
Closed

Using FirebaseApp.delete() results in unusable Messaging singleton #3411

cooksimo opened this issue Jul 22, 2019 · 5 comments · Fixed by #3579
Assignees
Milestone

Comments

@cooksimo
Copy link
cooksimo commented Jul 22, 2019

Describe your environment

  • Xcode version: 10.2.1
  • Firebase SDK version: 6.5.0
  • Firebase Component: Messaging
  • Component version: 4.1.1

Describe the problem

We have several projects set up, representing different environments for remote config. We use Messaging to enable real time remote config updates. When a user switches the firebase environment within the app, we delete the default app, and attempt to recreate it.

FirebaseApp.delete({ _ in })
FirebaseApp.configure(options: options)

Once this has been done any interaction with Messaging will not work. We have encountered this because we were running a very old version of Firebase (4.13.0) & Messaging (2.2.0) and we're trying to update to the latest. This approach worked in those versions.

This problem looks to be in the implementation of messaging(), there is a dispatch_once block to actually set everything up, this doesn't make sense if the singleton object can actually be deallocated and recreated.

I have attempted to solve this by calling start manually, and this seems to fix some things, but the new Messaging instance never gets notified of the fcmToken being available so cannot subscribe to anything.

@morganchen12
Copy link
Contributor

IIRC this has never been an officially supported use case, though we also don't explicitly label deleteApp: as unrecoverable. We can revisit this behavior in API review, but in the meantime, what use case are you trying to accomplish by deleting and reconstructing the Firebase app?

@cooksimo
Copy link
Author

Our use case is to have production, staging and sandbox environments for Firebase, with an in-app option to toggle between these environments for debug builds.

@cooksimo
Copy link
Author

I have managed to workaround this and get it working again, but it's very hacky.

When we switch environments we do this:

var oldMessaging: Messaging? = nil
if FirebaseApp.app() != nil {
    oldMessaging = Messaging.messaging()
}

...

// If we are switching environments, we need to delete the old FirebaseApp and recreate a new one
if let app = FirebaseApp.app() {
    app.delete({ _  in })

    FirebaseApp.configure(options: options)

    if oldMessaging != nil {
        Messaging.messaging().perform(Selector("start"))
        let notification = Notification(name: Notification.Name(rawValue: "com.firebase.iid.notif.fcm-token"), object: Messaging.messaging().fcmToken, userInfo: nil) // The Messaging instance does know its token, things just didn't get wired back up properly
        NotificationQueue.default.enqueue(notification, postingStyle: .asap)
        Messaging.messaging().delegate = oldMessaging?.delegate
        Messaging.messaging().apnsToken = oldMessaging?.apnsToken
    }
}
else {
    FirebaseApp.configure(options: options)
}

ryanwilson added a commit that referenced this issue Aug 13, 2019
This fixes #3411. Test fails before the code change, and succeeds
afterwards.
@ryanwilson
Copy link
Member

Thanks @cooksimo for the detailed write-up and investigation into what the cause was!

ryanwilson added a commit that referenced this issue Aug 14, 2019
* Ensure Messaging instance is usable after FIRApp's `delete`

This fixes #3411. Test fails before the code change, and succeeds
afterwards.

* Update CHANGELOG.md

* Use Bug number instead of PR number
@paulb777 paulb777 added this to the 6.7.0 milestone Aug 14, 2019
@nordfogel
Copy link

Is it intended not taking the delegate into account by recreating the Messaging singleton after a FirebaseApp is deleted and recreated?

@property(nonatomic, weak, nullable) id<FIRMessagingDelegate> delegate;

@firebase firebase locked and limited conversation to collaborators Oct 11, 2019
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

Successfully merging a pull request may close this issue.

6 participants