ios - NSFetchController fetch with other predicate when 1ste one fails -
i'm having fetchrequest. need "tracked" points database when there no tracked points need retrieved.
how can that? set predicate on fetchcontroller when doesn't find tracked points doesn't show anything. can hook on , retrieve everything...
is there way listen there nothing found or how done?
edit:
- (nsfetchedresultscontroller *)fetchedresultscontroller { if (_fetchedresultscontroller != nil) { return _fetchedresultscontroller; } nsfetchrequest *fetchrequest = [[nsfetchrequest alloc] init]; nsentitydescription *entity = [nsentitydescription entityforname:@"rank" inmanagedobjectcontext:[nsmanagedobjectcontext mr_defaultcontext]]; [fetchrequest setentity:entity]; /* nspredicate *predicate = [nspredicate predicatewithformat:@"name == %@", @"auto"]; [fetchrequest setpredicate:predicate];*/ nssortdescriptor *sort = [[nssortdescriptor alloc] initwithkey:@"name" ascending:no]; [fetchrequest setsortdescriptors:@[sort]]; [fetchrequest setfetchbatchsize:20]; nsfetchedresultscontroller *thefetchedresultscontroller = [[nsfetchedresultscontroller alloc] initwithfetchrequest:fetchrequest managedobjectcontext:[nsmanagedobjectcontext mr_defaultcontext] sectionnamekeypath:nil cachename:nil]; self.fetchedresultscontroller = thefetchedresultscontroller; _fetchedresultscontroller.delegate = self; return _fetchedresultscontroller; }
thanks larme solution:
- (nsfetchedresultscontroller *)fetchedresultscontroller { if (_fetchedresultscontroller != nil) { return _fetchedresultscontroller; } nsfetchrequest *fetchrequest = [[nsfetchrequest alloc] init]; nsentitydescription *entity = [nsentitydescription entityforname:@"rank" inmanagedobjectcontext:[nsmanagedobjectcontext mr_defaultcontext]]; [fetchrequest setentity:entity]; nspredicate *predicate = [nspredicate predicatewithformat:@"name == %@", @"don"]; [fetchrequest setpredicate:predicate]; nssortdescriptor *sort = [[nssortdescriptor alloc] initwithkey:@"name" ascending:no]; [fetchrequest setsortdescriptors:@[sort]]; [fetchrequest setfetchbatchsize:20]; nsfetchedresultscontroller *thefetchedresultscontroller = [[nsfetchedresultscontroller alloc] initwithfetchrequest:fetchrequest managedobjectcontext:[nsmanagedobjectcontext mr_defaultcontext] sectionnamekeypath:nil cachename:nil]; self.fetchedresultscontroller = thefetchedresultscontroller; _fetchedresultscontroller.delegate = self; if ([[_fetchedresultscontroller fetchedobjects] count] == 0) { nslog(@"0 found"); [fetchrequest setpredicate:nil]; } return _fetchedresultscontroller; }
Comments
Post a Comment