多层MOC合并...CoreData老手,您怎么看?
原始问题已得到解答。更新解决了评论中提出的相关问题。
原帖:
我正在使用 Apple CoreDataBooks。然而,我似乎使用了两层 MOC 合并(三个 MOC,其中第三层与第二层合并,然后第二层与第一层合并)。
首先,我有一个列出学校班级的表格视图(ClassList)。选择一个类会推送一个 2 行表格视图 (AddClass)。在 AddClass 中,第一行允许用户编辑类标题。选择第二行会推送一个表格视图 (ClassRoster),其中显示该班级的学生名册。最后,选择一个学生会推送另一个 2 行表格视图 (AddStudent),用户可以在其中编辑学生姓名和用户名。
我可以使用双 MOC 合并方法(CoreDataBooks 使用的 ManagedObjectContext 和 AddingManagedObjectContext)成功添加和保存类。我将在第一个视图中将“基本 MOC”称为“MOC1”,将“便签本”MOC 称为“MOC2”。
MOC2 临时存储对类对象所做的更改。然后可以保存或取消这些更改,并向委托发送 -didFinishWithSave:(BOOL)
。如果我保存,MOC2 中所做的更改将与 MOC1 合并。该合并工作完美。
处理对学生对象所做的更改是我出错的地方。我认为我可以使用 MOC3 作为对学生对象进行更改的暂存器,这些更改将与 MOC2 合并(当我保存学生对象时)。当我保存类对象时,MOC2 又可以与 MOC1 一起保存。
但我在保存 MOC3 并将学生对象添加到类对象时遇到了错误,因为它们位于不同的上下文中。我可以发布代码,但首先我想问一个更大的问题:我的做法是否完全错误?
更新:
Zarra 先生建议使用 initWithEntity:insertIntoManagedObjectContext:
并将 MOC 设置为 nil,从而创建一个临时对象,稍后可以设置并保存其 MOC。
按照他的建议,我尝试合并以下代码:
NSManagedObjectModel *managedObjectModel - [[managedObjectContext persistentStoreCoordinator] managedObjectModel];
NSEntityDescription *entity = [[managedObjectModel entitiesByName] objectForKey:@"MyClass"];
MyClass *newClass = [[MyClass alloc] initWithEntity:entity insertIntoManagedObjectContext:nil];
我遇到了错误,但我不确定它是否与此代码相关。我将调试并发布我发现的内容。
Original question has been answered. Update addresses related question raised in comments.
Original post:
I am using the MOC save method used in Apple's CoreDataBooks. However, I seem to have use for two layers of MOC merging (three MOCs where the 3rd merges with the 2nd and then the 2nd merges with 1st).
First, I have a tableview (ClassList) listing school classes. Selecting a class pushes a 2-row tableview (AddClass). At AddClass, the first row allows the user to edit the class title. Selecting the second row pushes a tableview (ClassRoster) that displays the student roster for that class. Lastly, selecting a student pushes on another 2-row tableview (AddStudent) where the user can edit the student name and username.
I can add and save classes successfully by using the dual MOC merge method (managedObjectContext and addingManagedObjectContext as employed by CoreDataBooks). I will call the "base MOC" in my first view "MOC1" and call the "scratchpad" MOC "MOC2".
MOC2 temporarily stores changes made to a class object. These changes can then either be saved or canceled, sending a -didFinishWithSave:(BOOL)
to the delegate. If I save, the changes made in MOC2 are merged with MOC1. That merge is working perfectly.
Handling changes made to student objects is where I'm going wrong. I thought I could employ MOC3 as a scratchpad for changes to student objects which would merge with MOC2 (when I saved a student object). MOC2 could in turn be saved with MOC1 when I saved the class object.
But I have run into errors with saving MOC3 and adding student objects to class objects because they are in different contexts. I can post code, but first I wanted to ask the bigger question: Am I going about this all the wrong way?
UPDATE:
Mr. Zarra recommended using initWithEntity:insertIntoManagedObjectContext:
and setting the MOC to nil, thereby creating a temporary object which could later have its MOC set and saved.
Following his advice, I am attempting to incorporate the following code:
NSManagedObjectModel *managedObjectModel - [[managedObjectContext persistentStoreCoordinator] managedObjectModel];
NSEntityDescription *entity = [[managedObjectModel entitiesByName] objectForKey:@"MyClass"];
MyClass *newClass = [[MyClass alloc] initWithEntity:entity insertIntoManagedObjectContext:nil];
I have run into an error, but I'm not sure it is related to this code yet. I will debug and post what I find.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
是的。您不需要使用多个
NSManagedObjectContext
。这个例子是一个非常糟糕的例子。在您的情况下,您应该使用单一上下文,这将消除您的所有问题。如果您想要一个临时实体,请使用 nil NSManagedObjectContext 创建它。当您想要保存它时,您可以调用
-setManagedObjectContext:
,然后保存该NSManagedObjectContext
。您真正想要使用多个
NSManagedObjectContext
的唯一一次是当您处于多线程情况时。Yes. You do not need to use more than one
NSManagedObjectContext
. That example is a very poor one. In your case you should be using a single context and that will remove all of your issues.If you want a temporary entity, create it with a nil
NSManagedObjectContext
. When you want to save it you call-setManagedObjectContext:
and then save thatNSManagedObjectContext
.The only time you realistically want to use more than one
NSManagedObjectContext
is when you are in a multi-threaded situation.