用核心数据实施foreach.onmove()
我正在尝试使用核心数据实现foreach.onmove
。我有两个实体,parent
,与conder
有一段经常的关系,并将其标记为有序。视图是这样的:
struct ParentView : View {
@ObservedObject var parent : Parent
var body : some View {
List {
ForEach($parent.children! as! [Child]) { child in
ChildView(child)
}.onMove {
parent.managedObjectContext!.performAndWait { indices, to in
(parent.children! as! NSMutableOrderedSet).moveObjects(at: indices, to: to)
try! parent.managedObjectContext!.save()
parent.objectWillChange.send()
}
}
}
}
}
结果是:
- 没有任何错误。
- 在调试
onMove
函数期间,我可以看到根据需要重新订购这些项目 ManageDobjectContext.upDateDobjects
在同一调试步骤中为空,然后呼叫<<<代码> save()- 重新加载应用程序时,重新订购显然没有保存(显然是因为
更新>“更新”
集合设置为#3。
我在做什么错?如何使MOC意识到重新排序的更改?
I am trying to implement ForEach.onMove
using Core Data. I have two entities, Parent
with a To-Many relationship to Child
, with the relationship marked as Ordered. The view is something like this:
struct ParentView : View {
@ObservedObject var parent : Parent
var body : some View {
List {
ForEach($parent.children! as! [Child]) { child in
ChildView(child)
}.onMove {
parent.managedObjectContext!.performAndWait { indices, to in
(parent.children! as! NSMutableOrderedSet).moveObjects(at: indices, to: to)
try! parent.managedObjectContext!.save()
parent.objectWillChange.send()
}
}
}
}
}
Results are:
- No errors of any kind.
- During debug of the
onMove
function, I can see that the items are re-ordered as required - The
managedObjectContext.updatedObjects
is empty at the same debug step, before the call tosave()
- When reloading the app, the re-ordering is obviously not saved (apparently because the
updatedObjects
set was empty at #3.)
What am I doing wrong? How can I make the MOC realize the re-ordering change?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我修复了这一点,显然是用一个简单的解决方案解决的。我将其发布给子孙后代的Google搜索者。 :d
问题与此行:
显然,拿起不变的版本并使其可变起来,但没有更新托管对象。这有效:
I fixed this, apparently with a simple solution. I am posting this for future generations of google searchers. :D
The problem is with this line:
Apparently, taking the immutable version and making it mutable works, but doesn't update the managed object. This works: