diff --git a/installations/InstallationsSnippets/AppDelegate.swift b/installations/InstallationsSnippets/AppDelegate.swift index 91603fcf..0258c6d6 100644 --- a/installations/InstallationsSnippets/AppDelegate.swift +++ b/installations/InstallationsSnippets/AppDelegate.swift @@ -39,13 +39,14 @@ class AppDelegate: UIResponder, UIApplicationDelegate { // Use this method to release any resources that were specific to the discarded scenes, as they will not return. } + var installationIDObserver: NSObjectProtocol? func handleInstallationIDChange() { // [START handle_installation_id_change] - NotificationCenter.default.addObserver(forName: .InstallationIDDidChange, - object: nil, - queue: nil) { (notification) in + installationIDObserver = NotificationCenter.default.addObserver(forName: .InstallationIDDidChange, + object: nil, + queue: nil) { [weak self] (notification) in // Fetch new Installation ID - self.fetchInstallationToken() + self?.fetchInstallationToken() } // [END handle_installation_id_change] } diff --git a/installations/InstallationsSnippets/ObjCSnippets.m b/installations/InstallationsSnippets/ObjCSnippets.m index d51c6dee..16620dd1 100644 --- a/installations/InstallationsSnippets/ObjCSnippets.m +++ b/installations/InstallationsSnippets/ObjCSnippets.m @@ -18,16 +18,22 @@ #import "ObjCSnippets.h" +@interface ObjCSnippets () +@property(nonatomic, nullable) id installationIDObserver; +@end + @implementation ObjCSnippets - (void)handleInstallationIDChange { // [START handle_installation_id_change] - [[NSNotificationCenter defaultCenter] addObserverForName: FIRInstallationIDDidChangeNotification - object:nil - queue:nil - usingBlock:^(NSNotification * _Nonnull notification) { + __weak __auto_type weakSelf = self; + self.installationIDObserver = + [[NSNotificationCenter defaultCenter] addObserverForName: FIRInstallationIDDidChangeNotification + object:nil + queue:nil + usingBlock:^(NSNotification * _Nonnull notification) { // Fetch new Installation ID - [self fetchInstallationsID]; + [weakSelf fetchInstallationsID]; }]; // [END handle_installation_id_change] }