@@ -131,7 +131,9 @@ - (instancetype)initWithDelegate:(id<FIRMessagingClientDelegate>)delegate
131131}
132132
133133- (void )teardown {
134- FIRMessagingLoggerDebug (kFIRMessagingMessageCodeClient000 , @" " );
134+ if (![NSThread isMainThread ]) {
135+ FIRMessagingLoggerDebug (kFIRMessagingMessageCodeClient000 , @" FIRMessagingClient should be called from main thread only." );
136+ }
135137 self.stayConnected = NO ;
136138
137139 // Clear all the handlers
@@ -140,9 +142,8 @@ - (void)teardown {
140142 [self .connection teardown ];
141143
142144 // Stop all subscription requests
143- [self .registrar stopAllSubscriptionRequests ];
145+ [self .registrar stopAllSubscriptionRequests ];
144146
145- _FIRMessagingDevAssert (self.connection .state == kFIRMessagingConnectionNotConnected , @" Did not disconnect" );
146147 [NSObject cancelPreviousPerformRequestsWithTarget: self ];
147148
148149 [[NSNotificationCenter defaultCenter ] removeObserver: self ];
@@ -166,10 +167,7 @@ - (void)updateSubscriptionWithToken:(NSString *)token
166167 options : (NSDictionary *)options
167168 shouldDelete : (BOOL )shouldDelete
168169 handler : (FIRMessagingTopicOperationCompletion)handler {
169-
170- _FIRMessagingDevAssert (handler != nil , @" Invalid handler to FIRMessaging subscribe" );
171-
172- FIRMessagingTopicOperationCompletion completion = ^void (NSError *error) {
170+ FIRMessagingTopicOperationCompletion completion = ^void (NSError *error) {
173171 if (error) {
174172 FIRMessagingLoggerError (kFIRMessagingMessageCodeClient001 , @" Failed to subscribe to topic %@ " ,
175173 error);
@@ -182,7 +180,9 @@ - (void)updateSubscriptionWithToken:(NSString *)token
182180 @" Successfully subscribed to topic %@ " , topic);
183181 }
184182 }
185- handler (error);
183+ if (handler) {
184+ handler (error);
185+ }
186186 };
187187
188188 if ([[FIRInstanceID instanceID ] tryToLoadValidCheckinInfo ]) {
@@ -316,8 +316,6 @@ - (void)disconnectWithTryToConnectLater:(BOOL)tryToConnectLater {
316316
317317 self.stayConnected = tryToConnectLater;
318318 [self .connection signOut ];
319- _FIRMessagingDevAssert (self.connection .state == kFIRMessagingConnectionNotConnected ,
320- @" FIRMessaging connection did not disconnect" );
321319
322320 // since we can disconnect while still trying to establish the connection it's required to
323321 // cancel all performSelectors else the object might be retained
@@ -421,8 +419,6 @@ - (void)setupConnectionAndConnect {
421419- (void )setupConnection {
422420 NSString *host = FIRMessagingServerHost ();
423421 NSUInteger port = FIRMessagingServerPort ();
424- _FIRMessagingDevAssert ([host length ] > 0 && port != 0 , @" Invalid port or host" );
425-
426422 if (self.connection != nil ) {
427423 // if there is an old connection, explicitly sign it off.
428424 [self .connection signOut ];
@@ -447,17 +443,20 @@ - (void)tryToConnect {
447443 [NSObject cancelPreviousPerformRequestsWithTarget: self
448444 selector: @selector (tryToConnect )
449445 object: nil ];
450-
446+ NSString *deviceAuthID = [FIRInstanceID instanceID ].deviceAuthID ;
447+ NSString *secretToken = [FIRInstanceID instanceID ].secretToken ;
448+ if (deviceAuthID.length == 0 || secretToken.length == 0 ||
449+ !self.connection ) {
450+ FIRMessagingLoggerWarn (kFIRMessagingMessageCodeClientInvalidState ,
451+ @" Invalid state to connect, deviceAuthID: %@ , secretToken: %@ , connection state: %ld " ,
452+ deviceAuthID, secretToken, (long )self.connection .state );
453+ return ;
454+ }
451455 // Do not re-sign in if there is already a connection in progress.
452456 if (self.connection .state != kFIRMessagingConnectionNotConnected ) {
453457 return ;
454458 }
455459
456- _FIRMessagingDevAssert ([FIRInstanceID instanceID ].deviceAuthID .length > 0 &&
457- [FIRInstanceID instanceID ].secretToken .length > 0 &&
458- self.connection != nil ,
459- @" Invalid state cannot connect" );
460-
461460 self.connectRetryCount = MIN (kMaxRetryExponent , self.connectRetryCount + 1 );
462461 [self performSelector: @selector (didConnectTimeout )
463462 withObject: nil
@@ -466,9 +465,9 @@ - (void)tryToConnect {
466465}
467466
468467- (void )didConnectTimeout {
469- _FIRMessagingDevAssert (self.connection .state != kFIRMessagingConnectionSignedIn ,
470- @" Invalid state for MCS connection" );
471-
468+ if (self.connection .state == kFIRMessagingConnectionSignedIn ) {
469+ FIRMessagingLoggerWarn ( kFIRMessagingMessageCodeClientInvalidStateTimeout , @" Invalid state for connection timeout. " );
470+ }
472471 if (self.stayConnected ) {
473472 [self .connection signOut ];
474473 [self scheduleConnectRetry ];
0 commit comments