@@ -18,7 +18,23 @@ class DatabaseConnector {
1818
1919 private init ( ) {
2020 // Initialize Realm instance
21- realm = try ! Realm ( )
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+ }
36+
37+ realm = try ! Realm ( configuration: config)
2238 if let realmURL = realm. configuration. fileURL {
2339 print ( " Realm Database Path: \( realmURL. path) " )
2440 }
@@ -47,8 +63,6 @@ class DatabaseConnector {
4763 return !way. tags. isEmpty && !( way. tags [ " ext:link " ] == " true " )
4864 }
4965
50- let dateFormatter = DateFormatter ( )
51- dateFormatter. dateFormat = " yyyy-MM-dd HH:mm:ss "
5266 do {
5367 try realm. write {
5468 for node in nodes {
@@ -59,9 +73,8 @@ class DatabaseConnector {
5973 storedElement. tags [ key] = value
6074 }
6175// if let meta = node {
62- let timestampString = dateFormatter. string ( from: node. timestamp)
6376 storedElement. version = node. version
64- storedElement. timestamp = timestampString
77+ storedElement. timestamp = node . timestamp
6578// }
6679 if let asNode = node as? OSMNode {
6780 // coordinate from lat long
@@ -79,19 +92,18 @@ class DatabaseConnector {
7992 for way in ways {
8093 let storedWay = StoredWay ( )
8194 storedWay. id = Int64 ( way. id)
82- let timestampString = dateFormatter. string ( from: way. timestamp)
8395 storedWay. tags. removeAll ( )
8496 way. tags. forEach { key, value in
8597 if !key. contains ( " . " ) {
8698 storedWay. tags [ key] = value
8799 }
88100 }
89101 storedWay. version = way. version
90- storedWay. timestamp = timestampString
102+ storedWay. timestamp = way . timestamp
91103 if let asWay = way as? OSMWay {
92104 storedWay. nodes. append ( objectsIn: asWay. nodes. map ( { Int64 ( $0) } ) )
93105 // Get all the points for the p
94- var nodesInWay = List < CLLocationCoordinate2D > ( )
106+ let nodesInWay = List < CLLocationCoordinate2D > ( )
95107 asWay. nodes. forEach { nodeId in
96108 if let actualNode = nodesDict [ String ( nodeId) ] {
97109 nodesInWay. append ( CLLocationCoordinate2D ( latitude: actualNode. lat, longitude: actualNode. lon) )
@@ -174,13 +186,19 @@ class DatabaseConnector {
174186 */
175187 func getNodes( ) -> Results < StoredNode > {
176188 return realm. objects ( StoredNode . self)
189+
190+ func getNodes( _ predicate: NSPredicate ) -> Results < StoredNode > {
191+ return realm. objects ( StoredNode . self) . filter ( predicate)
177192 }
178193 /**
179194 Fetches all the storedWays in the Database
180195 @returns a Results object containing StoredWay
181196 */
182197 func getWays( ) -> Results < StoredWay > {
183198 return realm. objects ( StoredWay . self)
199+
200+ func getWays( _ predicate: NSPredicate ) -> Results < StoredWay > {
201+ return realm. objects ( StoredWay . self) . filter ( predicate)
184202 }
185203 /**
186204 Fetches the center of a given StoredWay
0 commit comments