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 7615495
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 31, 20262026-05-31T02:39:27+00:00 2026-05-31T02:39:27+00:00

I am having issues creating, and understanding how to create a core data model

  • 0

I am having issues creating, and understanding how to create a core data model for this situation.

(1) A person can have multiple pets (either combination of a Dog or Cat).

(2) There can be multiple people too.

I want to go through the Person entity and pull each person, and each pet and there information.

I’m not sure if I should use a relationship, or how can I set this model up in Core Data. I quickly jotted down a picker of what I thought the model would look. I made this model for simplicity sake, my model doesn’t deal with Cats and Dogs.

Any suggestions or ideas is greatly appreciated.

enter image description here

  • 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-31T02:39:28+00:00Added an answer on May 31, 2026 at 2:39 am

    I quickly put together a model for you:

    enter image description here

    So, basically “Person” is your person, which has a relationship “pets” – this is a “to many” relationship. One person can have multiple pets.

    Then there is a “Pet” entity. It’s an abstract entity that represents any pet, cats and dogs alike. It has an inverse relationship to “pets” of “Person”. Therefore, from any pet, you can always trace back to the corresponding owner. Also, every subclass of “Pet” will have some common attributes, like age/name/weight.

    Additionally, subclasses (entities which have “Pet” as “Parent Entity”, like “Dog” and “Cat”) can have their own attributes in addition to the attributes of “Pet”, like a “Dog” has a “barkSound”, and a “Cat” has a “meowSound”.

    You can of course add as many persons into your storage as you want, this has nothing to do with the data model.

    To retrieve the information, simply use a fetch request to fetch all persons. Then loop through them and access their “pets” property to get NSSets for their pets. You can loop through those sets to access the information of the pets.

    Here is an example of how to fetch all persons, then all pets:

    // Fetch all persons
    NSFetchRequest *fetchRequest = [[NSFetchRequest alloc] init];
    NSEntityDescription *entity = [NSEntityDescription entityForName:@"Person" inManagedObjectContext:yourContext];
    [fetchRequest setEntity:entity];
    
    NSError *error = nil;
    NSArray *fetchedObjects = [yourContext executeFetchRequest:fetchRequest error:&error];
    if (fetchedObjects == nil) {
        // Handle error case
    }
    
    [fetchRequest release];
    
    // Loop through all their pets
    for (Person* person in fetchedObjects)
    {
        NSLog(@"Hello, my name is %@", person.name);
    
        for (Pet* pet in person.pets) {
            if ([pet isKindOfClass:[Dog class]])
            {
                NSLog(@"Woof, I'm %@, owned by %@", pet.name, pet.owner.name);
            }
            else
            {
                NSLog(@"Meow, I'm %@, owned by %@", pet.name, pet.owner.name);
            }
        }
    }
    

    Note that you can also just fetch the pets, without going through their owners:

    // Fetch all persons
    NSFetchRequest *fetchRequest = [[NSFetchRequest alloc] init];
    NSEntityDescription *entity = [NSEntityDescription entityForName:@"Pet" inManagedObjectContext:yourContext];
    [fetchRequest setEntity:entity];
    [fetchRequest setIncludesSubentities:YES]; // State that you want Cats and Dogs, not just Pets.
    
    NSError *error = nil;
    NSArray *fetchedObjects = [yourContext executeFetchRequest:fetchRequest error:&error];
    if (fetchedObjects == nil) {
        // Handle error case
    }
    
    [fetchRequest release];
    

    The code above is not tested, and might include typos, but will give you the idea on how to do it.

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

Sidebar

Related Questions

I'm having issues with data persisting inside of multiple instances of objects I'm creating.
Still having issues with this problem. Please help if you can. So I am
Im having issues getting this to work, maybe its not even possible? I have
I'm having issues creating an ActionLink using Preview 5. All the docs I can
I have been having some issues with creating a connection to amazon's MWS service.
I’m having issues creating an IN clause using C# and lambdas. I have the
I am busy creating an order system. I am having issues trying to create
I am busy creating an order system. I am having issues trying to create
Having issues referencing $(this) from within a the nested ajax 'success' function... I know
I am having some issues creating a setup file for my Outlook 2007 add-in.

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.