Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 15 additions & 7 deletions NextcloudTalk/Rooms/NCRoomsManager.m
Original file line number Diff line number Diff line change
Expand Up @@ -164,27 +164,35 @@ - (void)updateRoomsUpdatingUserStatus:(BOOL)updateStatus onlyLastModified:(BOOL)
NSMutableArray *roomsWithNewMessages = [NSMutableArray new];

if (!error) {
__block BOOL expired = NO;
BGTaskHelper *bgTask = [BGTaskHelper startBackgroundTaskWithName:@"NCUpdateRoomsTransaction" expirationHandler:^(BGTaskHelper *task) {
expired = YES;
NSString *logMessage = [NSString stringWithFormat:@"ExpirationHandler called NCUpdateRoomsTransaction, number of rooms %ld", rooms.count];
[NCUtils log:logMessage];
}];

RLMRealm *realm = [RLMRealm defaultRealm];
NSInteger updateTimestamp = [[NSDate date] timeIntervalSince1970];

[realm transactionWithBlock:^{
NSInteger updateTimestamp = [[NSDate date] timeIntervalSince1970];

// Add or update rooms
for (NSDictionary *roomDict in rooms) {
if (expired) {
[realm cancelWriteTransaction];
return;
}

BOOL roomContainsNewMessages = [self updateRoomWithDict:roomDict withAccount:activeAccount withTimestamp:updateTimestamp withRealm:realm];

if (roomContainsNewMessages) {
NCRoom *room = [NCRoom roomWithDictionary:roomDict andAccountId:activeAccount.accountId];
[roomsWithNewMessages addObject:room];
}
}
}];
// Only delete rooms if it was a complete rooms update (not using modifiedSince)
if (!onlyLastModified) {
[realm transactionWithBlock:^{

// Only delete rooms if it was a complete rooms update (not using modifiedSince)
if (!onlyLastModified) {
// Delete old rooms
NSPredicate *roomsQuery = [NSPredicate predicateWithFormat:@"accountId = %@ AND lastUpdate != %ld", activeAccount.accountId, (long)updateTimestamp];
RLMResults *managedRoomsToBeDeleted = [NCRoom objectsWithPredicate:roomsQuery];
Expand All @@ -202,8 +210,8 @@ - (void)updateRoomsUpdatingUserStatus:(BOOL)updateStatus onlyLastModified:(BOOL)
}
}
[realm deleteObjects:managedRoomsToBeDeleted];
}];
}
}
}];

[bgTask stopBackgroundTask];
} else {
Expand Down