CoreData 的保存方法不起作用
我正在使用 coredata 为我的 iPhone 创建一个应用程序。
我有一个带有对象的视图控制器,我想将该对象保存到我的FavoriteViewController 中。 通过单击收藏夹中的按钮,我希望将我的对象保存到 ManagedObjectContext 中,但出现以下错误:
由于未捕获的异常“NSInternalInconsistencyException”而终止应用程序,原因:“尝试使用已存在的提取执行另一个操作”进步'。 *
我的代码:
// Step 1: Create Object
Favorite * newFavorite = (Favorite*)[NSEntityDescription insertNewObjectForEntityForName:@"Favorite" inManagedObjectContext:managedObjectContext];
// Step 2: Set Properties
newFavorite.name = @"Company";
NSLog(@"%@",newFavorite);
// Step 3: Save Object
NSError *error = nil;
if (![newFavorite.managedObjectContext save:&error]) { // this is where the program crash
NSLog(@"Unresolved error %@, %@", error, [error userInfo]);
abort();
}
我不确定我做错了什么。
Im a creating an app for my iPhone using coredata.
I have a viewcontroller with an object i want to save that object to my FavoriteViewController.
By clicking a button favorite I want my object to be save into the managedObjectContext but I'm getting the following error:
Terminating app due to uncaught exception 'NSInternalInconsistencyException', reason: 'Attempted to perform another operation with a fetch already in progress'. *
My Code:
// Step 1: Create Object
Favorite * newFavorite = (Favorite*)[NSEntityDescription insertNewObjectForEntityForName:@"Favorite" inManagedObjectContext:managedObjectContext];
// Step 2: Set Properties
newFavorite.name = @"Company";
NSLog(@"%@",newFavorite);
// Step 3: Save Object
NSError *error = nil;
if (![newFavorite.managedObjectContext save:&error]) { // this is where the program crash
NSLog(@"Unresolved error %@, %@", error, [error userInfo]);
abort();
}
I am not sure what I'm doing wrong.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我猜测您有一个 UI 元素,例如表格,它会在 UI 更改时激活提取。例如,如果您有一个获取结果控制器,则表的任何滚动都可以激活获取结果控制器的获取。
您无法在迭代集合时改变该集合,因为迭代计数在迭代过程中会发生变化。获取会迭代与其实体和谓词匹配的对象集合。如果在提取工作时插入对象,则会出现错误。
通常您会在多个线程中看到此问题,但我认为 UI 可能会在适当的情况下触发该问题。
I am going to guess you have a UI element, such as a table, that activates a fetch when the UI is changed. For example, if you have a fetched results controller, any scrolling of the table can activate the fetched results controller's fetch.
You can't mutate a collection while iterating over that collection because the count of the iteration changes while the iteration is in process. A fetch iterates over the collection of objects matching its entity and predicate. If you insert an object while the fetch is working you will get an error.
Usually you see this problem with multiple threads but I think the UI might trigger the problem in the right set of circumstances.
您可能更改了表中的某些内容。如果这是真的,请尝试使用表
Z_METADATA (Z_VERSION, Z_UUID, Z_PLIST), Z_PRIMARYKEY (Z_ENT)
... 的原始值You have probably changed something in your tables. If this is true, try to use the original values for the table
Z_METADATA (Z_VERSION, Z_UUID, Z_PLIST), Z_PRIMARYKEY (Z_ENT)
...