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

  • Home
  • SEARCH
  • 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 618757
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 13, 20262026-05-13T18:34:53+00:00 2026-05-13T18:34:53+00:00

I have a model with 2 entities: RealEstate and Container Containers object are already

  • 0

I have a model with 2 entities: RealEstate and Container

Containers object are already saved onto the persistent store with the following hierarchy:

Container 1
    Container 2
    Container 3
Container 4
    Container 5
        Container 6

Each container has a RealEstate owner (in a given hierarchy the realEstate is always the same)
Now I would like create a copy of this hierarchy changing for each containers the owner realEstate.
After some tests it seems to me that it’s not a trivial problem.

This is a simplified scheme of the model:

RealEstate (entity)
-------------------
name (string attribute)
containers (relation)

Container (entity)
------------------
level (int attribute)
name (string attribute)
parent (self relation to another container)
subcontainers (relation - set of containers)
realEstate (relation)

Basically each containers has a subcontainers relation with self, and a parent relation so for example
Container 1 has no parent but subcontainers=[container 2, container 3] etc…

I have 2 questions:

  • if I want to copy a single attribute (NSNumber) should I copy the attribute before assign it to the copied container with something like [newContainer setLevel:[[container level] copy]] because NSNumber is actually a pointer, or it’s ok to assign [newContainer setLevel:[container level]] ??
  • how to copy a relation?? I cannot simply copy the subcontainers with [newContainer setSubcontainers:[container subcontainers]] !!

This is what I’m doing:

- (void)copyContainersFromRealEstate:(RealEstate *)sourceRealEstate toRealEstate:(RealEstate *)destinationRealEstate {
    // read all RealEstate Containers to copy
 NSFetchRequest *request = [[NSFetchRequest alloc] init];
 NSPredicate *predicate = [NSPredicate predicateWithFormat:@"realEstate == %@", sourceRealEstate];
 [request setPredicate:predicate];
 [request setEntity: [ NSEntityDescription entityForName:@"Container"
                 inManagedObjectContext:destinationRealEstate.managedObjectContext] ];

 NSError *error;
 NSArray *results = [destinationRealEstate.managedObjectContext executeFetchRequest:request 
                          error:&error];

 if (results == nil) {
      NSLog(@"Unresolved error %@, %@", error, [error userInfo]);
      abort();
 }
 [request release];

    // *******************************************************
 // copy each caontainer changing the real estate
 Container *newContainer;
 for (Container *container in results) {
      newContainer = (Container *)[NSEntityDescription insertNewObjectForEntityForName:@"Container" 
                 inManagedObjectContext:destinationRealEstate.managedObjectContext];

      [newContainer setRealEstate:destinationRealEstate];
      [newContainer setLevel:[container level]];
      [newContainer setSubcontainers:[container subcontainers]];  // WRONG
      [newContainer setName:[container name]];
      [newContainer setParent:[container parent]];  // WRONG
 }
    // *******************************************************

}

Any help will be appreciated.
Thanks

  • 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-05-13T18:34:54+00:00Added an answer on May 13, 2026 at 6:34 pm

    if I want to copy a single attribute (NSNumber) should I copy the attribute before assign it to the copied container with something like [newContainer setLevel:[[container level] copy]] because NSNumber is actually a pointer, or it’s ok to assign [newContainer setLevel:[container level]] ??

    Set the value onto the new object, it will handle it even though it is a pointer because Core Data will be writing the value down into the database so it will be translating it to a primitive at some point and “break” that pointer relationship that you are concerned about.

    how to copy a relation?? I cannot simply copy the subcontainers with [newContainer setSubcontainers:[container subcontainers]] !!

    Just set the objects and Core Data will do the right thing and construct the new relationships correctly. When you pass the NSSet to the new parent object, Core Data will iterate through the objects in the set and create new relationship connections for each one of them to the new parent object.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Here's my situation.. Suppose you have the following model entities, which represent single tables
I have a model that has an arbitrary number of children entities. For simplicity
In GAE, I have a model called Foo, with existing entities, and attempt to
In my Core Data model I have two entities: List and Patient. List has
If I have Model.objects.all() I want to get only one object for any content_object=foo,
I have created domain model and define entities, value objects, Services and so on.
My CoreData model have 3 entities with to-many relationships: job <<-->> group <<-->> person
For example I have data model like this Entity: Store Entity: Inventory Entity:Product Attribute:
I have a data model that has 2 entities Person and Photo, they have
HI, I have the following model: @Entity class Flight{ private Airport airportFrom; private Airport

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.