-
-
Save verebes1/02950e46fff91456f2ad359b3f3ec3d9 to your computer and use it in GitHub Desktop.
import RealmSwift | |
import Realm | |
protocol CascadeDeleting { | |
func delete<S: Sequence>(_ objects: S, cascading: Bool) where S.Iterator.Element: Object | |
func delete<Entity: Object>(_ entity: Entity, cascading: Bool) | |
} | |
extension Realm: CascadeDeleting { | |
func delete<S: Sequence>(_ objects: S, cascading: Bool) where S.Iterator.Element: Object { | |
for obj in objects { | |
delete(obj, cascading: cascading) | |
} | |
} | |
func delete<Entity: Object>(_ entity: Entity, cascading: Bool) { | |
if cascading { | |
cascadeDelete(entity) | |
} else { | |
delete(entity) | |
} | |
} | |
} | |
private extension Realm { | |
private func cascadeDelete(_ entity: RLMObjectBase) { | |
guard let entity = entity as? Object else { return } | |
var toBeDeleted = Set<RLMObjectBase>() | |
toBeDeleted.insert(entity) | |
while !toBeDeleted.isEmpty { | |
guard let element = toBeDeleted.removeFirst() as? Object, | |
!element.isInvalidated else { continue } | |
resolve(element: element, toBeDeleted: &toBeDeleted) | |
} | |
} | |
private func resolve(element: Object, toBeDeleted: inout Set<RLMObjectBase>) { | |
element.objectSchema.properties.forEach { | |
guard let value = element.value(forKey: $0.name) else { return } | |
if let entity = value as? RLMObjectBase { | |
toBeDeleted.insert(entity) | |
} else if let list = value as? RLMSwiftCollectionBase { | |
for index in 0..<list._rlmCollection.count { | |
if let entity = list._rlmCollection.object(at: index) as? RLMObjectBase { | |
toBeDeleted.insert(entity) | |
} | |
} | |
} | |
} | |
delete(element) | |
} | |
} |
Hello, does anyone has a solution for this ? Just updated Realm to the latest version and now I get this message "No type named 'ListBase' in module 'RealmSwift'".
private func resolve(element: Object, toBeDeleted: inout Set<RLMObjectBase>) {
element.objectSchema.properties.forEach {
guard let value = element.value(forKey: $0.name) else { return }
if let entity = value as? RLMObjectBase {
toBeDeleted.insert(entity)
} else if let list = value as? RealmSwift.ListBase { /// <--- Error appears here !
for index in 0 ..< list._rlmArray.count {
if let realmObject = list._rlmArray.object(at: index) as? RLMObjectBase {
toBeDeleted.insert(realmObject)
}
}
}
}
delete(element)
}
Thank you !
faced same problem. Looking for solution for "No type named 'ListBase' in module 'RealmSwift'".
Probably won't fix all the situations, but you can solve some of the issue by using Embedded Objects.
Realm Uses Cascading Deletes for Embedded Objects
When you delete a Realm object, Realm automatically deletes any embedded objects referenced by that object. Any objects that your application must persist after the deletion of their parent object should use relationships instead.
I haven't used Realm for a while now. but I can point to the right place in Realm Docs
Two things come to my mind trying RealmSwift.List
instead RealmSwift.ListBase
And cleaning your derived data folder from Xcode.
Again these are only some ideas to go forward. I haven't tried them.
@jhoanarango @niralishaha25 as mentioned before, you can use Embedded Objects for automated cascade deleting. But if you still want to use the snippet on realm v10+, that should work as follows:
private func resolve(element: Object, toBeDeleted: inout Set<RLMObjectBase>) {
element.objectSchema.properties.forEach {
guard let value = element.value(forKey: $0.name) else { return }
if let entity = value as? RLMObjectBase {
toBeDeleted.insert(entity)
} else if let list = value as? RLMSwiftCollectionBase {
for index in 0..<list._rlmCollection.count {
if let entity = list._rlmCollection.object(at: index) as? RLMObjectBase {
toBeDeleted.insert(entity)
}
}
}
}
delete(element)
}
@aleyooop Thanks for your input. I've updated the Gist with your suggestion.
@stefanrenne and @verebes, because you are both using a
Set
fortoBeDeleted
, you are missing the objects which have the same hash.I modified your solution to work with Objective-C Realm and changed it to use an
Array
instead ofSet
.https://gist.github.com/georgescumihai/5c7e93322c59e6808c671bc65beaa221
Thanks for the source.
Example
This is true for objects without a primary key.