move pollers to background threads

pull/541/head
Ryan Zhao 4 years ago
parent a86310b0f5
commit 16c9b7793a

@ -89,14 +89,16 @@ public final class ClosedGroupPoller : NSObject {
SNLog("Next poll interval for closed group with public key: \(groupPublicKey) is \(nextPollInterval) s.") SNLog("Next poll interval for closed group with public key: \(groupPublicKey) is \(nextPollInterval) s.")
timers[groupPublicKey] = Timer.scheduledTimer(withTimeInterval: nextPollInterval, repeats: false) { [weak self] timer in timers[groupPublicKey] = Timer.scheduledTimer(withTimeInterval: nextPollInterval, repeats: false) { [weak self] timer in
timer.invalidate() timer.invalidate()
self?.poll(groupPublicKey).done2 { _ in Threading.closedGroupPollerQueue.async {
DispatchQueue.main.async { // Timers don't do well on background queues self?.poll(groupPublicKey).done2 { _ in
self?.pollRecursively(groupPublicKey) DispatchQueue.main.async { // Timers don't do well on background queues
} self?.pollRecursively(groupPublicKey)
}.catch2 { error in }
// The error is logged in poll(_:) }.catch2 { error in
DispatchQueue.main.async { // Timers don't do well on background queues // The error is logged in poll(_:)
self?.pollRecursively(groupPublicKey) DispatchQueue.main.async { // Timers don't do well on background queues
self?.pollRecursively(groupPublicKey)
}
} }
} }
} }

@ -23,11 +23,11 @@ public final class OpenGroupPollerV2 : NSObject {
guard let strongSelf = self else { return } guard let strongSelf = self else { return }
strongSelf.hasStarted = true strongSelf.hasStarted = true
strongSelf.timer = Timer.scheduledTimer(withTimeInterval: strongSelf.pollInterval, repeats: true) { _ in strongSelf.timer = Timer.scheduledTimer(withTimeInterval: strongSelf.pollInterval, repeats: true) { _ in
DispatchQueue.global().async { Threading.openGroupPollerQueue.async {
self?.poll().retainUntilComplete() self?.poll().retainUntilComplete()
} }
} }
DispatchQueue.global().async { Threading.openGroupPollerQueue.async {
strongSelf.poll().retainUntilComplete() strongSelf.poll().retainUntilComplete()
} }
} }

@ -55,7 +55,7 @@ public final class Poller : NSObject {
guard let strongSelf = self, strongSelf.isPolling else { return } guard let strongSelf = self, strongSelf.isPolling else { return }
Timer.scheduledTimer(withTimeInterval: Poller.retryInterval, repeats: false) { _ in Timer.scheduledTimer(withTimeInterval: Poller.retryInterval, repeats: false) { _ in
guard let strongSelf = self else { return } guard let strongSelf = self else { return }
DispatchQueue.global().async { Threading.pollerQueue.async {
strongSelf.setUpPolling() strongSelf.setUpPolling()
} }
} }

@ -3,4 +3,8 @@ import Foundation
internal enum Threading { internal enum Threading {
internal static let jobQueue = DispatchQueue(label: "SessionMessagingKit.jobQueue", qos: .userInitiated) internal static let jobQueue = DispatchQueue(label: "SessionMessagingKit.jobQueue", qos: .userInitiated)
internal static let pollerQueue = DispatchQueue(label: "SessionMessagingKit.pollerQueue")
internal static let closedGroupPollerQueue = DispatchQueue(label: "SessionMessagingKit.closedGroupPollerQueue")
internal static let openGroupPollerQueue = DispatchQueue(label: "SessionMessagingKit.openGroup")
} }

Loading…
Cancel
Save