我只是试图从一个实体中删除三个nsmanagedobject,正如我在其他应用程序中所做的那样,一切正常。这次核心数据似乎在保存期间崩溃,没有错误,我在所有应用程序中使用应用程序委托上下文,所以我认为这不是一个多重上下文问题...我已经尝试使用[NSThread isMainThread进行检查在保存期间,我在主线程中,因此不是线程问题......我的代码有什么问题?
-(void)EraseStore{
if (contesto == nil) {
contesto = [(AppDelegate *)[[UIApplication sharedApplication] delegate] managedObjectContext];
}
NSError *error=nil;
NSFetchRequest *fetchRequest = [[NSFetchRequest alloc] init];
NSEntityDescription *entity = [NSEntityDescription entityForName:@"Store" inManagedObjectContext:contesto];
[fetchRequest setEntity:entity];
NSArray *fetchedObjects = [contesto executeFetchRequest:fetchRequest error:&error];
NSString *Store1=[NSString stringWithFormat:@"%@",@"55"];
NSString *Store2=[NSString stringWithFormat:@"%@",@"63"];
NSString *Store3=[NSString stringWithFormat:@"%@",@"11"];
for (int i=0; i<[fetchedObjects count]; i++) {
NSString *StoreId=[NSString stringWithFormat:@"%@",[[fetchedObjects objectAtIndex:i]valueForKey:@"id"]];
if ([StoreId isEqualToString:Store1] || [StoreId isEqualToString:Store2]|| [StoreId isEqualToString:Store3]) {
[contesto deleteObject:[fetchedObjects objectAtIndex:i]];
}
}
NSError *error1 = nil;
BOOL success = [contesto save:&error1];
if (!success) {
NSLog(@"Unresolved error2 %@, %@", error1, [error1 userInfo]);
}
else{
NSLog(@"saved");
}
}
我如何创建我的上下文:
- (NSManagedObjectContext *)managedObjectContext {
if (managedObjectContext_ != nil) {
return managedObjectContext_;
}
NSPersistentStoreCoordinator *coordinator = [self persistentStoreCoordinator];
if (coordinator != nil) {
managedObjectContext_ = [[NSManagedObjectContext alloc] init];
[managedObjectContext_ setPersistentStoreCoordinator:coordinator];
}
return managedObjectContext_;
}
我收到的错误消息:
未解决的错误2错误域= NSCocoaErrorDomain代码= 133020“操作无法完成。(Cocoa错误133020.)”UserInfo = 0x23cf00 {conflictList =(NSMergeConflict(0x23cdc0)for NSManagedObject(0x234960)with objectID'0x2da8b0' oldVersion = 1,newVersion = 2,旧对象快照= ....
修改
在数据库结构中,我没有在表之间设置任何关系。