@@ -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