I hope everything will be fine with you.
I am new to iPhone development. I am creating a simple application using basic data. During the application, when I save the data, it works fine, and then I retrieve the data, it also works fine. But when I restart the application, all data is lost.
When I run the application in the ViewDidLoad function, I get data with the same function that I retrieve during the working application.
Saving data method:
NSManagedObjectContext *context=[app managedObjectContext]; Contacts *data=[NSEntityDescription insertNewObjectForEntityForName:@"Contacts" inManagedObjectContext:context]; if(nameField.text.length <=0 || phoneField.text.length <=0 ) { UIAlertView *alert=[[UIAlertView alloc]initWithTitle:@"Warning!" message:@"Please enter some data." delegate:self cancelButtonTitle:@"Ok" otherButtonTitles: nil]; [alert show]; } else { data.name = nameField.text; data.phone = phoneField.text; NSLog(data.name); NSLog(data.phone); [self.navigationController popToRootViewControllerAnimated:YES]; }
Extract data method:
NSEntityDescription *entity=[NSEntityDescription entityForName:@"Database" inManagedObjectContext:context]; NSFetchRequest *fetchRequest=[[NSFetchRequest alloc]init]; [fetchRequest setFetchBatchSize:20]; [fetchRequest setEntity:entity]; NSSortDescriptor *sorting = [[NSSortDescriptor alloc] initWithKey:@"name" ascending:YES]; NSArray *sorted_Array=[NSArray arrayWithObject:sorting]; [fetchRequest setSortDescriptors:sorted_Array]; NSError *error; NSMutableArray *tArray=[[context executeFetchRequest:fetchRequest error:&error]mutableCopy]; [self setArray:tArray]; [self.tableView reloadData];
Application Delegation Code
#import "ZAppDelegate.h"
Is something wrong here? I hope to get good answers as early as possible.
source share