1

I have 2 entity, Units and BUnits, the Units entity have a data that will be replaced many time, and the BUnits will be the backup of the Units entity before clearing it's data.

So, I've created a NSManagedObjectContext instance, then I've retrive an instance for each entity by using

NSManagedObjectContext *context = [mainDelegate managedObjectContext];

NSEntityDescription *unitsEntity = [NSEntityDescription entityForName:@"Units" inManagedObjectContext:context];

NSEntityDescription *bUnitsEntity = [NSEntityDescription entityForName:@"BUnits" inManagedObjectContext:context];

but i've didn't managed to copy the Units entity records to the BUnits entity, other than make a loop for each records, but i believe that there is a better solution.

What do you think about this, is there a better solution?

UPDATE:

The solution i've used in case anyone could use it is in my answer, I think there is a better way to do this, i will keep checking for it and i will update the question if i found anything.

Scar
  • 3,460
  • 3
  • 26
  • 51

2 Answers2

1

Here is what i've used, using looping for each record:

- (void) copyEntities:(NSString *)sourceEntity And:(NSString *)destinationEntity {
NSManagedObjectContext *context = [mainDelegate managedObjectContext];

NSEntityDescription *Units = [NSEntityDescription entityForName:sourceEntity inManagedObjectContext:context];
NSEntityDescription *BUnits = [NSEntityDescription entityForName:destinationEntity inManagedObjectContext:context];

NSFetchRequest *dataRequest = [[NSFetchRequest alloc] init];

[dataRequest setEntity:Units];

NSError *error = nil;

NSArray *dataArray = [context executeFetchRequest:dataRequest error:&error];

for (UnitsClass *unit in dataArray) {

    UnitsClass *savedUnits;

    NSFetchRequest *request = [[NSFetchRequest alloc] init];
    [request setEntity:BUnits];
    NSPredicate *pred = [NSPredicate predicateWithFormat:@"(code = %@)", unit.code];
    NSPredicate *pred1 = [NSPredicate predicateWithFormat:@"(code2 = %@)", unit.code2];

    NSPredicate *compoundPredicate = [NSCompoundPredicate andPredicateWithSubpredicates:[NSArray arrayWithObjects:pred, pred1, nil]];

    [request setPredicate:compoundPredicate];

    NSError *error = nil;

    NSArray *objects = [context executeFetchRequest:request error:&error];

    //this if to indicate if we inserting to the BUnits or updating it.
    if ([objects count] > 0) {
        //Found the record, update The info
        savedUnits = [objects objectAtIndex:0];
    } else {
        savedUnits = [NSEntityDescription insertNewObjectForEntityForName:destinationEntity inManagedObjectContext:context];
    }

    savedUnits.code = unit.code;
    /*Add your updated info*/

    NSError *errors;
    if (![context save:&errors]) {
        NSLog(@"Whoops, couldn't save: %@", [errors localizedDescription]);
    }
}

NSLog(@"BUnits count = %d",[context countForFetchRequest:[NSFetchRequest fetchRequestWithEntityName:destinationEntity] error:&error]);
}
Scar
  • 3,460
  • 3
  • 26
  • 51
0

Based on the given information, there is no better way than looping each unit object and creating the backup object.

Florian Mielke
  • 3,310
  • 27
  • 31
  • Thank you for the reply, I will update the question with the solution i've made using the loop in case anyone would use it. – Scar Jul 16 '12 at 13:21