1
votes

Why do remotely deleted entities not removed from Core Data and the datastore? Setting a breakpoint at the beginning of - (void)deleteCachedObjectsMissingFromResult:(RKObjectMappingResult *)result in RKManagedObjectLoader shows up that the variable result does not contain anything.

I could fix that problem by implementing this feature in the RestKit delegate - (void)objectLoader:(RKObjectLoader *)objectLoader didLoadObjects:(NSArray *)objects but that is kind of unclean code in my point of view. RestKit / Core Data should do that by itself?! Anyway, following implementation would solve the problem:

- (void)objectLoader:(RKObjectLoader *)objectLoader didLoadObjects:(NSArray *)objects
{

NSArray *allReservations = [Reservation findAll];  

for(Reservation *reservationRecord in allReservations) {
    if(![objects containsObject:reservationRecord]) {
        [[[[RKObjectManager sharedManager] objectStore] managedObjectContextForCurrentThread] deleteObject:reservationRecord];
    }
}
}

any ideas to solve that problem without the help of didLoadObjects? Adding / updating existing entities works properly.

1
Am stuck at exactly the same point. Glad that you have a solution here, but am wondering why the method -deleteCachedObjectsMissingFromResult does not delete the now non existent server objects. I see that somehow the method -fetchRequestForResourcePath does not give back the appropriate fetchRequest, but I havent done enough research to see how exactly RKObjectMappingProvider stores / creates fetchRequests for a resource path.Raj Pawan Gumdal

1 Answers

0
votes

RestKit will only delete the entries in the NSManagedObjectContext. Your method only edits the objects in the NSManagedObjectContext but never saves them to the objectStore. Make sure to save the changes to the ObjectStore after the adding/editing/deleting has been finished.

- (void)objectLoader:(RKObjectLoader *)objectLoader didLoadObjects:(NSArray *)objects
{
    NSArray *allReservations = [Reservation findAll];  

    // Deleting each item in NSManagedObjectContext
    for(Reservation *reservationRecord in allReservations) {
        if(![objects containsObject:reservationRecord]) {
            [[[RKObjectManager sharedManager] objectStore] managedObjectContextForCurrentThread] deleteObject:reservationRecord];
        }
    }

    // Changes only exist in NSManagedObjectContext, delete them in the ObjectStore
    NSError *error = nil;
    if (![[[RKObjectManager sharedManager] objectStore] managedObjectContextForCurrentThread] save:&error]) 
        NSLog(@"Unresolved error %@, %@", error, [error userInfo]);
}