Julien
Julien

Reputation: 87

Save an object after Delete in Core Data with Restkit make it crash

I'm trying to use RestKit and Core Data as a local cache of server side datas, but i can't manage to do it

I created a NSManagedObject :

@interface JSONShop : NSManagedObject

@property (nonatomic, retain) NSNumber * shopId;
@property (nonatomic, retain) NSNumber * type;

@end
@implementation JSONShop

@synthesize shopId;
@synthesize type ;

@end

It is automatically mapped and stored by Restkit, and I can fetch it from the database to display it in an TableViewController.

I have a primary key on my table (defined in my mapping), for example :

shopMap.primaryKeyAttribute = @"shopId" ;  

When I store my objects for the first time, everything is all right :)

After, when I delete all entities in the context , when Restkit try to map it again and save it again but I obtain this error :

'CoreData could not fulfill a fault for '0x9412ea0 <x-coredata://F8451322-3890-430F-8ABD-B5EEF1DFED2F/JSONShop/p177>'' 

Do you know why ?

Here is my delete code :

- (void) deleteAllObjects: (NSString *) entityDescription  {

    NSLog(@"Deleting %@",entityDescription);

    NSManagedObjectContext * managedObjectContext = [[RKObjectManager sharedManager].objectStore managedObjectContext] ;
    NSFetchRequest *fetchRequest = [[NSFetchRequest alloc] init];
    NSEntityDescription *entity = [NSEntityDescription entityForName:entityDescription inManagedObjectContext:managedObjectContext];
    NSError * error ;
    [fetchRequest setEntity:entity];

    NSArray *items = [managedObjectContext executeFetchRequest:fetchRequest error:&error];

    for (NSManagedObject *managedObject in items) {
        [managedObjectContext deleteObject:managedObject];
    }
    if (![managedObjectContext save:&error]) {
        NSLog(@"Error deleting %@ - error:%@",entityDescription,error);
    }

}

Upvotes: 2

Views: 2529

Answers (2)

lottadot
lottadot

Reputation: 367

So you are using Restkit to obtain your data, and using RestKit to store the data within a CoreData Moc.

I don't see where you are actually saving the changes to disk.

Why not use RestKit to get the objects then delete them? i.e.

- (IBAction)deleteAllButtonClicked:(id)sender {

    NSArray* objects = [JSONShop findAll]; 
    for (JSONShop *object in objects) { 
        [[JSONShop managedObjectContext] deleteObject:object]; 
    } 
    NSError* error = nil; 
    [[JSONShop managedObjectContext] save:&error]; 
    if (nil != error) { 
        // Error checking here... 
    }
}

This example project does similar: lottadot-restkit-ios-rails3-1-advanced

Upvotes: 1

romrom
romrom

Reputation: 642

Before RestKit 0.10, the RKManagedObjectStore was keeping a cached copy of every used entity. But when the entity was deleted from CoreData, the object was kept in the cache. So later on when you want to re-save the same object, instead of creating it in CoreData, it fetched the (faulty) object from its cache. I had the exact same issue, see CoreData validation error 1550

Two solutions:

  • Either upgrade to RestKit 0.10 (issue is fixed as part of a bigger rework of the store)
  • Or patch your current RKManagedObjectStore to disable the in-memory caching

Upvotes: 0

Related Questions