I'm entirly new to creating iOS apps. I have to quickly create a form-application where I can store information from people willing to fill it in. Basicly just a bunch of text-fields for stuff like name, mail etc.
Once the form is filled, I'm storing their data with this bit of code:
//Save action
NSEntityDescription *entityDesc = [NSEntityDescription entityForName:@"Person" inManagedObjectContext: context];
NSManagedObject *newPerson = [[NSManagedObject alloc]initWithEntity:entityDesc insertIntoManagedObjectContext:context];
//Fill in values
[newPerson setValue:self.btnPrefix.titleLabel.text forKey: @"prefix"];
[newPerson setValue:self.txtFirstName.text forKey: @"firstname"];
[newPerson setValue:self.txtLastName.text forKey: @"lastname"];
[newPerson setValue:self.txtLive.text forKey: @"country"];
[newPerson setValue:self.txtMail.text forKey: @"email"];
[newPerson setValue:self.txtPhone.text forKey: @"phonenumber"];
[newPerson setValue:self.txtLinked.text forKey: @"linkedIn"];
[newPerson setValue:self.txtAbout.text forKey: @"about"];
NSError *error;
[context save:&error];
When executed on the simulator no problem at all. But once ran on the iPad I get this error:
Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '+entityForName: nil is not a legal NSManagedObjectContext parameter searching for entity name 'Person''
After debugging it gets triggerd on the first line:
NSEntityDescription *entityDesc = [NSEntityDescription entityForName:@"Person" inManagedObjectContext: context];
After a bit more research, my Appdelegate contains a nil persistentContainer when ran on device, but it's filled when ran on virtual device. So I guess the problem is there, but I can't find a way to solve it.
- (void)viewDidLoad {
[super viewDidLoad];
AppDelegate *appdelegate = (AppDelegate*)[[UIApplication sharedApplication]delegate];
context = appdelegate.persistentContainer.viewContext;
}
Can anyone help me out?
context
parameter isnil
, which must not be the case. How do you get the managed object context object you're passing in there? And I assume you have ensured that your model does contain an Entity with the name "Person", right? – GeroAppDelegate *appdelegate = (AppDelegate*)[[UIApplication sharedApplication]delegate]; context = appdelegate.persistentContainer.viewContext;
. And yes I have a model that contains the entity Person. As I said it works on the simulator so I imagine there's just a thing missing to make it work on a physical device? – AkornapersistentContainer
method your app delegate has by default? It sounds like you're either trying to initiate a container in a location you don't have write access to on the device (the simulator usually has more write permissions in various places) or you're facing a racing condition on device (your view controller tries to access the container and context before theloadPersistentStoresWithCompletionHandler:
method is done). – Gero