@@ -13,31 +13,9 @@ import osmparser
1313
1414class DatabaseConnector {
1515 static let shared = DatabaseConnector ( )
16-
17- let realm : Realm
18-
16+
1917 private init ( ) {
20- // Initialize Realm instance
21- let config = Realm . Configuration ( schemaVersion: 1 ) { migration, oldSchemaVersion in
22- if oldSchemaVersion < 1 {
23- let formatter = DateFormatter ( )
24- formatter. dateFormat = " yyyy-MM-dd HH:mm:ss "
25- migration. enumerateObjects ( ofType: StoredNode . className ( ) ) { oldObject, newObject in
26- if let oldTimestamp = oldObject ? [ " timestamp " ] as? String {
27- if let date = formatter. date ( from: oldTimestamp) {
28- newObject ? [ " timestamp " ] = date
29- } else {
30- newObject ? [ " timestamp " ] = Date ( )
31- }
32- }
33- }
34- }
35- }
3618
37- realm = try ! Realm ( configuration: config)
38- if let realmURL = realm. configuration. fileURL {
39- print ( " Realm Database Path: \( realmURL. path) " )
40- }
4119 }
4220
4321 /**
@@ -48,6 +26,7 @@ class DatabaseConnector {
4826 func saveOSMElements( _ elements: [ OSMElement ] ) {
4927 // Save the elements appropriately
5028 // Get the ways and nodes out
29+ let realm = try ! Realm ( configuration: RealmConfig . configuration)
5130 let nodes = elements. filter ( { $0 is OSMNode } )
5231 let nodesOnly = elements. filter ( { $0 is OSMNode } )
5332 // Create a dictionary out of this like [id : osmnode]
@@ -131,6 +110,7 @@ class DatabaseConnector {
131110
132111 func clearDB( ) {
133112 do {
113+ let realm = try Realm ( configuration: RealmConfig . configuration)
134114 try realm. write {
135115 realm. deleteAll ( )
136116 }
@@ -143,6 +123,7 @@ class DatabaseConnector {
143123
144124 func saveElements( _ elements: [ OSMWay ] ) {
145125 do {
126+ let realm = try Realm ( configuration: RealmConfig . configuration)
146127 try realm. write {
147128 for element in elements {
148129 let realmElement = RealmOPElement ( )
@@ -186,6 +167,7 @@ class DatabaseConnector {
186167 */
187168
188169 func getNodes( _ predicate: NSPredicate ) -> Results < StoredNode > {
170+ let realm = try ! Realm ( configuration: RealmConfig . configuration)
189171 return realm. objects ( StoredNode . self) . filter ( predicate)
190172 }
191173 /**
@@ -194,6 +176,7 @@ class DatabaseConnector {
194176 */
195177
196178 func getWays( _ predicate: NSPredicate ) -> Results < StoredWay > {
179+ let realm = try ! Realm ( configuration: RealmConfig . configuration)
197180 return realm. objects ( StoredWay . self) . filter ( predicate)
198181 }
199182 /**
@@ -203,6 +186,7 @@ class DatabaseConnector {
203186 */
204187 func getCenterForWay( id: Int64 ) -> CLLocationCoordinate2D ? {
205188 // Get all the objects for the way
189+ let realm = try ! Realm ( configuration: RealmConfig . configuration)
206190 guard let way = realm. object ( ofType: StoredWay . self, forPrimaryKey: id) else {
207191 return nil
208192 }
@@ -229,6 +213,7 @@ class DatabaseConnector {
229213 @return StoredNode
230214 */
231215 func getNode( id: Int ) -> StoredNode ? {
216+ let realm = try ! Realm ( configuration: RealmConfig . configuration)
232217 return realm. object ( ofType: StoredNode . self, forPrimaryKey: id)
233218 }
234219 /**
@@ -241,6 +226,7 @@ class DatabaseConnector {
241226// }
242227
243228 func getWay( id: Int ) -> StoredWay ? {
229+ let realm = try ! Realm ( configuration: RealmConfig . configuration)
244230 return realm. object ( ofType: StoredWay . self, forPrimaryKey: id)
245231 }
246232
@@ -257,7 +243,7 @@ class DatabaseConnector {
257243 @return `StoredWay`
258244 */
259245 func addWayTags( id: Int , tags: [ String : String ] , version: Int ) -> StoredWay ? {
260-
246+ let realm = try ! Realm ( configuration : RealmConfig . configuration )
261247 // Step 1: Try to get the editable copy first
262248 if let editable = getWay ( id: id) {
263249 // Step 2: Update the existing editable copy
@@ -284,7 +270,7 @@ class DatabaseConnector {
284270 */
285271 func addNodeTags( id: Int , tags: [ String : String ] , version: Int ) -> StoredNode ? {
286272 print ( " 🟣 addNodeTags called for id: \( id) with tags: \( tags) " )
287-
273+ let realm = try ! Realm ( configuration : RealmConfig . configuration )
288274 if let editable = getNode ( id: id) {
289275 print ( " ✏️ Editable node exists: \( editable. id) " )
290276 do {
@@ -315,6 +301,7 @@ class DatabaseConnector {
315301 - Returns: An instance of `StoredChangeset`
316302 */
317303 func createChangeset( id: Int , type: StoredElementEnum , originalTags: [ String : String ] , tags: [ String : String ] , version: Int , point: CLLocationCoordinate2D ? = nil , nodes: List < Int64 > ? = nil ) -> StoredChangeset ? {
304+ let realm = try ! Realm ( configuration: RealmConfig . configuration)
318305 let storedChangeset = StoredChangeset ( )
319306 storedChangeset. elementId = id
320307 storedChangeset. elementType = type
@@ -350,6 +337,7 @@ class DatabaseConnector {
350337 /// - Returns: an instance of `Results<StoredChangeset>`
351338 func getChangesets( synced: Bool = false ) -> Results < StoredChangeset > {
352339 let results : Results < StoredChangeset >
340+ let realm = try ! Realm ( configuration: RealmConfig . configuration)
353341 if synced {
354342 results = realm. objects ( StoredChangeset . self) . where { $0. changesetId != - 1 }
355343 } else {
@@ -360,6 +348,7 @@ class DatabaseConnector {
360348 }
361349
362350 func getChangeset( for id: String ) -> StoredChangeset ? {
351+ let realm = try ! Realm ( configuration: RealmConfig . configuration)
363352 return realm. object ( ofType: StoredChangeset . self, forPrimaryKey: id)
364353 }
365354
@@ -368,6 +357,7 @@ class DatabaseConnector {
368357 /// - parameter changesetId: Assigned changeset ID from the server
369358 /// - Returns updated `StoredChangeset`
370359 func assignChangesetId( obj: String , changesetId: Int , updatedVersion: Int ) -> StoredChangeset ? {
360+ let realm = try ! Realm ( configuration: RealmConfig . configuration)
371361 guard let changeset = realm. object ( ofType: StoredChangeset . self, forPrimaryKey: obj) else {
372362 return nil
373363 }
@@ -383,6 +373,7 @@ class DatabaseConnector {
383373 }
384374
385375 func updateChangesetWithUndoResultSuccess( obj: String ) -> StoredChangeset ? {
376+ let realm = try ! Realm ( configuration: RealmConfig . configuration)
386377 guard let changeset = realm. object ( ofType: StoredChangeset . self, forPrimaryKey: obj) else {
387378 return nil
388379 }
@@ -398,6 +389,7 @@ class DatabaseConnector {
398389 }
399390
400391 func updateNodeVersion( nodeId: String , version: Int ) -> StoredNode ? {
392+ let realm = try ! Realm ( configuration: RealmConfig . configuration)
401393 let intId = Int ( nodeId) ?? - 1
402394 guard let theNode = getNode ( id: intId) else { return nil }
403395 do {
@@ -413,6 +405,7 @@ class DatabaseConnector {
413405 }
414406
415407 func updateWayVersion( wayId: String , version: Int ) -> StoredWay ? {
408+ let realm = try ! Realm ( configuration: RealmConfig . configuration)
416409 let intId = Int ( wayId) ?? - 1
417410 guard let theWay = getWay ( id: intId) else { return nil }
418411
@@ -429,3 +422,21 @@ class DatabaseConnector {
429422 }
430423
431424}
425+
426+ struct RealmConfig {
427+ static let configuration = Realm . Configuration ( schemaVersion: 1 ) { migration, oldSchemaVersion in
428+ if oldSchemaVersion < 1 {
429+ let formatter = DateFormatter ( )
430+ formatter. dateFormat = " yyyy-MM-dd HH:mm:ss "
431+ migration. enumerateObjects ( ofType: StoredNode . className ( ) ) { oldObject, newObject in
432+ if let oldTimestamp = oldObject ? [ " timestamp " ] as? String {
433+ if let date = formatter. date ( from: oldTimestamp) {
434+ newObject ? [ " timestamp " ] = date
435+ } else {
436+ newObject ? [ " timestamp " ] = Date ( )
437+ }
438+ }
439+ }
440+ }
441+ }
442+ }
0 commit comments