Skip to content
This repository has been archived by the owner on Feb 8, 2022. It is now read-only.

Relationships mirroring #57

Merged
merged 2 commits into from
Jan 8, 2016
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
38 changes: 37 additions & 1 deletion Source/ObjectExtension.swift
Original file line number Diff line number Diff line change
Expand Up @@ -47,4 +47,40 @@ extension Object {
let primaryKeyValue = (self as Object).valueForKey(primaryKey) else { return nil }
return String(self.dynamicType) + "-" + String(primaryKeyValue)
}
}


/**
Create a mirror of an object <T: Object>.
This mirror is not added to any Raelm so it is
"thread safe" as long as you don't try to access
any relationship from a background thread

If you want safely access relationships, you have to override
this method in your subclass for creating mirrors for relationships on your own,
like the code below:

extension Product {
override func getMirror() -> Product {
let clone = super.getMirror() as! Product
clone.category = self.category?.getMirror() as? Category
return clone
}
}

- parameter object Original object (T) to mirror

- returns a copy of the original object (T) but not included in any realm
*/
public func getMirror() -> Object {
let newObject = self.dynamicType.init()
let mirror = Mirror(reflecting: self)
for c in mirror.children.enumerate() {
guard let key = c.1.0
where !key.hasSuffix(".storage") else { continue }
let value = self.valueForKey(key)
guard let v = value else { continue }
(newObject as Object).setValue(v, forKey: key)
}
return newObject
}
}
24 changes: 0 additions & 24 deletions Source/RealmExtension.swift
Original file line number Diff line number Diff line change
Expand Up @@ -185,27 +185,3 @@ extension Results {
return array
}
}

/**
Create a mirror of an object <T: Object>.
This mirror is not added to any Raelm so it is
"thread safe" as long as you don't try to access
any relationship from a background thread

- parameter object Original object (T) to mirror

- returns a copy of the original object (T) but not included in any realm
*/
func getMirror<T: Object>(object: T) -> T {
let newObject = (object as Object).dynamicType.init()
let mirror = Mirror(reflecting: object)
for c in mirror.children.enumerate() {
guard let key = c.1.0
where !key.hasSuffix(".storage") else { continue }
let value = (object as Object).valueForKey(key)
guard let v = value else { continue }
(newObject as Object).setValue(v, forKey: key)
}
return newObject as! T
}

2 changes: 1 addition & 1 deletion Source/RealmLogger.swift
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ class RealmLogger {
- parameter action Action that was performed on that object
*/
func addObject<T: Object>(object: T, action: RealmAction) {
let realmChange = RealmChange(type: (object as Object).dynamicType, action: action, mirror: getMirror(object))
let realmChange = RealmChange(type: (object as Object).dynamicType, action: action, mirror: object.getMirror())
temporary.append(realmChange)
}

Expand Down
2 changes: 1 addition & 1 deletion Source/RealmResultsController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -184,7 +184,7 @@ public class RealmResultsController<T: Object, U> : RealmResultsCacheDelegate {
*/
public func performFetch() {
populating = true
var objects = self.request.execute().toArray().map(getMirror)
var objects = self.request.execute().toArray().map{ $0.getMirror() as! T }
if let filter = filter {
objects = objects.filter(filter)
}
Expand Down