From 33e1c24f58ebc4e2459a951245219c081dfe44a2 Mon Sep 17 00:00:00 2001 From: Hwee-Boon Yar Date: Wed, 9 May 2018 01:11:32 +0800 Subject: [PATCH] Fix: crash on iPhone X while fetching transactions in background. Part of upstream commit 61ec7a4405578e3042db64469cf85ca259b85f69 --- .../TransactionDataCoordinator.swift | 29 +++++++++++++++---- 1 file changed, 24 insertions(+), 5 deletions(-) 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() {