Skip to content

Commit

Permalink
[primary-key-fix] updated code to match latest Crust, fixed issue wit…
Browse files Browse the repository at this point in the history
…h writing to primary key without update
  • Loading branch information
rexmas committed Feb 11, 2016
1 parent a947355 commit 51e3c34
Show file tree
Hide file tree
Showing 6 changed files with 23 additions and 17 deletions.
Binary file modified .DS_Store
Binary file not shown.
16 changes: 11 additions & 5 deletions RealmCrust/RealmMappings.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand All @@ -58,7 +59,7 @@ public class RealmAdaptor {
}
}

public func fetchObjectsWithType(type: BaseType.Type, keyValues: Dictionary<String, CVarArgType>) -> ResultsType {
public func fetchObjectsWithType(type: BaseType.Type, keyValues: Dictionary<String, CVarArgType>) -> ResultsType? {

var predicates = Array<NSPredicate>()
for (key, value) in keyValues {
Expand All @@ -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)
}
Expand All @@ -87,7 +93,7 @@ public class RealmAdaptor {
//}
//extension RealmAdaptor : Adaptor { }

//public func <- <T: Mappable, U: Mapping, C: MappingContext where U.MappedObject == T>(field: List<T>, map:(key: KeyExtensions<U>, context: C)) -> C {
//public func <- <T, U: Mapping, C: MappingContext where U.MappedObject == T>(field: List<T>, map:(key: KeyExtensions<U>, 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
Expand Down
14 changes: 8 additions & 6 deletions RealmCrustTests/Company.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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<CRMappingKey> {
return [ "uuid" ]
public var primaryKeys: Dictionary<String, CRMappingKey>? {
return [ "uuid" : "data.uuid" ]
}

public required init(adaptor: RealmAdaptor) {
Expand All @@ -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" >*<
Expand All @@ -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" >*<
Expand Down
2 changes: 1 addition & 1 deletion RealmCrustTests/CompanyStub.swift
Original file line number Diff line number Diff line change
Expand Up @@ -27,11 +27,11 @@ class CompanyStub {
func generateJsonObject() -> Dictionary<String, AnyObject> {
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
]
Expand Down
6 changes: 2 additions & 4 deletions RealmCrustTests/Employee.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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<CRMappingKey> {
return [ "uuid" ]
public var primaryKeys: Dictionary<String, CRMappingKey>? {
return [ "uuid" : "uuid" ]
}

public required init(adaptor: RealmAdaptor) {
Expand Down
2 changes: 1 addition & 1 deletion RealmCrustTests/RealmMappingTest.swift
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ public protocol RealmMapping : Mapping {
}
extension RealmAdaptor : Adaptor { }

public func <- <T: Mappable, U: Mapping, C: MappingContext where U.MappedObject == T>(field: List<T>, map:(key: KeyExtensions<U>, context: C)) -> C {
public func <- <T, U: Mapping, C: MappingContext where U.MappedObject == T>(field: List<T>, map:(key: KeyExtensions<U>, 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
Expand Down

0 comments on commit 51e3c34

Please sign in to comment.