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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 16, 20262026-05-16T16:13:11+00:00 2026-05-16T16:13:11+00:00

Okay, here’s the situation. I have two NSManagedObjects called Store and Aisle . There

  • 0

Okay, here’s the situation. I have two NSManagedObjects called Store and Aisle. There is a one-to-many relationship from Store to Aisle and a one-to-one relationship from Aisle to Store.

I have existing stores added in Core Data that I can access just fine. My problem is that I cannot figure out how to add an Aisle object to an existing Store. I’ve tried fetching the Store and then adding an Aisle object to the .Aisles set, I’ve tried using the AddAislesObject: method provided by CoreData in Store.h, and I’ve tried adding a Store object to the .Stores set in the Aisle object.

I’m watching my SQLite database and it looks like the Aisles are being added successfully and that they are associated to the correct Store using the Core Data primary key. But for some reason when I try to retrieve the Aisle objects (using the .Aisles property of the fetched Store object) I always get a zero-count NSSet.

I’ve also tried deleting my .sqlite file to no avail.

Does anyone have some simple code or even a high level suggestion for the best way to do this?

Thank you!

— UPDATE 8/30 0917 CST

I’ve taken some of Adam’s suggestions below and my aisles are definitely saving into Core Data correctly. However, when I try to access the saved Aisle from the Store object, I’m not getting anything returned. In the code below you can see that I’m retrieving an existing Store object from Core Data using a name and location (there are safeguards on insert to make sure that this combination is unique). I then use some NSLog debugs to access the single Store in the returned array, and the aisles property to iterate through any aisles. Ignore everything below the debug loops because I haven’t finished the method yet.

What I’m seeing is that aisleList has zero objects in it.

-(NSArray *)getAisleListByStore: (Store *)aStore {

// this method should return an NSArray of aisles for aStore

NSManagedObjectContext *context = [self managedObjectContext];
NSFetchRequest *fetchRequest = [[NSFetchRequest alloc] init];
NSEntityDescription *entity = [NSEntityDescription entityForName:@"Store" inManagedObjectContext:context];
NSPredicate *predicate = [NSPredicate predicateWithFormat:@"name MATCHES[c] %@ AND location MATCHES[c] %@", aStore.name, aStore.location];

[fetchRequest setEntity:entity];
[fetchRequest setPredicate:predicate];

NSError *error;
NSArray *storesList = [context executeFetchRequest:fetchRequest error:&error];
[fetchRequest release];

if (storesList == nil || [storesList count] != 1) {

    // error detected
    NSLog(@"Error retrieving stores: %@", [error localizedDescription]);
    UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Error" message:@"Error retrieving stores" delegate:nil cancelButtonTitle:@"Ok" otherButtonTitles:nil];
    [alert show];
    [alert release];
    return nil;

} else {

    // get aisles

    // debugs
    for (Store *theStore in storesList) {
        NSLog(@"Name: %@", theStore.name);
        NSLog(@"Aisle count: %d", [theStore.aisles count]);
        NSSet *aisleList = theStore.aisles;

        for (Aisle *theAisle in aisleList) {
            NSLog(@"Aisle name: %@", theAisle.name);
        }
    }

    // Store *theStore = [storesList objectAtIndex:0];

    // NSArray *aisles = (NSArray *)theStore.aisles;
    // return aisles;
}

}

  • 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-16T16:13:11+00:00Added an answer on May 16, 2026 at 4:13 pm

    No need for a third table.

    You probably have a NSSet* in Store, but you have an Store* attribute in Aisle. This is one huge advantage of following Core Data’s advice about making every relationship have an inverse.

    Store* store = [NSEntityDescription insertNewObjectForEntityForName:@"Store" inManagedObjectContext:moc];
    Aisle* aisle = [NSEntityDescription insertNewObjectForEntityForName:@"Aisle" inManagedObjectContext:moc];
    

    Assuming the Aisle entity has the relationship inStore as an inverse of Store’s to-many aisles relationship:

    aisle.inStore = store;
    

    You’ll find Core Data takes care of the other side of the relationship, i.e. adding the Aisle to Store’s NSSet.

    Don’t forget:

      NSError *error;
      if (![moc save:&error]) 
      {
        NSLog(@"Core Data Save error %@, %@", error, [error userInfo]);
      }
    

    to make your changes permanent.

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

Sidebar

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.