diff --git a/Trust/Transactions/Coordinators/TransactionDataCoordinator.swift b/Trust/Transactions/Coordinators/TransactionDataCoordinator.swift index cb36a1355..92994e478 100644 --- a/Trust/Transactions/Coordinators/TransactionDataCoordinator.swift +++ b/Trust/Transactions/Coordinators/TransactionDataCoordinator.swift @@ -45,20 +45,39 @@ class TransactionDataCoordinator { ) { self.session = session self.storage = storage + NotificationCenter.default.addObserver(self, selector: #selector(stopTimers), name: .UIApplicationWillResignActive, object: nil) + NotificationCenter.default.addObserver(self, selector: #selector(restartTimers), name: .UIApplicationDidBecomeActive, object: nil) } func start() { + runScheduledTimers() + // Start fetching all transactions process. + if transactionsTracker.fetchingState != .done { + initialFetch(for: session.account.address, page: 0) { _ in } + } + } + + @objc func stopTimers() { + timer?.invalidate() + timer = nil + updateTransactionsTimer?.invalidate() + updateTransactionsTimer = nil + } + + @objc func restartTimers() { + runScheduledTimers() + } + + private func runScheduledTimers() { + guard timer == nil, updateTransactionsTimer == nil else { + return + } timer = Timer.scheduledTimer(timeInterval: 5, target: BlockOperation { [weak self] in self?.fetchPending() }, selector: #selector(Operation.main), userInfo: nil, repeats: true) updateTransactionsTimer = Timer.scheduledTimer(timeInterval: 15, target: BlockOperation { [weak self] in self?.fetchTransactions() }, selector: #selector(Operation.main), userInfo: nil, repeats: true) - - // Start fetching all transactions process. - if transactionsTracker.fetchingState != .done { - initialFetch(for: session.account.address, page: 0) { _ in } - } } func fetch() {