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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 20, 20262026-05-20T07:32:48+00:00 2026-05-20T07:32:48+00:00

I have been trying to find a good CoreData tutorial that focuses on more

  • 0

I have been trying to find a good CoreData tutorial that focuses on more advanced databases that have one-to-many and many-to-many relationships. From what I’ve read, CoreData hides the middle table that would normally be in a many-to-many relationship. But my question is more about how to set the one-to-many relationships using CoreData.

A portion of my database consists of a Category table and an Item table. One category can have many items.

In my program I get to the point where the user Selects a category, then a list view pops up showing all the items that should be in that category.

I can add an item to the table perfectly fine, it’s when I try to add an item to a table with a relationship to a category when problems/crashes arise.

Should the item be completely saved to the database first, then regrab it and set the relationship?

Some code:

[item.m]

@interface ITEM :  NSManagedObject  
{
}

@property (nonatomic, retain) NSString * name;
@property (nonatomic, retain) CATEGORY * category;

@end

[/item.m]

[category.m]

@interface CATEGORY :  NSManagedObject  
{
}

@property (nonatomic, retain) NSString * name;
@property (nonatomic, retain) NSSet* items;

@end


@interface CATEGORY (CoreDataGeneratedAccessors)
- (void)addItemObject:(item *)value;
- (void)removeItemObject:(item *)value;
- (void)addItems:(NSSet *)value;
- (void)removeItems:(NSSet *)value;

@end

[/category.m]

None of the CoreDataGeneratedAccessors are implemented by me, I assume they are already created and hidden.

I tried setting the relationship both ways, once from CATEGORY and once from ITEM
i.e.
[[self category] addItemObject:[self item]];
and also
[[self item] setCategory:[self category]];

Neither worked… I got an error like this:

*** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: 'Illegal attempt to establish a relationship 'category' between objects in different contexts (source = <NSManagedObject: 0x614b660> (entity: item; id: 0x61558f0 <x-coredata:///item/tF691713B-18B1-4D28-93CC-40327353C08F3> ; data: {
    category = nil;
    name = TestName;

}) , destination = <CATEGORY: 0x4d55c20> (entity: category; id: 0x4d55050 <x-coredata://188D1CD9-6CC0-47EF-9C2A-A5DDDB1FAA24/category/p1> ; data: {
    items = "<relationship fault: 0x4da4800 'items'>";
    name = "Test Category";
}))'

So I guess the question would be: where am I going wrong, and is there a really good tutorial that walks through CoreData examples that have more than one table and actually have a one-to-many relationship (and what is the link)?

  • 1 1 Answer
  • 3 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-20T07:32:48+00:00Added an answer on May 20, 2026 at 7:32 am

    ‘Illegal attempt to establish a relationship ‘category’ between objects in different contexts

    There is your problem. You can’t have relationships between different contexts.

    You can either refactor your code so that it uses only one NSManagedObjectContext or merge the item into the “main context” before you assign a category to it.

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

Sidebar

Related Questions

I have been trying to find a good guide to create a templated control.
I have been trying to find a good-looking design using Aero in Delphi 2010.
I've been trying to find a good architecture for one application for the last
I've been trying to troubleshoot this, but can't find a good function that will
Good Evening, I have been trying to implement a simple Android(tm) application that utilizes
I have been trying to find a collision avoidance example that I can adapt
I have been trying to find a book or web reference that gives me
I've been trying to find a good video that I can show at a
I have been trying to find a good definition, and get an understanding, of
I have been trying to find some more information about the next Microsoft Dynamics

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.