Skip to content

Commit 70fd558

Browse files
author
Achyut Kumar M
committed
remove and add tags instead of replacing
1 parent 7de2263 commit 70fd558

File tree

2 files changed

+42
-8
lines changed

2 files changed

+42
-8
lines changed

GoInfoGame/GoInfoGame/DataBase/DatabaseConnector.swift

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -54,8 +54,9 @@ class DatabaseConnector {
5454
for node in nodes {
5555
let storedElement = StoredNode()
5656
storedElement.id = node.id
57-
for tag in node.tags {
58-
storedElement.tags.setValue(tag.value, forKey: tag.key)
57+
storedElement.tags.removeAll()
58+
node.tags.forEach { key, value in
59+
storedElement.tags[key] = value
5960
}
6061
// if let meta = node {
6162
let timestampString = dateFormatter.string(from: node.timestamp)
@@ -81,9 +82,10 @@ class DatabaseConnector {
8182
let storedWay = StoredWay()
8283
storedWay.id = way.id
8384
let timestampString = dateFormatter.string(from: way.timestamp)
84-
for tag in way.tags {
85-
if(!tag.key.contains(".")){ // Do a utility function
86-
storedWay.tags.setValue(tag.value, forKey: tag.key)
85+
storedWay.tags.removeAll()
86+
way.tags.forEach { key, value in
87+
if !key.contains(".") {
88+
storedWay.tags[key] = value
8789
}
8890
}
8991
storedWay.version = way.version

GoInfoGame/GoInfoGame/data/DatasyncManager.swift

Lines changed: 35 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -89,29 +89,40 @@ class DatasyncManager {
8989
do {
9090
let isFinished = try await syncNode(node: payload)
9191
if isFinished {
92+
// ✅ Mark changeset as submitted
9293
DispatchQueue.main.async {
9394
self.dbInstance.assignChangesetId(obj: key, changesetId: 0)
9495
}
96+
97+
// 🔄 Fetch updated node from OSM
98+
let updatedNode = try await fetchNode2(nodeId: "\(payload.id)")
99+
refreshOriginalNodeIfNewer(updatedNode)
100+
101+
print("✅ Sync finished for node: \(payload.id)")
95102
} else {
96103
syncSuccess = false
97104
return false
98105
}
99-
print("Sync finished for node: \(isFinished)")
100106
} catch {
101-
print("Failed to sync node: \(error.localizedDescription)")
107+
print("Failed to sync node: \(error.localizedDescription)")
102108
syncSuccess = false
103109
throw error
104110
}
105111
}
106112

113+
107114
for (key, way) in waysToSync {
108115
var payload = way.asOSMWay()
109116
do {
110117
let isFinished = try await syncWay(way: payload)
111118
if isFinished {
119+
// Update changeset locally
112120
DispatchQueue.main.async {
113121
self.dbInstance.assignChangesetId(obj: key, changesetId: 0)
114122
}
123+
124+
let updatedWay = try await fetchway2(wayId: "\(payload.id)")
125+
refreshOriginalWayIfNewer(updatedWay)
115126
} else {
116127
syncSuccess = false
117128
return false
@@ -122,12 +133,33 @@ class DatasyncManager {
122133
throw error
123134
}
124135
}
136+
125137
isSynching = false
126138
return syncSuccess
127139
}
128140

141+
func refreshOriginalWayIfNewer(_ newWay: OSMWay) {
142+
guard let existing = DatabaseConnector.shared.getWay(id: newWay.id, version: .original) else {
143+
DatabaseConnector.shared.saveOSMElements([newWay])
144+
return
145+
}
129146

147+
if newWay.version > existing.version {
148+
DatabaseConnector.shared.saveOSMElements([newWay])
149+
}
150+
}
130151

152+
func refreshOriginalNodeIfNewer(_ newNode: OSMNode) {
153+
guard let existing = DatabaseConnector.shared.getNode(id: newNode.id, version: .original) else {
154+
DatabaseConnector.shared.saveOSMElements([newNode])
155+
return
156+
}
157+
158+
if newNode.version > existing.version {
159+
DatabaseConnector.shared.saveOSMElements([newNode])
160+
}
161+
}
162+
131163
func syncDataDummy() async {
132164

133165
let changesets = dbInstance.getChangesets()
@@ -429,7 +461,7 @@ class DatasyncManager {
429461

430462
return try await withCheckedThrowingContinuation { continuation in
431463
ApiManager.shared.performRequest(
432-
to: .fetchLatestWay(workspaceID, nodeId),
464+
to: .fetchLatestNode(workspaceID, nodeId),
433465
setupType: .osm,
434466
modelType: OSMNodeResponse.self
435467
) { result in

0 commit comments

Comments
 (0)