iphone - CoreData Querying -
i have created coredata model , defined entities , attributes. have created class(named dbinterface) of nsobject conforms <nsfetchedresultscontrollerdelegate>
, added next objects
nsfetchedresultscontroller *fetchedresultscontroller; nsmanagedobjectcontext *managedobjectcontext;
here lost. how proceed info sqlite3 file created?
my query, if thats right term use, needs select records 1 of entities based on condition. not know how write query or of sort in coredata.
you not want nsfetchedresultscontroller. results controller typically used uitableviewcontroller helper object populate table.
if want, continue. however, if looking getting entities core info context, need nsfetchrequest. lets have core info object called person. lets want entities named "ryan"
nsfetchrequest *fetch = [nsfetchrequest fetchrequestwithentityname:@"person"]; nsstring *nametoget = @"ryan"; nspredicate *predicate = [nspredicate predicatewithformat:@"name = %@", nametoget]; [fetch setpredicate:predicate]; nserror *error = nil; nsarray *results = [[self managedobjectcontext] executefetchrequest:fetch error:&error]; if(results) { nslog(@"entities name: %@", results); for(person *p in results) { nslog(@"person = %@", p); } homecoming results; } else { nslog(@"error: %@", error); } homecoming nil;
so create fetch request. nspredicate conditions of sql statement. create predicate format (there tons of options creating predicates). set request's predicate, tell managedobjectcontext execute fetch request. results request in returned array. every object in array object of type person.
if looking utilize nsfetchedresultscontroller, same type of thing. however, create controller 1 fetch request immutable, gets same data. if want different data, need create controller. if stick nsfetchrequests (i sperate info layers ui, using controllers isn't useful me) can query info want , homecoming results.
iphone ios objective-c xcode core-data
No comments:
Post a Comment