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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 7, 20262026-06-07T12:48:22+00:00 2026-06-07T12:48:22+00:00

I wrote an ios application in order to save a data to core data

  • 0

I wrote an ios application in order to save a data to core data and then fetch it with the following code:

   NSManagedObjectContext *context = [self managedObjectContext];
NSError *error;

NSManagedObject *failedBankInfo = [NSEntityDescription
                                   insertNewObjectForEntityForName:@"UserData"
                                   inManagedObjectContext:context];
[failedBankInfo setValue:[NSNumber numberWithInt:1] forKey:@"userId"];

NSFetchRequest *fetchRequest = [[NSFetchRequest alloc] init];
NSEntityDescription *entity = [NSEntityDescription
                               entityForName:@"UserData" inManagedObjectContext:context];
[fetchRequest setEntity:entity];
NSArray *fetchedObjects = [context executeFetchRequest:fetchRequest error:&error];
for (NSManagedObject *info in fetchedObjects) {
     NSLog(@"Name: %@", [info valueForKey:@"userId"]);

}

There no problem in here. But After I close my application and modify the setvalue to 2, I only receive the last data which is 2. Earlier data (1) gets deleted.

What should I do to keep the data entries even after I close my application.

Thank You!

  • 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-07T12:48:25+00:00Added an answer on June 7, 2026 at 12:48 pm

    You need to create a UIManagedDocument to save the Core Data information. I usually create the UIManagedDocument as a property of the class. For the example I have created a UIManagedDocument called theManagedDocument:

    NSURL *url = [[[NSFileManager defaultManager] URLsForDirectory:NSDocumentDirectory inDomains:NSUserDomainMask] lastObject];
    url = [url URLByAppendingPathComponent:@"DocumentName"];
    
    self.theManagedDocument = [[UIManagedDocument alloc] initWithFileURL:url];
    
    if([[NSFileManager defaultManager] fileExistsAtPath:[url path]])
    {
        [theManagedDocument openWithCompletionHandler:^(BOOL success){
            if(success) [self documentIsReady];
            if(!success) NSLog(@"Couldn't Open Document");
        }];
    }
    else 
    {
        [theManagedDocument saveToURL:url forSaveOperation:UIDocumentSaveForCreating completionHandler:^(BOOL success){
            if(success) [self documentIsReady];
            if(!success) NSLog(@"Couldn't Create Document");
        }];
    }
    

    Then I create a method called documentIsReady that is called when the document has been successfully created or opened (opened if it is already present, created otherwise). I will also keep the context as a property. Here it is called context. I also added in your code:

    - (void) documentIsReady
    {
        if(self.theManagedDocument.documentState == UIDocumentStateNormal) 
        {
            self.context = self.theManagedDocument.managedObjectContext;
            NSError *error;
    
            NSManagedObject *failedBankInfo = [NSEntityDescription
                                   insertNewObjectForEntityForName:@"UserData"
                                   inManagedObjectContext:self.context];
            [failedBankInfo setValue:[NSNumber numberWithInt:1] forKey:@"userId"];
    
            NSFetchRequest *fetchRequest = [[NSFetchRequest alloc] init];
            NSEntityDescription *entity = [NSEntityDescription
                               entityForName:@"UserData" inManagedObjectContext:self.context];
            [fetchRequest setEntity:entity];
            NSArray *fetchedObjects = [self.context executeFetchRequest:fetchRequest error:&error];
            for (NSManagedObject *info in fetchedObjects) {
                NSLog(@"Name: %@", [info valueForKey:@"userId"]);
            }
        }
    }
    

    You will then want to close the document when you are done using:

    [self.theManagedDocument closeWithCompletionHandler:^(BOOL success){
        if(success) NSLog(@"Closed Successfully");
        if(!success) NSLog(@"Error Closing Document");
    }];
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm a newbie iOS developer. I wrote a small application that save an NSMutableArray
I am working on a Core Data based iOS application , in which i
I wrote this code in my viewDidLoad function in my iOS project. lettersLeftLabel.text =
I have the following error on a Ios 5 application using ARC: *** -[ViewDettaglio
I'm building and iOS application which fetches data from my MYSQL database, so to
Hi I am using following code in the MAC application for setting font of
When the application terminates, I write data on a database. As from iOS 4
I have a couple of Android and iOS native mobile application that I wrote
i wrote an application on the Xcode 4.2 using iOS SDK 5.0. i have
I'm trying to write an iOS application that'll get data from a web server

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.