Last active
June 14, 2017 15:46
-
-
Save gregpardo/93420647700d31e2a9b10ed7b309cf96 to your computer and use it in GitHub Desktop.
Realm+CascadingDeletions
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| import Foundation | |
| import RealmSwift | |
| protocol CascadingDeletable { | |
| var cascadingDeletions: [AnyObject?] { get } | |
| } | |
| extension Realm { | |
| func delete<T: AnyObject>(cascading: List<T>) where T: CascadingDeletable { | |
| let o = cascading | |
| cascading.removeAll() | |
| delete(o) | |
| } | |
| func delete<T: AnyObject>(cascading: Results<T>) where T: CascadingDeletable { | |
| let o = cascading | |
| delete(o) | |
| } | |
| func delete<T: AnyObject>(cascading: [T]) where T: CascadingDeletable { | |
| for c in cascading { | |
| delete(c as! Object) | |
| } | |
| } | |
| func delete<T: AnyObject>(cascading: T) where T: Object, T: CascadingDeletable { | |
| for child in cascading.cascadingDeletions { | |
| if let object = child as? Object { | |
| delete(object) | |
| } | |
| if let cascade = child as? CascadingDeletable { | |
| delete(cascade as! Object) | |
| } | |
| } | |
| delete(cascading) | |
| } | |
| } |
Same issue here, I haven't been able to remove the a List attribute.
Any solutions to this?
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
I tested this code today but it does not fullfill the requirements of a full cascading delete.
All my model classes are impelmenting the
CascadingDeletableprotocoll and are returning all childs that should be deleted in thecascadingDeletionsarray. Lets say you have the following structure:Sub 1,Sub 2,Sub 3are getting deleted butSubSub 2.1,SubSub 2.2and theObject Aitself are not getting deleted with current Realm 2.4.0 version and your extensions. Tested on XCode 8 with Swift 3.0 and Realm 2.4.0.