I try to invoke this method for one time ever the apps is install and launch for the first time.
My idea is to fill the core data table with my data.
How to auto release this method in viewDidLoad?
- (void)insertNewObject:(id)sender
{
if ((firstRun!=YES)) {
NSLog(@"The data was already Added");
}
else {
NSManagedObjectContext *context = [self.fetchedResultsController managedObjectContext];
NSEntityDescription *entity = [[self.fetchedResultsController fetchRequest] entity];
NSManagedObject *newManagedObject = [NSEntityDescription insertNewObjectForEntityForName:[entity name] inManagedObjectContext:context];
[newManagedObject setValue:[[NSString alloc]initWithFormat:@"new"] forKey:@"name"];
firstRun = NO;
// Save the context.
NSError *error = nil;
if (![context save:&error]) {
NSLog(@"Unresolved error %@, %@", error, [error userInfo]);
abort();
}
}
}
You can use
NSUserDefaultsto verify if it’s the first run.You can call this method in your
viewWillDisappearorviewDidLoadwith[self insertNewObject:nil];