Merge pull request #1612 from vector-im/fix_bg_sync_loop

Bug Fix - Bg Sync loop
This commit is contained in:
giomfo
2017-10-23 17:03:52 +02:00
committed by GitHub
+25 -11
View File
@@ -192,7 +192,7 @@ NSString *const kAppDelegateNetworkStatusDidChangeNotification = @"kAppDelegateN
@property (nonatomic, strong) PKPushRegistry *pushRegistry;
@property (nonatomic) NSMutableArray <NSString *> *incomingPushEventIds;
@property (nonatomic) NSMutableDictionary <NSNumber *, NSMutableArray <NSString *> *> *incomingPushEventIds;
@end
@@ -376,7 +376,7 @@ NSString *const kAppDelegateNetworkStatusDidChangeNotification = @"kAppDelegateN
[self startGoogleAnalytics];
// Prepare Pushkit handling
_incomingPushEventIds = [NSMutableArray array];
_incomingPushEventIds = [NSMutableDictionary dictionary];
// Add matrix observers, and initialize matrix sessions if the app is not launched in background.
[self initMatrixSessions];
@@ -473,7 +473,10 @@ NSString *const kAppDelegateNetworkStatusDidChangeNotification = @"kAppDelegateN
NSLog(@"[AppDelegate] applicationWillEnterForeground");
// Flush all the pending push notifications.
[self.incomingPushEventIds removeAllObjects];
for (NSMutableArray *array in self.incomingPushEventIds)
{
[array removeAllObjects];
}
// Called as part of the transition from the background to the inactive state; here you can undo many of the changes made on entering the background.
@@ -1093,8 +1096,11 @@ NSString *const kAppDelegateNetworkStatusDidChangeNotification = @"kAppDelegateN
NSString *eventId = payload.dictionaryPayload[@"event_id"];
if (eventId)
{
// Store the event identifier.
[self.incomingPushEventIds addObject:eventId];
// Add this event identifier in the pending push array for each session.
for (NSMutableArray *array in self.incomingPushEventIds)
{
[array addObject:eventId];
}
}
else
{
@@ -1118,6 +1124,9 @@ NSString *const kAppDelegateNetworkStatusDidChangeNotification = @"kAppDelegateN
NSLog(@"[AppDelegate] launchBackgroundSync");
__weak typeof(self) weakSelf = self;
// Flush all the pending push notifications for this session.
[self.incomingPushEventIds[@(account.mxSession.hash)] removeAllObjects];
[account backgroundSync:20000 success:^{
// Sanity check
@@ -1886,9 +1895,10 @@ NSString *const kAppDelegateNetworkStatusDidChangeNotification = @"kAppDelegateN
}
else if (mxSession.state == MXSessionStatePaused)
{
// Check whether some push notifications are pending.
if (self.incomingPushEventIds.count)
// Check whether some push notifications are pending for this session.
if (self.incomingPushEventIds[@(mxSession.hash)].count)
{
NSLog(@"[AppDelegate] relaunch a background sync for the kMXSessionStateDidChangeNotificationpending incoming push");
[self launchBackgroundSync];
}
}
@@ -2046,6 +2056,9 @@ NSString *const kAppDelegateNetworkStatusDidChangeNotification = @"kAppDelegateN
// Do the one time check on device id
[self checkDeviceId:mxSession];
// Add an array to handle incoming push
self.incomingPushEventIds[@(mxSession.hash)] = [NSMutableArray array];
}
}
@@ -2072,6 +2085,8 @@ NSString *const kAppDelegateNetworkStatusDidChangeNotification = @"kAppDelegateN
[[NSNotificationCenter defaultCenter] removeObserver:matrixCallObserver];
matrixCallObserver = nil;
}
[self.incomingPushEventIds removeObjectForKey:@(mxSession.hash)];
}
- (void)markAllMessagesAsRead
@@ -2110,7 +2125,6 @@ NSString *const kAppDelegateNetworkStatusDidChangeNotification = @"kAppDelegateN
{
self.pushRegistry = nil;
isPushRegistered = NO;
[self.incomingPushEventIds removeAllObjects];
// Clear cache
[MXMediaManager clearCache];
@@ -2384,12 +2398,12 @@ NSString *const kAppDelegateNetworkStatusDidChangeNotification = @"kAppDelegateN
// Sanity check
if (event.eventId && event.roomId && rule)
{
// Check whether this event corresponds to a pending push.
NSUInteger index = [self.incomingPushEventIds indexOfObject:event.eventId];
// Check whether this event corresponds to a pending push for this session.
NSUInteger index = [self.incomingPushEventIds[@(mxSession.hash)] indexOfObject:event.eventId];
if (index != NSNotFound)
{
// Remove it from the pending list.
[self.incomingPushEventIds removeObjectAtIndex:index];
[self.incomingPushEventIds[@(mxSession.hash)] removeObjectAtIndex:index];
}
// Add it to the list of the events to notify.