FIX: download operation gets cancelled when apps are backgrounded
This commit is contained in:
parent
c17132f694
commit
645ffa6734
|
@ -88,6 +88,8 @@ public class SDKSynchronizer: Synchronizer {
|
||||||
public private(set) var blockProcessor: CompactBlockProcessor?
|
public private(set) var blockProcessor: CompactBlockProcessor?
|
||||||
public private(set) var initializer: Initializer
|
public private(set) var initializer: Initializer
|
||||||
|
|
||||||
|
private var isSubscribedToAppDelegateEvents = false
|
||||||
|
|
||||||
private var transactionManager: OutboundTransactionManager
|
private var transactionManager: OutboundTransactionManager
|
||||||
private var transactionRepository: TransactionRepository
|
private var transactionRepository: TransactionRepository
|
||||||
var taskIdentifier: UIBackgroundTaskIdentifier = .invalid
|
var taskIdentifier: UIBackgroundTaskIdentifier = .invalid
|
||||||
|
@ -112,7 +114,6 @@ public class SDKSynchronizer: Synchronizer {
|
||||||
transactionManager: try OutboundTransactionManagerBuilder.build(initializer: initializer),
|
transactionManager: try OutboundTransactionManagerBuilder.build(initializer: initializer),
|
||||||
transactionRepository: initializer.transactionRepository)
|
transactionRepository: initializer.transactionRepository)
|
||||||
|
|
||||||
self.subscribeToAppDelegateNotifications()
|
|
||||||
}
|
}
|
||||||
|
|
||||||
init(status: Status,
|
init(status: Status,
|
||||||
|
@ -131,19 +132,32 @@ public class SDKSynchronizer: Synchronizer {
|
||||||
self.blockProcessor = nil
|
self.blockProcessor = nil
|
||||||
self.taskIdentifier = .invalid
|
self.taskIdentifier = .invalid
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private func lazyInitialize() throws {
|
||||||
|
guard self.blockProcessor == nil else { return }
|
||||||
|
guard let processor = initializer.blockProcessor() else {
|
||||||
|
throw SynchronizerError.generalError(message: "compact block processor initialization failed")
|
||||||
|
}
|
||||||
|
|
||||||
|
if !isSubscribedToAppDelegateEvents {
|
||||||
|
subscribeToAppDelegateNotifications()
|
||||||
|
}
|
||||||
|
subscribeToProcessorNotifications(processor)
|
||||||
|
|
||||||
|
self.blockProcessor = processor
|
||||||
|
}
|
||||||
/**
|
/**
|
||||||
Starts the synchronizer
|
Starts the synchronizer
|
||||||
- Throws: CompactBlockProcessorError when failures occur
|
- Throws: CompactBlockProcessorError when failures occur
|
||||||
*/
|
*/
|
||||||
public func start(retry: Bool = false) throws {
|
public func start(retry: Bool = false) throws {
|
||||||
|
|
||||||
guard let processor = initializer.blockProcessor() else {
|
try lazyInitialize()
|
||||||
|
|
||||||
|
guard let processor = self.blockProcessor else {
|
||||||
throw SynchronizerError.generalError(message: "compact block processor initialization failed")
|
throw SynchronizerError.generalError(message: "compact block processor initialization failed")
|
||||||
}
|
}
|
||||||
|
|
||||||
subscribeToProcessorNotifications(processor)
|
|
||||||
|
|
||||||
self.blockProcessor = processor
|
|
||||||
guard status == .stopped || status == .disconnected || status == .synced else {
|
guard status == .stopped || status == .disconnected || status == .synced else {
|
||||||
assert(true,"warning: synchronizer started when already started") // TODO: remove this assertion sometime in the near future
|
assert(true,"warning: synchronizer started when already started") // TODO: remove this assertion sometime in the near future
|
||||||
return
|
return
|
||||||
|
@ -154,7 +168,6 @@ public class SDKSynchronizer: Synchronizer {
|
||||||
} catch {
|
} catch {
|
||||||
throw mapError(error)
|
throw mapError(error)
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -174,9 +187,38 @@ public class SDKSynchronizer: Synchronizer {
|
||||||
}
|
}
|
||||||
|
|
||||||
// MARK: event subscription
|
// MARK: event subscription
|
||||||
|
|
||||||
|
private func subscribeFromAppDelegateNotifications() {
|
||||||
|
self.isSubscribedToAppDelegateEvents = true
|
||||||
|
|
||||||
|
let center = NotificationCenter.default
|
||||||
|
|
||||||
|
center.removeObserver(self,
|
||||||
|
name: UIApplication.didBecomeActiveNotification,
|
||||||
|
object: nil)
|
||||||
|
|
||||||
|
center.removeObserver(self,
|
||||||
|
name: UIApplication.willTerminateNotification,
|
||||||
|
object: nil)
|
||||||
|
|
||||||
|
center.removeObserver(self,
|
||||||
|
name: UIApplication.willResignActiveNotification,
|
||||||
|
object: nil)
|
||||||
|
|
||||||
|
center.removeObserver(self,
|
||||||
|
name: UIApplication.didEnterBackgroundNotification,
|
||||||
|
object: nil)
|
||||||
|
|
||||||
|
center.removeObserver(self,
|
||||||
|
name: UIApplication.willEnterForegroundNotification,
|
||||||
|
object: nil)
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
private func subscribeToAppDelegateNotifications() {
|
private func subscribeToAppDelegateNotifications() {
|
||||||
// todo: iOS 13 platform specific
|
// todo: iOS 13 platform specific
|
||||||
|
|
||||||
|
self.isSubscribedToAppDelegateEvents = true
|
||||||
let center = NotificationCenter.default
|
let center = NotificationCenter.default
|
||||||
|
|
||||||
center.addObserver(self,
|
center.addObserver(self,
|
||||||
|
@ -399,9 +441,10 @@ public class SDKSynchronizer: Synchronizer {
|
||||||
invalidateBackgroundActivity()
|
invalidateBackgroundActivity()
|
||||||
if status == .stopped || status == .disconnected {
|
if status == .stopped || status == .disconnected {
|
||||||
do {
|
do {
|
||||||
try start()
|
try start(retry: true)
|
||||||
} catch {
|
} catch {
|
||||||
self.status = .disconnected
|
self.status = .disconnected
|
||||||
|
self.notifyFailure(error)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue