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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 1, 20262026-06-01T12:50:17+00:00 2026-06-01T12:50:17+00:00

Ok, lets start with the code. I am looping through a returned array of

  • 0

Ok, lets start with the code. I am looping through a returned array of dictionaries and creating (or updating) objects based on them. In this method I’m trying find or create a new entity. And then if the object is supposed to be deleted, I’d like to do so and not waste time updating it with the new information.

- (void)updateOrCreateObjectWith:(NSDictionary*)dictionary {
    RKManagedObjectStore *objectStore = ((MyAppDelegate*)[[UIApplication sharedApplication] delegate]).objectStore;

    id updateObject = (NSManagedObject*)[objectStore findOrCreateInstanceOfEntity:[resource entity] withPrimaryKeyAttribute:@"myID" andValue:[dictionary objectForKey:@"id"]];

    [updateObject setMyID:[dictionary objectForKey:@"id"]];

    // if marked for deletion, delete it now
    if ([[dictionary objectForKey:@"deleted_at"] isKindOfClass:[NSString class]]) {
        if ([updateObject isNew]){
            NSError *error = nil;
            [objectStore.managedObjectContext save:&error];
            if (error) {
                NSLog(@"error saving before delete: %@",error);
                return;
            }
//          [objectStore.managedObjectContext deleteObject:updateObject];
//          [objectStore.managedObjectCache delete:updateObject];  
        }
        else {
            [objectStore.managedObjectContext deleteObject:updateObject];            
        }
        return;
    }

    [updateObject updateWith:dictionary];
}

The part to be aware of is the deleted_at section with the (1) save section, (2) delete object from context, and (3) delete object from cache. I have tried a few combinations of those three but I don’t get the desired results.

If I delete it from the cache (just #3):

  • The objects get saved but they have no attributes.

If I delete it from the managed context (just #2) I get:

NSUnderlyingException=Cannot update object that was never inserted.

Since it was never inserted, I thought I’d save it and then delete it (#1 and #2), but then I get:

*** Terminating app due to uncaught exception 'NSObjectInaccessibleException', reason: 'CoreData could not fulfill a fault for '0xed27810 <x-coredata://4EE6AD5A-CC34-460A-A97A-0909454126A4/User/p166>''

So what is the proper way to remove a “new” object from NSMangedObjectContext?

  • 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-01T12:50:18+00:00Added an answer on June 1, 2026 at 12:50 pm

    It’s easier to get the RKManagedObjectStore instance with [[RKObjectManager sharedManager] objectStore] (assuming you want the shared one, which it seems like you do since you’re calling your app delegate).

    Check for the deleted_at key before you ever create an NSManagedObject. This code assumes you’re converting to type Resource, which is a subclass of NSManagedObject. It’s untested but should give you an idea of what you should be doing.

    - (void)updateOrCreateObjectWith:(NSDictionary*)dictionary {
    RKManagedObjectStore *objectStore = [[RKObjectManager sharedManager] objectStore];
    
        //get a reference to the object
        Resource *resource = [Resource findFirstByAttribute:@"myID" withValue:[dictionary objectForKey:@"id"]];
    
        //see if "deleted_at" exists in dictionary
        if ([[dictionary objectForKey:@"deleted_at"] isKindOfClass:[NSString class]])
        {
            //check to see if object exists in the context
            if(resource)
            {
                //if it exists, delete it
                [objectStore.managedObjectContext deleteObject:resource];
            }
        } else {
            //no "deleted at", so create the object
            if (!resource) {
                //resource is nil (it doesn't exist in the context), so we need to create it
                resource = [Resource object];
            }
            [resource updateWith:dictionary];
        }
    
        NSError *error = nil;
        [objectStore.managedObjectContext save:&error];
        if (error) {
            NSLog(@"error saving before delete: %@",error);
        }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

stuck on this. Lets start with code. Settings.php class settings{ public $db_host; public $db_username;
Lets start by saying that I am debugging someone else's code :-) The error
I am having troubles with this code. It's supposed to do this: Start the
Lets go from the start Created New Xcode project,opened interface builder dragged one NSButton
Let’s start with this statement: We have published couple of free applications on the
Let's first start with a code snippet to explain the issue: = Haml::Engine.new('#bar= yield').render
Let me explain my question by posing a hypothetical situation. Lets start with a
Let's start with this overload of List BinarySearch: public int BinarySearch(T item, IComparer<T> comparer);
Yes, this is a bit of a trick question; one array (without copies), as
Lets say i am running some code in dispatch async. .. is there a

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.