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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T22:31:24+00:00 2026-06-13T22:31:24+00:00

I have a CoreData Entity (called MyData) that has Attributes that relate to what

  • 0

I have a CoreData Entity (called “MyData”) that has Attributes that relate to what the user has entered in a variety of UIControls on one of my UIViewControllers, plus one Attribute (“recordKey”) that is a unique identifier for each record. When the user opens the page, I want to check to see if I have a record already for that recordKey. If so, then load it and set all the controls based on what was in that record. If not, set all the controls based off of a “default” record and create a new record for this session, then save the new session when the user exits, but leave the “default” record unchanged. My code (in viewDidLoad) so far is:

MyAppDelegate *app = [[UIApplication sharedApplication] delegate];
NSString *table = @"MyData";
NSError *error;
NSManagedObjectContext *context = [app managedObjectContext];
NSEntityDescription *entity = [NSEntityDescription entityForName:table inManagedObjectContext:context];
NSPredicate *predicate = [NSPredicate predicateWithFormat:@"recordKey LIKE %@", self.recordKey];
NSFetchRequest *request = [[NSFetchRequest alloc] init];
[request setPredicate:predicate];
[request setEntity:entity];

NSArray *arr = [context executeFetchRequest:request error:&error];
if (arr.count == 0)
{
    // No entries exist for this recordKey, so use default values
    predicate = [NSPredicate predicateWithFormat:@"recordKey LIKE %@", @"DEFAULT"];
    [request setPredicate:predicate];
    NSArray *defaultArr = [context executeFetchRequest:request error:&error];
    if (defaultArr.count == 1)
    {
        MyData *defaultData = [defaultArr objectAtIndex:0]; //create placeholder for for default data
        currentData = [NSEntityDescription insertNewObjectForEntityForName:table inManagedObjectContext:context]; //create a new row to correspond to this session
        currentData = defaultData; //preload with default values
        currentData.recordKey = self.recordKey; //set record key for this session to it's unique identifier
    }
    else
    {
        NSLog(@"Error - DEFAULT record not found in table '%@', or multiple DEFAULT records found, in viewDidLoad on RunwayAnalysisController.m",table);
    }
}
else if (arr.count == 1)
{
    // Record found for this recordKey, so set it as the current MyData object
    currentData = [arr objectAtIndex:0];
}

Then when I leave my controller, I just

if (![context save:&error])
{
    NSLog(@"Error writing entry to %@ table in saveClick on RunwayAnalysisController.m",table);
}

When I run this, my default records are being overwritten by any changes the user makes during this session. I think the problem is currentData = [NSEntityDescription insertNewObjectForEntityForName:table inManagedObjectContext:context]; currentData = defaultData; isn’t doing what I want it to – create a new entry, load that entry with the information in the default record, then sever the tie with the default record and be on it’s own to be filled as the user interacts with the controls on the page. I’m probably misunderstanding what exactly one of these two lines is doing. Does anyone see what I’m doing wrong here, and suggest how to fix it?

  • 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-13T22:31:25+00:00Added an answer on June 13, 2026 at 10:31 pm

    AFAIK you cannot simply fetch a CoreData object and re-insert it into the managedObjectContext. If you want to have a copy, create a new managedObject and set your default values.

    In Your ObjectModel you can even set default values to an Entity … this would be an even better way I guess. You then just have to create a new managedObject and modify what needs to be modified. No copying needed. (as long as your default model does not change.)

    The way you create a managedObject looks good to me

    MyData *defaultData = [defaultArr objectAtIndex:0];
    currentData = [NSEntityDescription insertNewObjectForEntityForName:table inManagedObjectContext:context];
    

    But then, by setting currentData = defaultData; you overwrite your previous object. you should do:

    currentData.var1 = defaultData.var1;
    currentData.var2 = defaultData.var2;
    ...
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have an entity in core data that has 2 Attributes. One that is
I have a simple core-data entity that has a Boolean attribute called subscribedToNewsletter .
I have a simple Coredata model with one entity called conversation and the other
In my core data model I have a Person entity that has a to
I have a core data entity called TruckNumber which has a string as it's
I have a CoreData NSManagedObject subclass, TextNarration, that has an attribute of type NSString,
I have an entity called 'SyncInfo' (including a UUID and a modified date) that
I'm wanting to make a CoreData entity called Employees, some Employees could have a
I have a core data model with an entity called clients that is made
I have an entity called Person and a set of UIImages for it (One-To-Many).

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.