Skip to content

Commit e6807b1

Browse files
committed
calculate cache asyncronously. Closes #274
1 parent b18f745 commit e6807b1

File tree

2 files changed

+22
-19
lines changed

2 files changed

+22
-19
lines changed

Diff for: OpenGpxTracker/PreferencesTableViewController.swift

+18-2
Original file line numberDiff line numberDiff line change
@@ -84,8 +84,24 @@ class PreferencesTableViewController: UITableViewController, UINavigationBarDele
8484
action: #selector(PreferencesTableViewController.closePreferencesTableViewController))
8585
self.navigationItem.rightBarButtonItems = [shareItem]
8686

87-
let fileSize = cache.diskCache.fileSize ?? 0
88-
cachedSize = Int(fileSize).asFileSize()
87+
//let fileSize = cache.diskCache.fileSize ?? 0
88+
//cachedSize = Int(fileSize).asFileSize()
89+
// Set a temporary value for cachedSize
90+
cachedSize = "Calculating..."
91+
print("PreferencesTableViewConroller: Starting cache calculation")
92+
// Perform the file size calculation asynchronously
93+
DispatchQueue.global(qos: .background).async {
94+
let fileSize = self.cache.diskCache.fileSize ?? 0
95+
self.cachedSize = Int(fileSize).asFileSize()
96+
97+
// Update the cachedSize on the main thread once the operation is complete
98+
DispatchQueue.main.async {
99+
print("PreferencesTableViewController: Completing cache calculation")
100+
if let cell = self.tableView.cellForRow(at: IndexPath(row: kUseOfflineCacheCell, section: kCacheSection)) {
101+
cell.detailTextLabel?.text = self.cachedSize
102+
}
103+
}
104+
}
89105
}
90106

91107
/// Close this controller.

Diff for: OpenGpxTracker/ViewController.swift

+4-17
Original file line numberDiff line numberDiff line change
@@ -977,23 +977,10 @@ class ViewController: UIViewController, UIGestureRecognizerDelegate {
977977
///
978978
@objc func openPreferencesTableViewController() {
979979
print("openPreferencesTableViewController")
980-
981-
// Show loading toast
982-
Toast.showLoading("Loading Preferences...")
983-
984-
// Perform all operations on the main thread
985-
DispatchQueue.main.async {
986-
// Simulate a delay for testing (remove in production)
987-
//Thread.sleep(forTimeInterval: 4.5)
988-
989-
let vc = PreferencesTableViewController(style: .grouped)
990-
vc.delegate = self
991-
let navController = UINavigationController(rootViewController: vc)
992-
993-
// Hide the loading toast and present the view controller
994-
Toast.hideLoading()
995-
self.present(navController, animated: true)
996-
}
980+
let vc = PreferencesTableViewController(style: .grouped)
981+
vc.delegate = self
982+
let navController = UINavigationController(rootViewController: vc)
983+
self.present(navController, animated: true)
997984
}
998985

999986
///

0 commit comments

Comments
 (0)