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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 2, 20262026-06-02T04:54:27+00:00 2026-06-02T04:54:27+00:00

Overview I have an iOS project in which I am using Core data I

  • 0

Overview

  • I have an iOS project in which I am using Core data
  • I am inserting an object, then I want to save it.
  • I am not sure if save works.
  • Save seems to be working when app goes into background
  • When using Simulator, If I click on Stop button on Xcode, save doesn’t seem to be working.

Question

  1. Is the save actually happening ?
  2. Am I facing a problem because I created a view based app (the core data checkbox was not available) ?

Steps Followed

  • I am using the simulator to test it.
  • Insert an object (code is in the next section)
  • Save the inserted object (code is in the next section)
  • I press the Stop button on Xcode to stop running the app

Output noticed

setBeforeSave.count = 1
setAfterSave.count = 0
  • Before saving, The NSManagedObjectContext method insertedObjects returns 1 object
  • Before saving, The NSManagedObjectContext method insertedObjects returns 0 objects
  • When Xcode Stop button is pressed, and when the app is relaunched, the previous data is not available (is it because I clicked on stop on xcode)
  • managedObjectContext is NOT nil
  • The NSManagedObjectContext method save: returns YES.

Code to Insert Object

Test *test = [NSEntityDescription insertNewObjectForEntityForName:@"Test" inManagedObjectContext:self.database.managedObjectContext];

Code to Save:

//database is a property of the type UIManagedDocument

NSSet *setBeforeSave = [self.database.managedObjectContext insertedObjects];
NSLog(@"setBeforeSave.count = %i", setBeforeSave.count);

NSError *error = nil;

if(![self.database.managedObjectContext save:&error])
    NSLog(@"error = %@", error);    

NSSet *setAfterSave = [self.database.managedObjectContext insertedObjects];
NSLog(@"setAfterSave.count = %i", setAfterSave.count);
  • 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-02T04:54:29+00:00Added an answer on June 2, 2026 at 4:54 am

    According to the UIManagedDocument documentation, you should not call save on either of the internal managed contexts. Instead, if you want data saved, you should do one of two things.

    1. Use the undoManager, as it will mark the context dirty, and ready to be saved.

    2. Call [document updateChangeCount:UIDocumentChangeDone];

    Thus, in your case, you should replace that save call with:

    [self.database updateChangeCount:UIDocumentChangeDone];
    

    And your data will get saved.

    EDIT

    To provide additional detail. A UIManagedDocument has two MOCs., in a parent/child relationship. The child is the one you get when calling document.managedObjectContext. Now, when a NSManagedObjectContext has a parent, the normal way to propagate changes to the parent is to call save:. However, the UIManagedDocuememt does other stuff, and its documentation specifically says NOT to call save on either the parent or child context.

    Well, how does stuff get saved, then? Well, you tell the UIManagedDocument that it is “dirty” and needs to be saved. The two ways you can do that are by either using the undoManager, or calling updateChangeCount:.

    When doing either of those, the internals of UIManagedDocument will make sure that the parent context is notified of the change. At some point in the future, the parent will effect the change to the actual backing store (i.e., file(s) on disk).

    Furthermore, when a context is “saved” it may or may not keep references to the objects that were changed. You can set a property which tells it to retain objects that have been saved, or to release them.

    Hopefully, that addresses your problems.

    to summarize, though, see the original answer.

    BTW, you can actually see a log of what the SQL store is doing underneath by adding “-com.apple.CoreData.SQLDebug 1” to your command line arguments. You do that in the “Edit Scheme” dialog.

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

Sidebar

Related Questions

Overview I have an iOS project which contains 2 navigation controllers as shown in
Overview I have a iOS project in which the view controller implementation which has
Overview I have an iOS project in which I store a list of names
Overview I am doing an iOS project using the MVC architecture. Pardon my ignorance,
Overview I have an iOS project with a table view with the following specification:
I have a overview page containing a list with some links from which multiple
In Eclipse we are using Mylyn to have an overview of our Trac Tickets.
Overview: I have an Expandable List View which displays items of the type ToDoElement
I want to make #scrollbox clickable/dragable within #overview, and have it control the scroll
I have a overview page that shows the data in a table. A pop-up

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.