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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 7, 20262026-06-07T04:03:54+00:00 2026-06-07T04:03:54+00:00

I have gone over this over and over, and cannot for the life of

  • 0

I have gone over this over and over, and cannot for the life of me figure out why assigning a relationship through setValue:forKey: is not working. My method is below.

-(void)saveNewCartItemName:(NSString *)name Price:(float)price Qty:(int)qty Tax:(int)tax inCart:(NSDate *)cartDate{
    NSManagedObject *newItem = [NSEntityDescription insertNewObjectForEntityForName:@"Item"
                                                             inManagedObjectContext:self.appDelegate.managedObjectContext];

    NSFetchRequest *query = [[NSFetchRequest alloc] initWithEntityName:@"Cart"];
    NSLog(@"cartDate = %@", cartDate);
    [query setPredicate:[NSPredicate predicateWithFormat:@"cartDate = %@", cartDate]];
    NSError *error;
    NSMutableArray *queryResults = [[self.appDelegate.managedObjectContext executeFetchRequest:query error:&error] mutableCopy];
    NSLog(@"queryResults count = %d", [queryResults count]);
    if ([queryResults count] == 1) [newItem setValue:[queryResults objectAtIndex:0] forKey:@"cartThatHasItem"];
    [newItem setValue:name forKey:@"itemName"];
    [newItem setValue:[NSNumber numberWithFloat:price] forKey:@"itemPrice"];
    [newItem setValue:[NSNumber numberWithInt:qty] forKey:@"itemQty"];
    [newItem setValue:[NSNumber numberWithInt:tax] forKey:@"itemTax"];

    [self.appDelegate.managedObjectContext save:&error];
}

The line that it crashes on every time is:

if ([queryResults count] == 1) [newItem setValue:[queryResults objectAtIndex:0] forKey:@"cartThatHasItem"];

My NSLog shows that there is a NSManagedObject to assign the relationship to, and I have double checked the name in the forKey: that there are no typos. All that Xcode tells me is that it prints out ‘(null)’ on my console and then tells me ‘libc++abi.dylib: terminate called throwing an exception’ on the next line.

Why is it crashing? And how do I fix it?

Update: That’s the entire error message.

2012-07-03 18:28:57.844 appName[3063:11603] (null)
libc++abi.dylib: terminate called throwing an exception
(lldb)
  • 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-07T04:03:55+00:00Added an answer on June 7, 2026 at 4:03 am

    Well, no enough debug info… Cause you only need to check whether queryResults has an object, so I will use

    [newItem setValue:[queryResults lastObject] forKey:@"cartThatHasItem"];
    

    instead of

    if ([queryResults count] == 1) [newItem setValue:[queryResults objectAtIndex:0] forKey:@"cartThatHasItem"];
    

    Not sure whether it’ll solve your issue, but worth trying. 🙂


    EDIT

    Through your question title ‘… for many-to-many relationship’, I wonder what’s the relationship between cartThatHasItem & Cart? If it is many to one or many to many, then the cartThatHasItem‘s data type will be NSSet. So, you need to wrap the [queryResults objectAtIndex:0]‘ like

    [newItem setValue:[NSSet setWithObject:[queryResults objectAtIndex:0]]
               forKey:@"cartThatHasItem"];
    

    , or you can use

    [newItem addCartThatHasItem:[queryResults objectAtIndex:0]]
    

    instead. (addCartThatHasItem: will be added automatically when you generate your Cart model.)


    EDIT 2

    The Cart model should be like this:

    @interface Cart : NSManagedObject
    
    //...
    @property (nonatomic, retain) NSSet *cartThatHasItem;
    
    @end
    
    @interface Cart (CoreDataGeneratedAccessors)
    
    //...
    - (void)addCartThatHasItemObject:(CartThatHasItem *)value;
    - (void)removeCartThatHasItemObject:(CartThatHasItem *)value;
    - (void)addCartThatHasItem:(NSSet *)values;
    - (void)removeCartThatHasItem:(NSSet *)values;
    
    @end
    

    if you generate your model correctly.

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

Sidebar

Related Questions

Over the years as I have gone through school and been working in the
I have gone through this to change the color of my title bar. I'm
I have gone through this page... http://www.php.net/manual/en/function.hash.php MD5 is 32 characters long while sha1
I have gone through the fadein/fadeout tutorial that is on this page: For the
I have gone through many posts on SO regarding this issue: Tried everything in
I have gone through this and this , but the question I am asking
I am sure someone has gone over this but I have had no luck
Ok I am getting this FC error. I have gone over the code three
I have gone over the entire file including validating and sniffing out missing or
I've seen this question asked numerous times and have gone over my code over

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.