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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 30, 20262026-05-30T01:01:19+00:00 2026-05-30T01:01:19+00:00

I got problem with core data, I had a table that exactly look like

  • 0

I got problem with core data, I had a table that exactly look like this

User (relationship with) Rack (relationship with) RackItem

How should I insert new object to rackItem with existing item object in User and existing Rack object

I try fetch the existing object

predicate = 
        [NSPredicate predicateWithFormat:@"userID = %@ AND rack.rackID = %@", 
                                        _userID, 
                                        @"1"];
entityName = @"RackItem";
NSArray *result = [self fetchDataWithEntity:entityName predicate:predicate];
id object = [result last object];

then on the setRackItem part I create a new EntityDescription and save it to core data using this function

- (id)objectInManagedObjectContextForDictionary:(NSDictionary *)dictionary 
                                     entity:(NSString *)entityName 
                              managedObject:(id)object 
                                   insertKey:(NSArray *)keys 
                                   relation:(BOOL)isRelation {
// Recursive method
for (id key in dictionary) {
    id value = [dictionary objectForKey:key];
    NSString *camelCase = [key stringByReplacingCharactersInRange:NSMakeRange(0,1) 
                                                       withString:[[key substringToIndex:1] uppercaseString]];
    if ([value isKindOfClass:[NSDictionary class]] && isRelation) {   
        if ([keys containsObject:camelCase] && keys != nil) {
            id newObject = 
                [NSEntityDescription insertNewObjectForEntityForName:camelCase 
                                              inManagedObjectContext:_managedObjectContext];
                value = [self objectInManagedObjectContextForDictionary:[dictionary objectForKey:key] 
                                                                 entity:camelCase 
                                                          managedObject:newObject
                                                              insertKey:keys 
                                                               relation:isRelation];
        } else {
            SEL selector = NSSelectorFromString(key);
            id newObject = [object performSelector:selector];
            value = [self objectInManagedObjectContextForDictionary:[dictionary objectForKey:key] 
                                                             entity:camelCase 
                                                      managedObject:newObject
                                                          insertKey:keys 
                                                           relation:isRelation];
        }
    }
    NSString *methodName = [NSString stringWithFormat:@"set%@:", camelCase];
    SEL selector = NSSelectorFromString(methodName);
    [object performSelector:selector withObject:value];
}
return object;
}

And the result is good, data was inserted successfully. But the existing object got modified.
I think this is the problem of
id object = [result lastObject];
need to be retain. Anyone can help me?

Some Update
The result after followed insert

Before Insert

 1|9|2|0|0||||248|52|||1|||||||
 2|9|2|0|0|||||||||||||||
 3|9|2|0|0|||||||||||||||
 4|9|2|0|0|||||||||||||||
 5|9|1|0|0|1||||||||||||||
 sqlite> select *from zrackitem;

After Insert

 1|9|2|0|0||||248|52|||1|||||||
 2|9|2|0|0|||||||||||||||
 3|9|2|0|0|||||||||||||||
 4|9|2|0|0|||||||||||||||
 5|9|2|0|0|||||||||||||||
 6|9|1|0|0|1||||||||||||||
 sqlite> select *from zrackitem; 

it shows that every time I insert the existing row of zrack variable is gone.

  • 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-30T01:01:22+00:00Added an answer on May 30, 2026 at 1:01 am

    This seems a little confusing …
    first of all you should have of course as NeverBe said a to-many relationship between Rack and RackItem

    Then it seems to me that you’re having a conceptual issue here.
    If you want to insert a new object related to the same Rack than a previous RackItem … just fetch that Rack Object and insert your new RackItem by setting it’s rack property to the Rack you just fetched.

    You really should keep it simple or you’re just gonna have a lot of Coredata inconstancy issues
    Hope that this will help

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

Sidebar

Related Questions

I'm dealing with an annoying problem in core data I've got a table named
I got a problem like this (this is html/css menu): Eshop | Another eshop
I have 3 entities like this: sorry that this image has a problem that
I've got about 5000-7000 objects in my core data store that I want to
In my Core Data model, I've got a relationship called listItems which links to
I am new to Core Data and i got stuck with a problem. I
Really weird problem. I have a tableView that pulls data out of a Core
Got a problem with a query I'm trying to write. I have a table
I got a problem with this linq query: from PersistedFileInfo fi in m_Database from
I got a problem today. It had a method and I need to find

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.