Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
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)
}
}
@anfriis

This comment has been minimized.

Copy link

anfriis commented Sep 26, 2016

Looks nice with extension methods!

But I am having trouble compiling it with Swift 3 and RealmSwift 1.1.0, but I receive the following error in line 11 and 15:
"Extra argument 'cascade' in call"

Do you know what could be wrong?

@anfriis

This comment has been minimized.

Copy link

anfriis commented Nov 9, 2016

?

@fishfisher

This comment has been minimized.

Copy link

fishfisher commented Nov 9, 2016

@Anders123f: I was able to compile by changing from objects.toArray() to Array(objects)

@orangeince

This comment has been minimized.

Copy link

orangeince commented Dec 29, 2016

Hi
First, this extension looks very cool. Thanks for shared this
But, I found maybe you lost something.
If object is realmObject and CascadingDeletable, the object should be deleted too.
I add some code to the delete method:

 func delete<T: AnyObject>(_ object: T, cascade: Bool = true) where T: CascadingDeletable {
    ....
    if let object = object as? Object {
        delete(object)
    }
}
@AnthonyMDev

This comment has been minimized.

Copy link

AnthonyMDev commented Jan 4, 2017

When I call delete(_:) in Swift, it is being dispatched to the original delete(_:) function on Realm, not the cascading one from your extension. Not sure how to make this work...

@YoamFarges

This comment has been minimized.

Copy link

YoamFarges commented Jan 13, 2017

I would advise you not to use this solution.
Even after:

  • adapting the code to Swift 3,
  • fixing the algorithm with @orangeince answer
  • adding some function definitions for the delete(_:, cascade:) method to be called for Object instance not conforming to the protocol

I've found issues with the fact that the method will not be called recursively, as let cascadingChild = child as? T will not work if the child is not the same type as its parent.

Removing this check and just using the child as a regular Object won't work either, as the where clause is not recognized if we do not pass a type that conforms to the type in its definition.

@ndizazzo

This comment has been minimized.

Copy link

ndizazzo commented Jan 13, 2017

@AnthonyMDev - Realm provides initializers for an array of Realm Object<T>. The compiler can't infer the correct object type if you use .toArray().

func delete<T: AnyObject>(_ objects: List<T>, cascade: Bool = true) where T: CascadingDeletable {
    delete(Array(objects), cascade: cascade)
}

func delete<T: AnyObject>(_ objects: Results<T>, cascade: Bool = true) where T: CascadingDeletable {
    delete(Array(objects), cascade: cascade)
}
@Montrazul

This comment has been minimized.

Copy link

Montrazul commented Feb 10, 2017

I tested this code today but it does not fullfill the requirements of a full cascading delete.

All my model classes are impelmenting the CascadingDeletable protocoll and are returning all childs that should be deleted in the cascadingDeletions array. Lets say you have the following structure:

Object A
   Sub 1
   Sub 2
      SubSub 2.1
      SubSub 2.2
   Sub 3
   ...

Sub 1, Sub 2, Sub 3 are getting deleted but SubSub 2.1, SubSub 2.2 and the Object A itself 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.

@chrux

This comment has been minimized.

Copy link

chrux commented May 3, 2017

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

@anfriis

This comment has been minimized.

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
You can’t perform that action at this time.