How to clear an object and add new entries with a magical record

When I request new data, I want to delete all records before adding news. Sometimes there is no change between the old data and the new data.

This is what I tried, but my new entry is never saved (always 0 entries saved)

When I request data:

Autorisation* aut = [Autorisation MR_createEntity];
// setter method

      

When I want to save:

+(void)saveAutorisationList:(NSMutableArray*)autorisationList{
    NSManagedObjectContext* localContext = [NSManagedObjectContext MR_defaultContext];
    for (Autorisation* aut in [self getAutorisationList]) {
      [aut MR_deleteEntityInContext:localContext];  // method that return all Autorisation
    }
    [localContext MR_saveToPersistentStoreWithCompletion:^(BOOL contextDidSave, NSError * error) {
        for (Autorisation* aut in autorisationList) {
            [aut MR_inContext:localContext];
        }
        [localContext MR_saveToPersistentStoreWithCompletion:nil];
    }];
}

+(NSMutableArray*)getAutorisationList {
    NSManagedObjectContext* localContext = [NSManagedObjectContext MR_defaultContext];
    return [[Autorisation MR_findAllInContext:localContext] mutableCopy];
}

      

+3


source to share


1 answer


What's going on here is that you are deleting all objects, including the ones you want to keep. Here's what happens step by step:

+(void)saveAutorisationList:(NSMutableArray*)autorisationList {
    // as it seems, here autorisationList is a list of new objects you want to save.

    NSManagedObjectContext* localContext = [NSManagedObjectContext MR_defaultContext];

      

The misbehavior starts here:

    for (Autorisation* aut in [self getAutorisationList]) {

      

getAutorisationList

All objects existing in localContext

, old + new are selected here .

      [aut MR_deleteEntityInContext:localContext];  
      // here you deleted each Autorisation object currently existing, including those you want to save
    }
    ...
}

      



Instead, you should look for the differences between what you got and what came before, and only remove the objects that were not received with the update.

eg. imagine you have a set of objects OldSet = {auth1, auth2, auth3}

, and when you update, you get objects NewSet = {auth2, auth3, auth4}

. Differences to remove will be

ToBeDeletedSet = OldSet - NewSet = {auth1}

      

This way you will keep the records you had and keep the new records.

Then your save method will look like this:

+(void)saveAutorisationList:(NSMutableArray*)updatedAutorisationList{
    NSManagedObjectContext* localContext = [NSManagedObjectContext MR_defaultContext];
    NSMutableArray *oldAutorisationList = [self getAutorisationList];
    [oldAutorisationList removeObjectsInArray: updatedAutorisationList];
    for (Autorisation* aut in oldAutorisationList) {
      [aut MR_deleteEntityInContext:localContext];  // method that return all Autorisation
    }
    [localContext MR_saveToPersistentStoreWithCompletion:^(BOOL contextDidSave, NSError * error) {
        for (Autorisation* aut in updatedAutorisationList) {
            [aut MR_inContext:localContext];
        }
        [localContext MR_saveToPersistentStoreWithCompletion:nil];
    }];
}

      

+1


source







All Articles