diff --git a/MatrixSDK/Data/Store/MXFileStore/MXFileStore.m b/MatrixSDK/Data/Store/MXFileStore/MXFileStore.m index 33d13627db..e84954fc22 100644 --- a/MatrixSDK/Data/Store/MXFileStore/MXFileStore.m +++ b/MatrixSDK/Data/Store/MXFileStore/MXFileStore.m @@ -2178,14 +2178,14 @@ - (void)saveReceipts { NSArray *roomsToCommit = [[NSArray alloc] initWithArray:roomsToCommitForReceipts copyItems:YES]; [roomsToCommitForReceipts removeAllObjects]; - + #if DEBUG MXLogDebug(@"[MXFileStore commit] queuing saveReceipts for %tu rooms", roomsToCommit.count); #endif MXWeakify(self); dispatch_async(dispatchQueue, ^(void){ MXStrongifyAndReturnIfNil(self); - + #if DEBUG NSDate *startDate = [NSDate date]; #endif @@ -2199,31 +2199,37 @@ - (void)saveReceipts { NSString *file = [self threadedReadReceiptsFileForRoom:roomId forBackup:NO]; NSString *backupFile = [self threadedReadReceiptsFileForRoom:roomId forBackup:YES]; - + // Backup the file if (backupFile && [[NSFileManager defaultManager] fileExistsAtPath:file]) { [self checkFolderExistenceForRoom:roomId forBackup:YES]; [[NSFileManager defaultManager] moveItemAtPath:file toPath:backupFile error:nil]; } - + // Store new data [self checkFolderExistenceForRoom:roomId forBackup:NO]; - NSError *error = nil; - NSData *result = [NSKeyedArchiver archivedDataWithRootObject:receiptsStore requiringSecureCoding:false error:&error]; + __block NSData *result = nil; + __block NSError *archiveError = nil; - if (error != nil) - { - MXLogErrorDetails(@"Failed archiving receipts store", error); + dispatch_sync(dispatch_get_main_queue(), ^{ + result = [NSKeyedArchiver + archivedDataWithRootObject:receiptsStore + requiringSecureCoding:NO + error:&archiveError]; + }); + + if (archiveError) { + MXLogErrorDetails(@"Failed archiving receipts store", archiveError); continue; } - [result writeToURL:[NSURL fileURLWithPath:file] options: NSDataWritingAtomic error: &error]; + [result writeToURL:[NSURL fileURLWithPath:file] options: NSDataWritingAtomic error: &archiveError]; - if (error != nil) + if (archiveError != nil) { - MXLogErrorDetails(@"Failed writing receipts store to file", error); + MXLogErrorDetails(@"Failed writing receipts store to file", archiveError); continue; } } diff --git a/changelog.d/1810.bugfix b/changelog.d/1810.bugfix new file mode 100644 index 0000000000..8b9c7092dd --- /dev/null +++ b/changelog.d/1810.bugfix @@ -0,0 +1 @@ +MXFileStore.saveReceipts: Fixed a race condition involving multiple saves to the RoomThreadedReceiptsStore while commiting more receipts. Contributed by the Connect v3 Team at @powerhome \ No newline at end of file