Skip to content

Instantly share code, notes, and snippets.

@gregpardo
Last active June 14, 2017 15:46
Show Gist options
  • Save gregpardo/93420647700d31e2a9b10ed7b309cf96 to your computer and use it in GitHub Desktop.
Save gregpardo/93420647700d31e2a9b10ed7b309cf96 to your computer and use it in GitHub Desktop.
Realm+CascadingDeletions
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)
}
}
@chrux
Copy link

chrux commented May 3, 2017

Same issue here, I haven't been able to remove the a List attribute.

@anfriis
Copy link

anfriis commented May 24, 2017

Any solutions to this?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment