Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8283215
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 8, 20262026-06-08T10:39:26+00:00 2026-06-08T10:39:26+00:00

I have 2 entity, Units and BUnits, the Units entity have a data that

  • 0

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.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-08T10:39:27+00:00Added an answer on June 8, 2026 at 10:39 am

    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]);
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

i have an entity Entity1 that have one to many relation with Entity2 as
I have an application that contains methods that work with data using Entity Framework
In my business model I have Entity class (IPoint interface ) that has a
I have an Entity which holds a collection of OtherEntity in a many-to-many style
I have entity Unit and Tag , each with to-many relation to other. I
I have a User entity which has a HasCompletedSecurity property which indicates whether that
We have some code that uses Entity Framework against a DB2 database. When we
Suppose you have a method: public void Save(Entity data) { this.repositoryIocInstance.EntitySave(data); } Would you
I have configured two persistent units with the entity managers set up as show
I have a two entity classes called User and Group, both reserved words that

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.