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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 29, 20262026-05-29T18:15:20+00:00 2026-05-29T18:15:20+00:00

I new to iOS development and doing some test with the core data. It

  • 0

I new to iOS development and doing some test with the core data. It was all fine until I started playing with relationship.
In context:

I have 2 entity: Article and Category.

Article has two members idArticle and text. Articles has to have a single Category.
Category has two members idCategory and name. Category can have 0 or several Articles.

Article:

  • idArticle
  • text
  • category (Relationship to Category, Inverse = articles, minimum optional, maximum 1)

Category:

  • idCategory
  • name
  • articles (To many Relationship to Article, Inverse = category, minimum optional, maximum unlimited)

While I am adding an article. I am first surprise to not only have article1.category but also article1.idCategory and article1.name!
I have currently set all my attributes, relationship to optional.
Whatever I do, when I add a new article using the code below, it will add a new Category as well which will contain idCategory = 0 and name = nil if I dont set article.idCategory and article.name! or to the corresponding value if I set them. However, I don’t want it to create that category, I just want to add an existing category.
article.category works fine; it add the article to the right category! If I only set article.category; article.idCategory will = 0 and article.name = nil.

I suppose that I could delete the newly created category but I want my code to be neat. I have search the web but didnt find similar example with that problem. I am not dealing with any GUI here.
My code:

 - (BOOL)createNewGmtArticle:(NSNumber*)articleID title:(NSString*)paramTitle text:(NSString*)paramText date:(NSDate*)paramDate categoryID:(NSNumber*)paramCategoryID categoryName:(NSString*)paramCategoryName

{
    GasMattersTodayArticles *gmtArticle = [NSEntityDescription insertNewObjectForEntityForName:@"GasMattersTodayArticles" inManagedObjectContext:self.managedObjectContext]; // Look the given entitiy GasMattersTodayArticles in the given managed obj context

    if(gmtArticle != nil)
    {
        // Fill article
        gmtArticle.idArticle = articleID;
        gmtArticle.text = paramText;

        gmtArticle.category = [self getGmtCategoryWithId:paramCategoryID];
        //gmtArticle.idCategory = gmtArticle.category.idCategory; 
        //gmtArticle.name = gmtArticle.category.name;        

        NSError *savingError = nil;
        if([self.managedObjectContext save:&savingError]) // flush all unsaved data of the context to the persistent store
        {
            NSLog(@"Successfully saved the context");
            return YES;
        }
        else
        {
            NSLog(@"Failed to save the context. Error = %@", savingError);
            return NO;
        }
    }

    NSLog(@"Failed to create the new article");
    return NO;
}

and

-(GasMattersTodayCategory *)getGmtCategoryWithId:(NSNumber*)categoryID

{
    // Create the fetch request first
    NSDictionary *subs = [NSDictionary dictionaryWithObject:categoryID forKey:@"SEARCH_KEY"];    
    NSFetchRequest *fetchRequest = [self.managedObjectModel fetchRequestFromTemplateWithName:@"CategoryWithKey" substitutionVariables:subs];
    // Entity whose contents we want to read
    NSEntityDescription *entity = [NSEntityDescription entityForName:@"GasMattersTodayCategory" inManagedObjectContext:self.managedObjectContext];

    // Tell the request that we want to read the content of the person entity
    [fetchRequest setEntity:entity];

    // Excecute the fetch request on the context
    NSError* requestError = nil;
    GasMattersTodayCategory *category = [[self.managedObjectContext executeFetchRequest:fetchRequest error:&requestError] lastObject];

    // Make sur we get a category
    if(category != nil)
    {
        return category;
    }
    else
    {
        NSLog(@"Could not find any Category entities with this Id in the context.");
        return nil;
    }    
}

Thanks! This super basic task is currently ruining my sunday!

  • 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-29T18:15:27+00:00Added an answer on May 29, 2026 at 6:15 pm

    The most likely way this could have happened is if your Article Entity is a sub-class of Category.

    You would get exceptions otherwise when setting gmtArticle.idCategory telling you that GasMattersTodayArticles is not key-value compliant to idCategory.

    GasMattersTodayArticles and GasMattersTodayCategory should both be sub-classes of NSManagedObject (or maybe a base class for your project).

    You are creating a new category when you create an article because according to your structure an Article is-a Category.

    However without seeing the .xcdatamodel this answer is a guess.

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

Sidebar

Related Questions

So I'm new to iOS development and am doing all I can to learn
I am new to ios development and doing development in ios4.0.1 and xcode 3.2.3.
I'm new to iOS development. I've read through some of the source code I've
I'm new to iOS development and I was wondering if it is at all
Hi All I'm new to iOS development and am currently having an issue with
I am new to iOS development. How to save data (say user information) over
I am new to OpenGL ES. I was doing iOS development. Now I actually
New to ios development, looking for some guides, Can I develop ios application on
I am new in ios development. I am doing a photo cropper app. I
i am new to ios development and the concept of views looks confusing to

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.