From 51e3c3443c108165f621d6b23fbeaa07205186c3 Mon Sep 17 00:00:00 2001 From: rexmas Date: Wed, 10 Feb 2016 18:20:39 -0800 Subject: [PATCH] [primary-key-fix] updated code to match latest Crust, fixed issue with writing to primary key without update --- .DS_Store | Bin 6148 -> 6148 bytes RealmCrust/RealmMappings.swift | 16 +++++++++++----- RealmCrustTests/Company.swift | 14 ++++++++------ RealmCrustTests/CompanyStub.swift | 2 +- RealmCrustTests/Employee.swift | 6 ++---- RealmCrustTests/RealmMappingTest.swift | 2 +- 6 files changed, 23 insertions(+), 17 deletions(-) diff --git a/.DS_Store b/.DS_Store index ec0587963a952ae258a73e6c289ac15ce33b9f37..ede025b2f13cf15c3463c94a7e8b33194c46ebfc 100644 GIT binary patch delta 278 zcmZoMXfc=|#>B!ku~2NHo+2aH#(>?7iytsEv2rmmFmO!{WER=1!j#O&G-JzTUuGSF z+he{;t);k4(mE%+icLRz_K@8i0F1xjvor9kP=B)qu~2NHo+2a5#(>?7tSk%+3|x}~Sw%Lhup~1wO=+CW$Ywv;f&KW# lh6?7*>>T_YKpmR}IlePb<`;3~0IFtSWME*~93irX82~?s65s#; diff --git a/RealmCrust/RealmMappings.swift b/RealmCrust/RealmMappings.swift index e70dcbc..1428a5c 100644 --- a/RealmCrust/RealmMappings.swift +++ b/RealmCrust/RealmMappings.swift @@ -32,13 +32,14 @@ public class RealmAdaptor { public func createObject(objType: BaseType.Type) throws -> BaseType { let obj = objType.init() - try self.saveObjects([ obj ]) return obj } public func saveObjects(objects: [BaseType]) throws { let saveBlock = { - self.realm.add(objects) + for obj in objects { + self.realm.add(objects, update: obj.dynamicType.primaryKey() != nil) + } } if self.realm.inWriteTransaction { saveBlock() @@ -58,7 +59,7 @@ public class RealmAdaptor { } } - public func fetchObjectsWithType(type: BaseType.Type, keyValues: Dictionary) -> ResultsType { + public func fetchObjectsWithType(type: BaseType.Type, keyValues: Dictionary) -> ResultsType? { var predicates = Array() for (key, value) in keyValues { @@ -71,7 +72,12 @@ public class RealmAdaptor { return fetchObjectsWithType(type, predicate: andPredicate) } - public func fetchObjectsWithType(type: BaseType.Type, predicate: NSPredicate) -> ResultsType { + public func fetchObjectsWithType(type: BaseType.Type, predicate: NSPredicate) -> ResultsType? { + + if type.primaryKey() != nil { + // We're going to build an unstored object and update when saving based on the primary key. + return nil + } return realm.objects(type).filter(predicate) } @@ -87,7 +93,7 @@ public class RealmAdaptor { //} //extension RealmAdaptor : Adaptor { } -//public func <- (field: List, map:(key: KeyExtensions, context: C)) -> C { +//public func <- (field: List, map:(key: KeyExtensions, context: C)) -> C { // Realm specifies that List "must be declared with 'let'". Seems to actually work either way in practice, but for safety // we're going to include a List mapper that accepts fields with a 'let' declaration and forward to our diff --git a/RealmCrustTests/Company.swift b/RealmCrustTests/Company.swift index 4c647bc..3f7d945 100644 --- a/RealmCrustTests/Company.swift +++ b/RealmCrustTests/Company.swift @@ -10,15 +10,17 @@ public class Company: Object { public dynamic var foundingDate: NSDate = NSDate() public dynamic var founder: Employee? public dynamic var pendingLawsuits: Int = 0 + + public override class func primaryKey() -> String? { + return "uuid" + } } -extension Company: Mappable { } - public class CompanyMapping : RealmMapping { public var adaptor: RealmAdaptor - public var primaryKeys: Array { - return [ "uuid" ] + public var primaryKeys: Dictionary? { + return [ "uuid" : "data.uuid" ] } public required init(adaptor: RealmAdaptor) { @@ -30,7 +32,7 @@ public class CompanyMapping : RealmMapping { tomap.employees <- .Mapping("employees", employeeMapping) >*< tomap.founder <- .Mapping("founder", employeeMapping) >*< - tomap.uuid <- "uuid" >*< + tomap.uuid <- "data.uuid" >*< tomap.name <- "name" >*< tomap.foundingDate <- "data.founding_date" >*< tomap.pendingLawsuits <- "data.lawsuits.pending" >*< @@ -46,7 +48,7 @@ public class CompanyMappingWithDupes : CompanyMapping { tomap.employees <- .MappingOptions(mappingExtension, [ .AllowDuplicatesInCollection ]) >*< tomap.founder <- .Mapping("founder", employeeMapping) >*< - tomap.uuid <- "uuid" >*< + tomap.uuid <- "data.uuid" >*< tomap.name <- "name" >*< tomap.foundingDate <- "data.founding_date" >*< tomap.pendingLawsuits <- "data.lawsuits.pending" >*< diff --git a/RealmCrustTests/CompanyStub.swift b/RealmCrustTests/CompanyStub.swift index 2e4b135..0810abb 100644 --- a/RealmCrustTests/CompanyStub.swift +++ b/RealmCrustTests/CompanyStub.swift @@ -27,11 +27,11 @@ class CompanyStub { func generateJsonObject() -> Dictionary { let founder = self.founder?.generateJsonObject() return [ - "uuid" : uuid, "name" : name, "employees" : employees.map { $0.generateJsonObject() } as NSArray, "founder" : founder == nil ? NSNull() : founder! as NSDictionary, "data" : [ + "uuid" : uuid, "lawsuits" : [ "pending" : pendingLawsuits ] diff --git a/RealmCrustTests/Employee.swift b/RealmCrustTests/Employee.swift index 0c2ea7e..8b0aaf5 100644 --- a/RealmCrustTests/Employee.swift +++ b/RealmCrustTests/Employee.swift @@ -13,13 +13,11 @@ public class Employee: Object { public dynamic var percentYearlyRaise: Double = 0.0 } -extension Employee: Mappable { } - public class EmployeeMapping : RealmMapping { public var adaptor: RealmAdaptor - public var primaryKeys: Array { - return [ "uuid" ] + public var primaryKeys: Dictionary? { + return [ "uuid" : "uuid" ] } public required init(adaptor: RealmAdaptor) { diff --git a/RealmCrustTests/RealmMappingTest.swift b/RealmCrustTests/RealmMappingTest.swift index af43a1e..beb8989 100644 --- a/RealmCrustTests/RealmMappingTest.swift +++ b/RealmCrustTests/RealmMappingTest.swift @@ -13,7 +13,7 @@ public protocol RealmMapping : Mapping { } extension RealmAdaptor : Adaptor { } -public func <- (field: List, map:(key: KeyExtensions, context: C)) -> C { +public func <- (field: List, map:(key: KeyExtensions, context: C)) -> C { // Realm specifies that List "must be declared with 'let'". Seems to actually work either way in practice, but for safety // we're going to include a List mapper that accepts fields with a 'let' declaration and forward to our