从 Realm 中的列表中删除对象 - Swift
Delete objects from List in Realm - Swift
我有一个集合视图,您可以在其中 select 删除多个单元格。这意味着如果删除了多个单元格,那么 Realm 中也应该删除多个对象 - 每个单元格 1 个对象。
我有一个函数,它接受一个 Int
的数组,该数组将从集合视图的 selected indexPaths 中填充。
问题是我不确定如何同时执行这两项操作
1)删除Realm中的对象,
2) 使 List
保持最新且没有删除的对象
我的代码是:
我得到的索引路径是这样的:
let indexPaths = collectionView.indexPathsForSelectedItems
这是我的函数,用于获取 indexPaths、更新 List
并删除 Realm 中的对象。它目前不工作,因为对象没有被删除。我注意到 removeAll
没有删除任何东西。
func removeVideos(at indexes: [Int]) {
let newVideos = List<Video>()
for (index, video) in favorite!.videos.enumerated() {
if !indexes.contains(index) {
newVideos.append(video)
}
}
let realm = try! Realm()
try! realm.write {
favorite!.videos.removeAll()
newVideos.forEach { newVideo in
favorite!.videos.append(newVideo)
}
}
}
我这样调用该函数:
removeVideos(at: indexPaths.map { [=17=].item })
有什么想法吗?
List.removeAll()
doesn't delete the objects from Realm. It just removes them out of that List
object, deleting their relationship to their parent object (in this case, the favorite
object). Deleting objects along with their parent List
object is a feature called 'cascading deletes' and it's still being discussed 领域 GitHub.
如果你真的想删除它们,那么只需调用realm.delete(favorite!.videos)
。这将从 Realm 中删除它们并自动清除 List
属性.
尽管如此,您可能需要谨慎实施。一旦 Object
从 Realm 中删除,任何现有的对它的引用都不能重新添加回 Realm。只删除 newVideo
对象本身而不是清除整个 List
.
可能是合适的
func removeVideos(at indexes: [Int]) {
let newVideos = [Video]()
for (index, video) in favorite!.videos.enumerated() {
if !indexes.contains(index) {
newVideos.append(video)
}
}
let realm = try! Realm()
try! realm.write {
realm.delete(newVideos)
}
}
只要您在集合视图上设置了 Realm 通知块,就可以将它们从 UI 中删除。
我有一个集合视图,您可以在其中 select 删除多个单元格。这意味着如果删除了多个单元格,那么 Realm 中也应该删除多个对象 - 每个单元格 1 个对象。
我有一个函数,它接受一个 Int
的数组,该数组将从集合视图的 selected indexPaths 中填充。
问题是我不确定如何同时执行这两项操作
1)删除Realm中的对象,
2) 使 List
保持最新且没有删除的对象
我的代码是:
我得到的索引路径是这样的:
let indexPaths = collectionView.indexPathsForSelectedItems
这是我的函数,用于获取 indexPaths、更新 List
并删除 Realm 中的对象。它目前不工作,因为对象没有被删除。我注意到 removeAll
没有删除任何东西。
func removeVideos(at indexes: [Int]) {
let newVideos = List<Video>()
for (index, video) in favorite!.videos.enumerated() {
if !indexes.contains(index) {
newVideos.append(video)
}
}
let realm = try! Realm()
try! realm.write {
favorite!.videos.removeAll()
newVideos.forEach { newVideo in
favorite!.videos.append(newVideo)
}
}
}
我这样调用该函数:
removeVideos(at: indexPaths.map { [=17=].item })
有什么想法吗?
List.removeAll()
doesn't delete the objects from Realm. It just removes them out of that List
object, deleting their relationship to their parent object (in this case, the favorite
object). Deleting objects along with their parent List
object is a feature called 'cascading deletes' and it's still being discussed 领域 GitHub.
如果你真的想删除它们,那么只需调用realm.delete(favorite!.videos)
。这将从 Realm 中删除它们并自动清除 List
属性.
尽管如此,您可能需要谨慎实施。一旦 Object
从 Realm 中删除,任何现有的对它的引用都不能重新添加回 Realm。只删除 newVideo
对象本身而不是清除整个 List
.
func removeVideos(at indexes: [Int]) {
let newVideos = [Video]()
for (index, video) in favorite!.videos.enumerated() {
if !indexes.contains(index) {
newVideos.append(video)
}
}
let realm = try! Realm()
try! realm.write {
realm.delete(newVideos)
}
}
只要您在集合视图上设置了 Realm 通知块,就可以将它们从 UI 中删除。