ios - Core Data - NSManagedObjectContext returns nil in Master-Detail Application -
i'm working on alarm clock app, use core data stack apple's master-detail application template, worked fine core data, until i'm calling in - (void)applicationdidenterbackground:(uiapplication *)application
method masterviewcontroller work during app in background , should tableview [self.tableview reloaddata]
.
and point error
+entityforname: nil not legal nsmanagedobjectcontext parameter searching entity name 'myentity'
everything works fine when app running when tableview should reload when application in background error. knows how solve problem?
in appdelegate.m
- (nsmanagedobjectcontext *)managedobjectcontext { if (_managedobjectcontext != nil) return _managedobjectcontext; nspersistentstorecoordinator *coordinator = [self persistentstorecoordinator]; if (coordinator != nil) { _managedobjectcontext = [[nsmanagedobjectcontext alloc] init]; [_managedobjectcontext setpersistentstorecoordinator:coordinator]; } return _managedobjectcontext; }
i have code in applicationdidfinishlaunchingwithoptions method:
//some other stuff before... wkmasterviewcontroller *controller = (wkmasterviewcontroller *)navigationcontroller.topviewcontroller; controller.managedobjectcontext = self.managedobjectcontext;
in masterviewcontroller.m have following code:
- (nsfetchedresultscontroller *)fetchedresultscontroller { if (_fetchedresultscontroller != nil) { return _fetchedresultscontroller; } nsfetchrequest *fetchrequest = [[nsfetchrequest alloc] init]; nsentitydescription *entity = [nsentitydescription entityforname:@"myentity" inmanagedobjectcontext:self.managedobjectcontext]; [fetchrequest setentity:entity]; [fetchrequest setfetchbatchsize:20]; nssortdescriptor *sortdescriptor = [[nssortdescriptor alloc] initwithkey:@"zeit" ascending:no]; nsarray *sortdescriptors = @[sortdescriptor]; [fetchrequest setsortdescriptors:sortdescriptors]; nsfetchedresultscontroller *afetchedresultscontroller = [[nsfetchedresultscontroller alloc] initwithfetchrequest:fetchrequest managedobjectcontext:self.managedobjectcontext sectionnamekeypath:nil cachename:@"master"]; afetchedresultscontroller.delegate = self; self.fetchedresultscontroller = afetchedresultscontroller; nserror *error = nil; if (![self.fetchedresultscontroller performfetch:&error]) { nslog(@"unresolved error %@, %@", error, [error userinfo]); abort(); } return _fetchedresultscontroller; }
thanks!!
check-> function
- (nsmanagedobjectcontext *)managedobjectcontext {}
is returning nil _managedobjectcontext
?
if variable context nil. means managedobjectcontext method not working correctly.
Comments
Post a Comment