Skip to content

Commit c66d3d5

Browse files
authored
Merge pull request #140 from matrix-org/vector_623
App crashes when we start a chat during initial sync
2 parents 31226a1 + a64f325 commit c66d3d5

File tree

3 files changed

+47
-22
lines changed

3 files changed

+47
-22
lines changed

MatrixSDK/Data/MXUser.h

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -92,7 +92,6 @@
9292
*/
9393
- (void)updateWithRoomMemberEvent:(MXEvent*)roomMemberEvent roomMember:(MXRoomMember *)roomMember inMatrixSession:(MXSession*)mxSession;
9494

95-
9695
/**
9796
Update the MXUser data with a m.presence event.
9897
@@ -101,6 +100,15 @@
101100
*/
102101
- (void)updateWithPresenceEvent:(MXEvent*)presenceEvent inMatrixSession:(MXSession*)mxSession;
103102

103+
/**
104+
Make a request to the homeserver to update the MXUser.
105+
106+
@param mxSession the mxSession to the home server.
107+
@param success a block when the operation succeeds.
108+
@param failure a block when the operation fails.
109+
*/
110+
- (void)updateFromHomeserverOfMatrixSession:(MXSession*)mxSession success:(void (^)())success failure:(void (^)(NSError *error))failure;
111+
104112

105113
#pragma mark - Events listeners
106114
/**

MatrixSDK/Data/MXUser.m

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -122,6 +122,31 @@ - (void)updateWithPresenceEvent:(MXEvent*)presenceEvent inMatrixSession:(MXSessi
122122
[self notifyListeners:presenceEvent];
123123
}
124124

125+
- (void)updateFromHomeserverOfMatrixSession:(MXSession *)mxSession success:(void (^)())success failure:(void (^)(NSError *))failure
126+
{
127+
[mxSession.matrixRestClient displayNameForUser:_userId success:^(NSString *displayname) {
128+
129+
[mxSession.matrixRestClient avatarUrlForUser:_userId success:^(NSString *avatarUrl) {
130+
131+
self.displayname = displayname;
132+
self.avatarUrl = avatarUrl;
133+
134+
success();
135+
136+
[self notifyListeners:nil];
137+
138+
} failure:^(NSError *error) {
139+
140+
NSLog(@"[MXUser] updateFromHomeserverOfMatrixSession. Error when getting avatar: %@", error);
141+
failure(error);
142+
}];
143+
} failure:^(NSError *error) {
144+
145+
NSLog(@"[MXUser] updateFromHomeserverOfMatrixSession. Error when getting avatar: %@", error);
146+
failure(error);
147+
}];
148+
}
149+
125150
- (NSUInteger)lastActiveAgo
126151
{
127152
NSUInteger lastActiveAgo = -1;

MatrixSDK/MXSession.m

Lines changed: 13 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -227,8 +227,12 @@ -(void)setStore:(id<MXStore>)store success:(void (^)())onStoreDataReady failure:
227227
}
228228
else
229229
{
230+
// Create self.myUser instance to expose the user id as soon as possible
231+
_myUser = [[MXMyUser alloc] initWithUserId:matrixRestClient.credentials.userId];
232+
_myUser.mxSession = self;
233+
230234
NSLog(@"[MXSession] Total time to mount SDK data from MXStore: %.0fms", [[NSDate date] timeIntervalSinceDate:startDate] * 1000);
231-
235+
232236
[self setState:MXSessionStateStoreDataReady];
233237

234238
// The SDK client can use this data
@@ -299,31 +303,19 @@ - (void)startWithMessagesLimit:(NSUInteger)messagesLimit
299303
{
300304
// Get data from the home server
301305
// First of all, retrieve the user's profile information
302-
[matrixRestClient displayNameForUser:matrixRestClient.credentials.userId success:^(NSString *displayname) {
306+
[_myUser updateFromHomeserverOfMatrixSession:self success:^{
303307

304-
[matrixRestClient avatarUrlForUser:matrixRestClient.credentials.userId success:^(NSString *avatarUrl) {
305-
306-
// Create the user's profile
307-
_myUser = [[MXMyUser alloc] initWithUserId:matrixRestClient.credentials.userId andDisplayname:displayname andAvatarUrl:avatarUrl];
308-
_myUser.mxSession = self;
309-
310-
// And store him as a common MXUser
311-
[_store storeUser:_myUser];
312-
313-
// Initial server sync
314-
[self serverSyncWithServerTimeout:0 success:onServerSyncDone failure:^(NSError *error) {
308+
// And store him as a common MXUser
309+
[_store storeUser:_myUser];
315310

316-
[self setState:MXSessionStateInitialSyncFailed];
317-
failure(error);
311+
// Initial server sync
312+
[self serverSyncWithServerTimeout:0 success:onServerSyncDone failure:^(NSError *error) {
318313

319-
} clientTimeout:CLIENT_TIMEOUT_MS setPresence:nil];
320-
321-
} failure:^(NSError *error) {
322-
323314
[self setState:MXSessionStateInitialSyncFailed];
324315
failure(error);
325-
326-
}];
316+
317+
} clientTimeout:CLIENT_TIMEOUT_MS setPresence:nil];
318+
327319
} failure:^(NSError *error) {
328320

329321
[self setState:MXSessionStateInitialSyncFailed];

0 commit comments

Comments
 (0)