如何暂停 NSFetchedResultsController 的工作?
我有一个由 NSFetchedResultsController 提供的 UITableViewController 。用户可以从中调用模态视图控制器,在其中他或她可以输入新数据。首先,我创建一个临时对象,如下所示:
newPtr = [[Entry alloc] initWithEntity:[NSEntityDescription
entityForName:@"Entry" inManagedObjectContext:self.nmocontext]
insertIntoManagedObjectContext:self.nmocontext];
当用户做出选择时,会设置此“临时”对象 newPtr 的属性。
问题在于,当模态 ViewController 可见时,基本 UITableViewController 保持活动状态。在某些情况下,当它意识到 newPtr 的强制属性尚未设置时,它似乎会崩溃(导致崩溃)。
我该怎么做才能阻止 NSFetchedResultsController 查看我的托管对象上下文,直到模态 ViewController 被关闭?
I have a UITableViewController fed by an NSFetchedResultsController. From it, the user can call up a modal ViewController in which he or she can enter new data. As this begins, I create a temporary object as follows:
newPtr = [[Entry alloc] initWithEntity:[NSEntityDescription
entityForName:@"Entry" inManagedObjectContext:self.nmocontext]
insertIntoManagedObjectContext:self.nmocontext];
As the user makes choices, attributes of this 'provisional' object, newPtr, are set.
The problem is that the base UITableViewController remains active while the modal ViewController is visible. It seems to be freaking out (causing crashes) in some cases when it realizes a mandatory attribute of newPtr has not been set yet.
What can I do to stop the NSFetchedResultsController from looking at my managed object context until the modal ViewController is dismissed?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
Core Data 支持“嵌套”托管对象上下文,这允许灵活的架构,从而可以轻松支持独立的、可取消的变更集。通过子上下文,您可以允许用户对托管对象进行一组更改,然后这些更改可以作为单个事务批量提交给父级(并最终保存到存储中),也可以被丢弃。如果应用程序的所有部分只是从应用程序委托中检索相同的上下文,则这种行为很难或不可能支持。
Core Data supports "nested" managed object contexts which allow for a flexible architecture that make it easy to support independent, cancellable, change sets. With a child context, you can allow the user to make a set of changes to managed objects that can then either be committed wholesale to the parent (and ultimately saved to the store) as a single transaction, or discarded. If all parts of the application simply retrieve the same context from, say, an application delegate, it makes this behavior difficult or impossible to support.
我自己还没有对此进行测试,但可能的方法是实现
viewWillAppear
和viewWillDisappear
,并将 fetchedResultsController 委托设置为 self >将出现,nil将消失。或者
您可以创建一个 NSObject,它在编辑窗口中镜像 NSManagedObject 的属性。一旦用户完成属性编辑(并且您已运行适当的验证规则),您可以将它们传递回 NSManagedObject 实例并让 fetchedResultsController 完成其工作。
I haven't tested this myself but a possible approach would be to implement
viewWillAppear
andviewWillDisappear
, and set the fetchedResultsController delegate toself
on will appear andnil
on will disappear.OR
You could create an NSObject that mirrors the attributes of your NSManagedObject in your editing window. Once the user has finished editing the attributes (and you have run the appropriate validation rules) you can pass them back to your NSManagedObject instance and let the fetchedResultsController do its job.