Skip to content

Commit 0cc0b79

Browse files
committed
Fixed submitting multi selection quest form issue
- Implemented dispatch barrier and Semaphore to submit the each element in queue and sequentially. - This is may not be the ideal solution. It is a quick fix. If possible we need to implement in such a way that, open change set, submit all the quests async and close the change set. for this it requires structural changes
1 parent 3a8a42f commit 0cc0b79

File tree

2 files changed

+32
-21
lines changed

2 files changed

+32
-21
lines changed

GoInfoGame/GoInfoGame/UI/Map/MapViewModel.swift

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,7 @@ class MapViewModel: ObservableObject {
6565
}
6666

6767
for quest in self.selectedAnnotaions {
68+
print("Quest ID: \(quest.displayUnit.id) Tags: \(tags)")
6869
if let longElementQuest = quest.displayUnit.parent as? LongElementQuest {
6970
longElementQuest.onAnswer(answer: tags)
7071
}

GoInfoGame/GoInfoGame/data/DatasyncManager.swift

Lines changed: 31 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -15,36 +15,46 @@ class DatasyncManager {
1515
static let shared = DatasyncManager()
1616
private init() {}
1717

18-
private var isSynching: Bool = false
18+
// private var isSynching: Bool = false
1919

2020
private let dbInstance = DatabaseConnector.shared
21+
private let barrierQueue: DispatchQueue = DispatchQueue(label: "com.goinfogame.DatasyncManager.barrierQueue", attributes: .concurrent)
2122

2223
func syncDataToOSM(completionHandler: @escaping (Result<Bool, APIError>) -> Void) {
23-
Task {
24-
do {
25-
let isSynced = try await syncData()
26-
print("Sync finished")
27-
if isSynced {
28-
print("Sync successful")
29-
DispatchQueue.main.async {
30-
completionHandler(.success(true)) // Success
24+
barrierQueue.async(flags: .barrier) { [weak self] in
25+
guard let self = self else { return }
26+
let semaphore = DispatchSemaphore(value: 0)
27+
28+
Task.detached(priority: .userInitiated) {
29+
do {
30+
let isSynced = try await self.syncData()
31+
32+
print("Sync finished")
33+
if isSynced {
34+
print("Sync successful")
35+
DispatchQueue.main.async {
36+
completionHandler(.success(true)) // Success
37+
}
38+
} else {
39+
print("Sync failed")
40+
DispatchQueue.main.async {
41+
completionHandler(.failure(APIError.custom("Sync failed. Please try again."))) // Failure
42+
}
3143
}
32-
} else {
33-
print("Sync failed")
44+
} catch {
45+
print("Sync failed: \(error)")
3446
DispatchQueue.main.async {
35-
completionHandler(.failure(APIError.custom("Sync failed. Please try again."))) // Failure
47+
completionHandler(.failure(error as! APIError)) // Failure
3648
}
3749
}
38-
} catch {
39-
print("Sync failed: \(error)")
40-
DispatchQueue.main.async {
41-
completionHandler(.failure(error as! APIError)) // Failure
42-
}
50+
semaphore.signal()
4351
}
52+
53+
semaphore.wait()
4454
}
4555
}
4656

47-
/// *** Terminating app due to uncaught exception 'RLMException', reason: 'Realm accessed from incorrect thread.'
57+
/// *** Terminating app due to uncaught exception 'RLMException', reason: 'Realm accessed from incorrect thread.'
4858
/// To fix the above error added @mainActor
4959
@MainActor
5060
func syncData() async throws -> Bool {
@@ -58,7 +68,7 @@ class DatasyncManager {
5868
// }
5969

6070
let changesets = dbInstance.getChangesets()
61-
print("Starting to sync data")
71+
print("Starting to sync data changesets: \(changesets.count)")
6272

6373
var nodesToSync: [String: StoredNode] = [:]
6474
var waysToSync: [String: StoredWay] = [:]
@@ -113,7 +123,7 @@ class DatasyncManager {
113123
throw error
114124
}
115125
}
116-
isSynching = false
126+
// isSynching = false
117127
return syncSuccess
118128
}
119129

@@ -202,7 +212,7 @@ class DatasyncManager {
202212
let wayBody = changesetUploadBody.data(using: .utf8)
203213
let wayId = "\(localWay.id)"
204214
let newVersion = way.version + 1
205-
215+
print("Uploading changeset \(changesetUploadBody)")
206216
guard let accessToken = KeychainManager.load(key: "accessToken") else {
207217
throw NSError(domain: "No AccessToken", code: 0, userInfo: nil)
208218
}

0 commit comments

Comments
 (0)