Merge pull request #2215 from AlphaWallet/fix-token-balance-refresh-in-wallet-tab-too-slow

Fix: token balance refreshing in wallet tab is way too slow
pull/2217/head
James Sangalli 4 years ago committed by GitHub
commit 697e784df4
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
  1. 7
      AlphaWallet/Tokens/Types/TokensDataStore.swift
  2. 22
      AlphaWallet/Tokens/ViewControllers/TokensViewController.swift
  3. 22
      AlphaWallet/Transactions/ViewControllers/TransactionsViewController.swift

@ -486,12 +486,7 @@ class TokensDataStore {
} }
} }
for tokenObject in tokens { for tokenObject in tokens {
//We don't want a whole lot of RPC calls to go out at once. If the user has 100 ERC20 tokens, that's 100 `balanceOf`. iOS doesn't lke it and will return this error: refreshBalance(forToken: tokenObject, completion: incrementCountAndUpdateDelegate)
//Error Domain=NSPOSIXErrorDomain Code=28 "No space left on device" UserInfo={_kCFStreamErrorCodeKey=28, _kCFStreamErrorDomainKey=1}
let delay = TimeInterval.random(in: 0...20)
DispatchQueue.main.asyncAfter(deadline: .now() + delay) {
self.refreshBalance(forToken: tokenObject, completion: incrementCountAndUpdateDelegate)
}
} }
} }

@ -26,7 +26,6 @@ class TokensViewController: UIViewController {
private let assetDefinitionStore: AssetDefinitionStore private let assetDefinitionStore: AssetDefinitionStore
private let eventsDataStore: EventsDataStoreProtocol private let eventsDataStore: EventsDataStoreProtocol
private let sections: [Section] = Section.allCases private let sections: [Section] = Section.allCases
private var timeOfLastFetchBecauseViewAppears: Date?
private var viewModel: TokensViewModel { private var viewModel: TokensViewModel {
didSet { didSet {
@ -227,7 +226,7 @@ class TokensViewController: UIViewController {
navigationController?.navigationBar.prefersLargeTitles = true navigationController?.navigationBar.prefersLargeTitles = true
hidesBottomBarWhenPushed = false hidesBottomBarWhenPushed = false
fetchWithThrottling() fetch()
fixNavigationBarAndStatusBarBackgroundColorForiOS13Dot1() fixNavigationBarAndStatusBarBackgroundColorForiOS13Dot1()
keyboardChecker.viewWillAppear() keyboardChecker.viewWillAppear()
} }
@ -244,7 +243,7 @@ class TokensViewController: UIViewController {
@objc func pullToRefresh() { @objc func pullToRefresh() {
tableViewRefreshControl.beginRefreshing() tableViewRefreshControl.beginRefreshing()
collectiblesCollectionViewRefreshControl.beginRefreshing() collectiblesCollectionViewRefreshControl.beginRefreshing()
fetchWithThrottling() fetch()
} }
@objc func openConsole() { @objc func openConsole() {
@ -256,23 +255,6 @@ class TokensViewController: UIViewController {
tokenCollection.fetch() tokenCollection.fetch()
} }
//To reduce chance of this error occurring:
//Error Domain=NSPOSIXErrorDomain Code=28 "No space left on device" UserInfo={_kCFStreamErrorCodeKey=28, _kCFStreamErrorDomainKey=1}
private func fetchWithThrottling() {
let ttl: TimeInterval = 60 * 5
if let timeOfLastFetchBecauseViewAppears = timeOfLastFetchBecauseViewAppears {
if Date().timeIntervalSince(timeOfLastFetchBecauseViewAppears) < ttl {
//no-op
} else {
fetch()
self.timeOfLastFetchBecauseViewAppears = Date()
}
} else {
fetch()
timeOfLastFetchBecauseViewAppears = Date()
}
}
override func viewDidLayoutSubviews() { override func viewDidLayoutSubviews() {
//viewDidLayoutSubviews() is called many times //viewDidLayoutSubviews() is called many times
configureSearchBarOnce() configureSearchBarOnce()

@ -16,7 +16,6 @@ class TransactionsViewController: UIViewController {
private let refreshControl = UIRefreshControl() private let refreshControl = UIRefreshControl()
private let dataCoordinator: TransactionDataCoordinator private let dataCoordinator: TransactionDataCoordinator
private let sessions: ServerDictionary<WalletSession> private let sessions: ServerDictionary<WalletSession>
private var timeOfLastFetchBecauseViewAppears: Date?
var paymentType: PaymentFlow? var paymentType: PaymentFlow?
weak var delegate: TransactionsViewControllerDelegate? weak var delegate: TransactionsViewControllerDelegate?
@ -75,12 +74,12 @@ class TransactionsViewController: UIViewController {
override func viewWillAppear(_ animated: Bool) { override func viewWillAppear(_ animated: Bool) {
super.viewWillAppear(animated) super.viewWillAppear(animated)
fetchWithThrottling() fetch()
} }
@objc func pullToRefresh() { @objc func pullToRefresh() {
refreshControl.beginRefreshing() refreshControl.beginRefreshing()
fetchWithThrottling() fetch()
} }
func fetch() { func fetch() {
@ -93,23 +92,6 @@ class TransactionsViewController: UIViewController {
} }
} }
//To reduce chance of this error occurring:
//Error Domain=NSPOSIXErrorDomain Code=28 "No space left on device" UserInfo={_kCFStreamErrorCodeKey=28, _kCFStreamErrorDomainKey=1}
private func fetchWithThrottling() {
let ttl: TimeInterval = 60 * 5
if let timeOfLastFetchBecauseViewAppears = timeOfLastFetchBecauseViewAppears {
if Date().timeIntervalSince(timeOfLastFetchBecauseViewAppears) < ttl {
//no-op
} else {
fetch()
self.timeOfLastFetchBecauseViewAppears = Date()
}
} else {
fetch()
timeOfLastFetchBecauseViewAppears = Date()
}
}
func configure(viewModel: TransactionsViewModel) { func configure(viewModel: TransactionsViewModel) {
self.viewModel = viewModel self.viewModel = viewModel
} }

Loading…
Cancel
Save