diff --git a/MatrixSDK/Data/MXRoomState.m b/MatrixSDK/Data/MXRoomState.m index 12ed90b8ee..0f19cd8a88 100644 --- a/MatrixSDK/Data/MXRoomState.m +++ b/MatrixSDK/Data/MXRoomState.m @@ -120,23 +120,31 @@ + (void)loadRoomStateFromStore:(id)store { [store stateOfRoom:roomId success:^(NSArray * _Nonnull stateEvents) { if (!stateEvents.count) { - MXLogWarning(@"[MXRoomState] loadRoomStateFromStore(%@): No state events stored, loading from api", logId); + MXLogWarning(@"[MXRoomState] loadRoomStateFromStore(%@): No state events stored, loading from API", logId); - [matrixSession.matrixRestClient stateOfRoom:roomId success:^(NSArray *JSONData) { - NSArray *events = [MXEvent modelsFromJSON:JSONData]; - MXLogDebug(@"[MXRoomState] loadRoomStateFromStore(%@): Loaded %lu events from api", logId, events.count); - - [roomState handleStateEvents:events]; - onComplete(roomState); - } failure:^(NSError *error) { - NSDictionary *details = @{ - @"log_id": logId ?: @"unknown", - @"error": error ?: @"unknown" - }; - MXLogErrorDetails(@"[MXRoomState] loadRoomStateFromStore: Failed to load any events from api", details); - + if (!matrixSession) + { + MXLogError(@"[MXRoomState] loadRoomStateFromStore: Missing session, unable to load from API") onComplete(roomState); - }]; + } + else + { + [matrixSession.matrixRestClient stateOfRoom:roomId success:^(NSArray *JSONData) { + NSArray *events = [MXEvent modelsFromJSON:JSONData]; + MXLogDebug(@"[MXRoomState] loadRoomStateFromStore(%@): Loaded %lu events from api", logId, events.count); + + [roomState handleStateEvents:events]; + onComplete(roomState); + } failure:^(NSError *error) { + NSDictionary *details = @{ + @"log_id": logId ?: @"unknown", + @"error": error ?: @"unknown" + }; + MXLogErrorDetails(@"[MXRoomState] loadRoomStateFromStore: Failed to load any events from API", details); + + onComplete(roomState); + }]; + } } else { MXLogDebug(@"[MXRoomState] loadRoomStateFromStore(%@): Initializing with %lu state events", logId, stateEvents.count); diff --git a/changelog.d/pr-1833.bugfix b/changelog.d/pr-1833.bugfix new file mode 100644 index 0000000000..b87469de9c --- /dev/null +++ b/changelog.d/pr-1833.bugfix @@ -0,0 +1 @@ +Fix unhandled callback when the session is nil. \ No newline at end of file