0

I'm simply trying to delete three nsmanagedobject from an entity, as i already do in other application,where all went ok. This time core data seems to crash during save, there's no error, i'm using the app delegate context in all the app, so i think that isn't a multiple context problem... I have tried a check with [NSThread isMainThread] , during the save i'm in the main thread, so isn't a threading problem... What's wrong in my code?

-(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");
    }
}

How I create my context:

- (NSManagedObjectContext *)managedObjectContext { 
    if (managedObjectContext_ != nil) { 
        return managedObjectContext_; 
    } 
    NSPersistentStoreCoordinator *coordinator = [self persistentStoreCoordinator]; 
    if (coordinator != nil) { 
        managedObjectContext_ = [[NSManagedObjectContext alloc] init]; 
        [managedObjectContext_ setPersistentStoreCoordinator:coordinator]; 
    } 
    return managedObjectContext_; 
}

The error message I am getting:

Unresolved error2 Error Domain=NSCocoaErrorDomain Code=133020 "The operation couldn’t be completed. (Cocoa error 133020.)" UserInfo=0x23cf00 {conflictList=( "NSMergeConflict (0x23cdc0) for NSManagedObject (0x234960) with objectID '0x2da8b0 ' with oldVersion = 1 and newVersion = 2 and old object snapshot =....

EDIT

In database structure i haven't set any relationship between tables.

Matthias Bauch
  • 89,811
  • 20
  • 225
  • 247
cri1682
  • 442
  • 6
  • 17
  • 3
    What is the crash message? , also, I would recommend you to change your `id` property name to something else (maybe `storeId`), just not to confuse it with Objective-C keyword **id**. you can set your request predicate to `[NSPredicate predicateWithFormat:@"storeId IN %@",@[@"55",@"63",@"11"]]` and after execution just delete all returning objects (instead of loading all existing `Store` object to memory and filtering them there). – Dan Shelly May 24 '13 at 09:45
  • Until now there wasn't a crash message...Now i have do you suggested modification and i have this log: Unresolved error2 Error Domain=NSCocoaErrorDomain Code=133020 "The operation couldn’t be completed. (Cocoa error 133020.)" UserInfo=0x23cf00 {conflictList=( "NSMergeConflict (0x23cdc0) for NSManagedObject (0x234960) with objectID '0x2da8b0 ' with oldVersion = 1 and newVersion = 2 and old object snapshot =.... It seems that there is two context, how it is possible? I'm using only one context... – cri1682 May 24 '13 at 10:02
  • Have you modified your `AppDelegate`s `managedObjectContext` method? are you using other contexts in the application? – Dan Shelly May 24 '13 at 10:13
  • No i haven't modify my managedObjectContext method: - (NSManagedObjectContext *)managedObjectContext { if (managedObjectContext_ != nil) { return managedObjectContext_; } NSPersistentStoreCoordinator *coordinator = [self persistentStoreCoordinator]; if (coordinator != nil) { managedObjectContext_ = [[NSManagedObjectContext alloc] init]; [managedObjectContext_ setPersistentStoreCoordinator:coordinator]; } return managedObjectContext_; } – cri1682 May 24 '13 at 10:51
  • I added the additional details to your question. In the future, please do so yourself. Posting this stuff in the comments does not help because it's basically unreadable. Also, try to get the indention of your code correct, and avoid superfluous whitespace. This will improve the readability (and the quality) of your question. – Matthias Bauch May 24 '13 at 10:57
  • Additionally, a full and complete error message would help a lot. – Matthias Bauch May 24 '13 at 11:00
  • Do you test your code on simulator? Do "Reset Content and Settings..." for it or remove the app in the way you like. Then try your code again. Looks like you have a conflict of two different data-model versions. – makaron May 24 '13 at 13:10
  • @makaron a model conflict will not result in a merge conflict, he would not be able to access the store at all. @cri1682 Do you invoke `managedObjectContext` from more then one thread? – Dan Shelly May 24 '13 at 13:41
  • Do you use more than one managed object context in this app? – Tom Harrington May 24 '13 at 16:06

0 Answers0