Skip to content

Commit 51a047e

Browse files
committed
optimised thread switchings
- preparing quest items async - removed performing update operation on main thread.
1 parent ac3c5c4 commit 51a047e

File tree

2 files changed

+14
-16
lines changed

2 files changed

+14
-16
lines changed

GoInfoGame/GoInfoGame/UI/Map/MapViewModel.swift

Lines changed: 13 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,6 @@ class MapViewModel: ObservableObject {
2525
@Published var region = MKCoordinateRegion()
2626
let viewSpanDelta = 0.005 // Delta lat/lng to show to the user
2727
// var userlocation = CLLocationCoordinate2D(latitude: 17.4700, longitude: 78.3534)
28-
@Published var userlocation: CLLocationCoordinate2D? = nil
2928
@Published var refreshMap = UUID()
3029
@Published var items: [DisplayUnitWithCoordinate] = []
3130
@Published var selectedQuest: DisplayUnit?
@@ -41,14 +40,13 @@ class MapViewModel: ObservableObject {
4140
locationManagerDelegate.locationUpdateHandler = { [weak self] location in
4241
guard let self = self else { return }
4342

44-
DispatchQueue.main.async {
45-
self.userlocation = location
43+
DispatchQueue.main.async { [unowned self] in
4644
self.region = MKCoordinateRegion(
4745
center: location,
4846
span: MKCoordinateSpan(latitudeDelta: self.viewSpanDelta, longitudeDelta: self.viewSpanDelta)
4947
)
50-
self.fetchOSMDataFor(from: .currentLocation(location: location))
5148
}
49+
self.fetchOSMDataFor(from: .currentLocation(location: location))
5250
}
5351

5452
locationManagerDelegate.requestLocationAuthorization()
@@ -115,24 +113,26 @@ class MapViewModel: ObservableObject {
115113
}
116114

117115
if let workspaceID = KeychainManager.load(key: "workspaceID") {
118-
116+
debugPrint("requested: start \(Date())")
119117
ApiManager.shared.performRequest(to: .fetchOSMElements(bBox.minLon, bBox.minLat, bBox.maxLon, bBox.maxLat, workspaceID), setupType: .osm, modelType: OSMMapDataResponse.self) { [unowned self] result in
120118
switch result {
121119
case .success(let success):
120+
debugPrint("response sucess: start \(Date())")
122121
let osmElements = success.getOSMElements()
123122
// print("OSM ELEMENTS ??? \(osmElements)")
124-
123+
debugPrint("saveOSMElements: start \(Date())")
125124
let response = Array(osmElements.values)
126-
let allValues = response
127-
128-
DispatchQueue.main.async { [unowned self, allValues] in
129-
self.dbInstance.saveOSMElements(allValues) // Save all where there are tags
130-
autoreleasepool { [unowned self] in
131-
self.items = AppQuestManager.shared.fetchQuestsFromDB()
132-
}
125+
self.dbInstance.saveOSMElements(response) // Save all where there are tags
126+
debugPrint("saveOSMElements: end \(Date())")
127+
debugPrint("fetchQuestsFromDB: start \(Date())")
128+
let items = AppQuestManager.shared.fetchQuestsFromDB()
129+
debugPrint("fetchQuestsFromDB: end \(Date())")
130+
DispatchQueue.main.async { [unowned self, items] in
131+
self.items = items
133132
self.isLoading = false
134133
if self.items.count == 0 {self.refreshMap = UUID()}
135134
}
135+
debugPrint("response sucess: end \(Date())")
136136
case .failure(let failure):
137137
print(failure)
138138
}

GoInfoGame/GoInfoGame/quests/QuestProtocols.swift

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -65,9 +65,7 @@ class QuestBase {
6565
print("❌ Undo failed: Element type not found")
6666
}
6767
print("undo result \(result)")
68-
DispatchQueue.main.async {
69-
_ = DatabaseConnector.shared.updateChangesetWithUndoResultSuccess(obj: changesetId)
70-
}
68+
_ = DatabaseConnector.shared.updateChangesetWithUndoResultSuccess(obj: changesetId)
7169
MapViewPublisher.shared.dismissSheet.send(.synced)
7270
// MapViewPublisher.shared.dismissSheet.send(.submitted(""))
7371
MapViewPublisher.shared.dismissSheet.send(.undoDone(changesetId))

0 commit comments

Comments
 (0)