Skip to content

Commit

Permalink
Merge pull request #1172 from nextcloud/background-fetch-improvements
Browse files Browse the repository at this point in the history
Background fetch improvements
  • Loading branch information
Ivansss committed Mar 31, 2023
2 parents 9dab18f + 3b2629c commit 424b8c9
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 2 deletions.
9 changes: 8 additions & 1 deletion NextcloudTalk/NCAPIController.m
Expand Up @@ -1257,7 +1257,14 @@ - (NSURLSessionDataTask *)receiveChatMessagesOfRoom:(NSString *)token fromLastMe
@"includeLastKnown" : include ? @(1) : @(0),
@"markNotificationsAsRead" : markNotificationsAsRead ? @(1) : @(0)};

NCAPISessionManager *apiSessionManager = [_longPollingApiSessionManagers objectForKey:account.accountId];
NCAPISessionManager *apiSessionManager;

if (timeout) {
apiSessionManager = [_longPollingApiSessionManagers objectForKey:account.accountId];
} else {
apiSessionManager = [_apiSessionManagers objectForKey:account.accountId];
}

NSURLSessionDataTask *task = [apiSessionManager GET:URLString parameters:parameters progress:nil success:^(NSURLSessionDataTask * _Nonnull task, id _Nullable responseObject) {
NSArray *responseMessages = [[responseObject objectForKey:@"ocs"] objectForKey:@"data"];
// Get X-Chat-Last-Given and X-Chat-Last-Common-Read headers
Expand Down
21 changes: 21 additions & 0 deletions NextcloudTalk/NCChatController.m
Expand Up @@ -346,8 +346,27 @@ - (NSMutableArray *)getTemporaryMessages
- (void)updateHistoryInBackgroundWithCompletionBlock:(UpdateHistoryInBackgroundCompletionBlock)block
{
NCChatBlock *lastChatBlock = [self chatBlocksForRoom].lastObject;
__block BOOL expired = NO;

BGTaskHelper *bgTask = [BGTaskHelper startBackgroundTaskWithName:@"updateHistoryInBackgroundWithCompletionBlock" expirationHandler:^(BGTaskHelper *task) {
[NCUtils log:@"ExpirationHandler called updateHistoryInBackgroundWithCompletionBlock"];
expired = YES;

// Make sure we actually end a running pullMessagesTask, because otherwise the completion handler might not be called in time
[self->_pullMessagesTask cancel];
}];

_pullMessagesTask = [[NCAPIController sharedInstance] receiveChatMessagesOfRoom:_room.token fromLastMessageId:lastChatBlock.newestMessageId history:NO includeLastMessage:NO timeout:NO lastCommonReadMessage:_room.lastCommonReadMessage setReadMarker:NO markNotificationsAsRead:NO forAccount:_account withCompletionBlock:^(NSArray *messages, NSInteger lastKnownMessage, NSInteger lastCommonReadMessage, NSError *error, NSInteger statusCode) {
if (expired) {
if (block) {
block(error);
}

[bgTask stopBackgroundTask];

return;
}

if (!self->_stopChatMessagesPoll) {
if (error) {
NSLog(@"Could not get background chat history. Error: %@", error.description);
Expand All @@ -366,6 +385,8 @@ - (void)updateHistoryInBackgroundWithCompletionBlock:(UpdateHistoryInBackgroundC
if (block) {
block(error);
}

[bgTask stopBackgroundTask];
}];
}

Expand Down
5 changes: 4 additions & 1 deletion NextcloudTalk/NCRoomsManager.m
Expand Up @@ -497,7 +497,10 @@ - (void)updateRoomsUpdatingUserStatus:(BOOL)updateStatus withCompletionBlock:(Up
NSMutableArray *roomsWithNewMessages = [NSMutableArray new];

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

RLMRealm *realm = [RLMRealm defaultRealm];
[realm transactionWithBlock:^{
Expand Down

0 comments on commit 424b8c9

Please sign in to comment.