Skip to content

Commit 822ff4c

Browse files
committed
incremeted realm version to 1 and modified time stamp filed in StoreNode and StoreWay from string to Date
- reduced conversion from string to Date and Date to String. it will improve the performance.
1 parent 7943508 commit 822ff4c

File tree

3 files changed

+28
-10
lines changed

3 files changed

+28
-10
lines changed

GoInfoGame/GoInfoGame/DataBase/DBModels/StoredNode.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ class StoredNode : Object {
1717
@Persisted(primaryKey: true) var id: Int64
1818
@Persisted var tags = Map<String,String>()
1919
@Persisted var version: Int = 0
20-
@Persisted var timestamp : String = ""
20+
@Persisted var timestamp : Date
2121
@Persisted var point: CLLocationCoordinate2D = CLLocationCoordinate2D(latitude: 0.0, longitude: 0.0)
2222

2323
// Give another method that gives node

GoInfoGame/GoInfoGame/DataBase/DBModels/StoredWay.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ class StoredWay: Object {
1818
@Persisted(primaryKey: true) var id: Int64
1919
@Persisted var tags = Map<String,String>()
2020
@Persisted var version: Int = 0
21-
@Persisted var timestamp : String = ""
21+
@Persisted var timestamp : Date
2222
@Persisted var nodes: List<Int64> = List<Int64>()
2323
// Need to persist the points
2424
@Persisted var polyline: List<CLLocationCoordinate2D> = List<CLLocationCoordinate2D>()

GoInfoGame/GoInfoGame/DataBase/DatabaseConnector.swift

Lines changed: 26 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -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

Comments
 (0)