0

我只是想从一个实体中删除三个 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 “操作无法完成。(可可错误 133020。)” UserInfo = 0x23cf00 {conflictList =(“NSMergeConflict (0x23cdc0) for NSManagedObject (0x234960) with objectID '0x2da8b0' with oldVersion = 1 和 newVersion = 2 和旧对象快照 =....

编辑

在数据库结构中,我没有设置表之间的任何关系。

4

0 回答 0