@ -7,13 +7,9 @@ import SessionSnodeKit
import SessionMessagingKit
import SessionMessagingKit
import SessionUtilitiesKit
import SessionUtilitiesKit
@objc ( LKBackgroundPoller )
public final class BackgroundPoller {
public final class BackgroundPoller : NSObject {
private static var promises : [ Promise < Void > ] = [ ]
private static var promises : [ Promise < Void > ] = [ ]
private override init ( ) { }
@objc ( pollWithCompletionHandler : )
public static func poll ( completionHandler : @ escaping ( UIBackgroundFetchResult ) -> Void ) {
public static func poll ( completionHandler : @ escaping ( UIBackgroundFetchResult ) -> Void ) {
promises = [ ]
promises = [ ]
. appending ( pollForMessages ( ) )
. appending ( pollForMessages ( ) )
@ -40,12 +36,29 @@ public final class BackgroundPoller: NSObject {
}
}
)
)
// B a c k g r o u n d t a s k s w i l l a u t o m a t i c a l l y b e t e r m i n a t e d a f t e r 3 0 s e c o n d s ( w h i c h r e s u l t s i n a c r a s h
// a n d a p r o m p t t o a p p e a r f o r t h e u s e r ) w e w a n t t o a v o i d t h i s s o w e s t a r t a t i m e r w h i c h e x p i r e s
// a f t e r 2 5 s e c o n d s a l l o w i n g u s t o c a n c e l a l l p e n d i n g p r o m i s e s
let cancelTimer : Timer = Timer . scheduledTimerOnMainThread ( withTimeInterval : 25 , repeats : false ) { timer in
timer . invalidate ( )
guard promises . contains ( where : { ! $0 . isResolved } ) else { return }
SNLog ( " Background poll failed due to manual timeout " )
completionHandler ( . failed )
}
when ( resolved : promises )
when ( resolved : promises )
. done { _ in
. done { _ in
cancelTimer . invalidate ( )
completionHandler ( . newData )
completionHandler ( . newData )
}
}
. catch { error in
. catch { error in
// I f w e h a v e a l r e a d y i n v a l i d a t e d t h e t i m e r t h e n d o n o t h i n g ( w e e s s e n t i a l l y t i m e d o u t )
guard cancelTimer . isValid else { return }
SNLog ( " Background poll failed due to error: \( error ) " )
SNLog ( " Background poll failed due to error: \( error ) " )
cancelTimer . invalidate ( )
completionHandler ( . failed )
completionHandler ( . failed )
}
}
}
}
@ -74,7 +87,7 @@ public final class BackgroundPoller: NSObject {
ClosedGroupPoller . poll (
ClosedGroupPoller . poll (
groupPublicKey ,
groupPublicKey ,
on : DispatchQueue . main ,
on : DispatchQueue . main ,
maxRetryCount : 4 ,
maxRetryCount : 0 ,
isBackgroundPoll : true
isBackgroundPoll : true
)
)
}
}
@ -85,7 +98,6 @@ public final class BackgroundPoller: NSObject {
. then ( on : DispatchQueue . main ) { swarm -> Promise < Void > in
. then ( on : DispatchQueue . main ) { swarm -> Promise < Void > in
guard let snode = swarm . randomElement ( ) else { throw SnodeAPIError . generic }
guard let snode = swarm . randomElement ( ) else { throw SnodeAPIError . generic }
return attempt ( maxRetryCount : 4 , recoveringOn : DispatchQueue . main ) {
return SnodeAPI . getMessages ( from : snode , associatedWith : publicKey )
return SnodeAPI . getMessages ( from : snode , associatedWith : publicKey )
. then ( on : DispatchQueue . main ) { messages -> Promise < Void > in
. then ( on : DispatchQueue . main ) { messages -> Promise < Void > in
guard ! messages . isEmpty else { return Promise . value ( ( ) ) }
guard ! messages . isEmpty else { return Promise . value ( ( ) ) }
@ -159,4 +171,3 @@ public final class BackgroundPoller: NSObject {
}
}
}
}
}
}
}