ios - Core Data - NSInvalidArgumentException -
there's not say, have done application core data went fine.
this application giving weird error.
*** terminating app due uncaught exception 'nsinvalidargumentexception', reason: '+entityforname: nil not legal nsmanagedobjectcontext parameter searching entity name 'ingredient'' *** first throw call stack:(0x1faa012 0x13e7e7e 0x10edf57 0x1134052 0x3211 0x11a04b 0x2fe2 0x22c1 0x19157 0x19747 0x1a94b 0x2bcb5 0x2cbeb 0x1e698 0x1f05df9 0x1f05ad0 0x1f1fbf5 0x1f1f962 0x1f50bb6 0x1f4ff44 0x1f4fe1b 0x1a17a 0x1bffc 0x20dd 0x2005) vlibc++abi.dylib: terminate called throwing exception i tried simplify app using app delegate , mainviewcontroller, in mainviewcontroller tried add pbject coredata see if everithing working. model
app delegate
- (bool)application:(uiapplication *)application didfinishlaunchingwithoptions:(nsdictionary *)launchoptions { self.window = [[uiwindow alloc] initwithframe:[[uiscreen mainscreen] bounds]]; masterviewcontroller *masterviewcontroller = [[masterviewcontroller alloc]initwithnibname:@"masterviewcontroller" bundle:nil]; masterviewcontroller.managedobjectcontext = self.managedobjectcontext; [self.window setrootviewcontroller:masterviewcontroller]; self.window.backgroundcolor = [uicolor whitecolor]; [self.window makekeyandvisible]; return yes; } - (nsmanagedobjectcontext *)managedobjectcontext { if (_managedobjectcontext != nil) { return _managedobjectcontext; } nspersistentstorecoordinator *coordinator = [self persistentstorecoordinator]; if (coordinator != nil) { _managedobjectcontext = [[nsmanagedobjectcontext alloc] init]; [_managedobjectcontext setpersistentstorecoordinator:coordinator]; } return _managedobjectcontext; } - (nsmanagedobjectmodel *)managedobjectmodel { if (_managedobjectmodel != nil) { return _managedobjectmodel; } nsurl *modelurl = [[nsbundle mainbundle] urlforresource:@"pizzacoredata" withextension:@"momd"]; _managedobjectmodel = [[nsmanagedobjectmodel alloc] initwithcontentsofurl:modelurl]; return _managedobjectmodel; } - (nspersistentstorecoordinator *)persistentstorecoordinator { if (_persistentstorecoordinator != nil) { return _persistentstorecoordinator; } nsurl *storeurl = [[self applicationdocumentsdirectory] urlbyappendingpathcomponent:@"pizzacoredata.sqlite"]; nserror *error = nil; _persistentstorecoordinator = [[nspersistentstorecoordinator alloc] initwithmanagedobjectmodel:[self managedobjectmodel]]; if (![_persistentstorecoordinator addpersistentstorewithtype:nssqlitestoretype configuration:nil url:storeurl options:nil error:&error]) { /* replace implementation code handle error appropriately. abort() causes application generate crash log , terminate. should not use function in shipping application, although may useful during development. typical reasons error here include: * persistent store not accessible; * schema persistent store incompatible current managed object model. check error message determine actual problem was. if persistent store not accessible, there typically wrong file path. often, file url pointing application's resources directory instead of writeable directory. if encounter schema incompatibility errors during development, can reduce frequency by: * deleting existing store: [[nsfilemanager defaultmanager] removeitematurl:storeurl error:nil] * performing automatic lightweight migration passing following dictionary options parameter: @{nsmigratepersistentstoresautomaticallyoption:@yes, nsinfermappingmodelautomaticallyoption:@yes} lightweight migration work limited set of schema changes; consult "core data model versioning , data migration programming guide" details. */ nslog(@"unresolved error %@, %@", error, [error userinfo]); abort(); } return _persistentstorecoordinator; } - (nsurl *)applicationdocumentsdirectory { return [[[nsfilemanager defaultmanager] urlsfordirectory:nsdocumentdirectory indomains:nsuserdomainmask] lastobject]; } mainviewcontroller
- (void)viewdidload { [super viewdidload]; tableingredientsviewcontroller *tableingredientvc = [[tableingredientsviewcontroller alloc]init]; tablepizzasviewcontroller *tablepizzavc = [[tablepizzasviewcontroller alloc]init]; nsmanagedobject *newpizza = [nsentitydescription insertnewobjectforentityforname:@"ingredient" inmanagedobjectcontext:_managedobjectcontext]; [newpizza setvalue:@"timida" forkey:@"name"]; nserror *error; if (![_managedobjectcontext save:&error]){ nslog(@"error %@ %@", error, [error userinfo]); } uinavigationcontroller *ingredientnavcontroller = [[uinavigationcontroller alloc]initwithrootviewcontroller:tableingredientvc]; uinavigationcontroller *pizzanavcontroller = [[uinavigationcontroller alloc]initwithrootviewcontroller:tablepizzavc]; [self setviewcontrollers:@[pizzanavcontroller, ingredientnavcontroller]]; } this screenshot of model: http://tinypic.com/view.php?pic=97nrcn&s=5
i have updated appdelegate, forgot copy last methods
your _managedobjectcontext nil when doing
nsmanagedobject *newpizza = [nsentitydescription insertnewobjectforentityforname:@"ingredient" inmanagedobjectcontext:_managedobjectcontext]; so before passing context view controller make sure initialized properly.
Comments
Post a Comment