ios - Core Data NSFetchResultController batch size -
nsfetchresultcontroller
nsfetchrequest
fetchbatchsize = 20
return entities. can be? didn't use sectionkeypath , tried different sort descriptors, it's still return objects.
thanks replies!i explain details. have entity 2 fields - distance , time. have created nsfetchresultcontroller:
func noticesfetcher() -> nsfetchedresultscontroller { let fetchrequest = nsfetchrequest() let defaultstore = rkmanagedobjectstore.defaultstore() let entity = nsentitydescription.entityforname("notice", inmanagedobjectcontext: defaultstore.mainqueuemanagedobjectcontext) fetchrequest.entity = entity let distancesortdescriptor = nssortdescriptor(key: "distance", ascending: true) let timesortdescriptor = nssortdescriptor(key: "time", ascending: false) let sortdescriptors = [distancesortdescriptor, timesortdescriptor] fetchrequest.sortdescriptors = sortdescriptors fetchrequest.fetchbatchsize = 20 let resultfetcher = nsfetchedresultscontroller(fetchrequest: fetchrequest, managedobjectcontext: defaultstore.mainqueuemanagedobjectcontext, sectionnamekeypath: nil, cachename: nil) return resultfetcher }
but when perform fetcher have entities in database.(100)
func performfetcher(fetcher: nsfetchedresultscontroller?, filter: nspredicate?, success: (() -> ())?, failure: ((nserror) -> (nserror))?) -> bool { nsfetchedresultscontroller.deletecachewithname(nil) var resultperform = false fetcher?.fetchrequest.predicate = filter { try fetcher?.performfetch() resultperform = true if success != nil { success!(); } } catch let error nserror { if failure != nil { failure!(error) } } return resultperform }
what can be? result want pagination getter. know can through limit , offset, problem here? thanks
well, depends on mean "return entities." doubt returns array populated entities realized.
the batch size fetch (in case) 20 entities @ time. go ahead , @ set of registered objects moc , can verify happening.
you can fire instruments , watch individual core data fetches take place.
no, batch size fetch not "only 20 entities", fetches entities. can make test project , test batch size?i'm sure have same issue – serd
@serd, answer providers here you, , wrong. if doubt answer, you should create test case demonstrates whether or not correct. why in world should take time so, when 1 problem, , ones freely offering assistance?
if doubt answer, you work, , if find answer in error, provide either correction or evidence answer incorrect.
however, new here, , in hopes may learn experience, i'll provide brief demonstration.
setup moc test adding num_objects
instances database.
nsuinteger const num_objects = 1000; nsuinteger const init_batch_size = 100; - (void)setup { [super setup]; helper = [[testhelper alloc] init]; url = [[[nsurl fileurlwithpath:nstemporarydirectory()] urlbyappendingpathcomponent:[[nsuuid uuid] uuidstring]] urlbyappendingpathcomponent:@"foo.sqlite"]; [[nsfilemanager defaultmanager] createdirectoryaturl:[url urlbydeletinglastpathcomponent] withintermediatedirectories:yes attributes:nil error:null]; @autoreleasepool { nsentitydescription *noticeentity = [[nsentitydescription alloc] init]; noticeentity.name = @"notice"; nsattributedescription *distance = [[nsattributedescription alloc] init]; distance.name = @"distance"; distance.attributetype = nsdoubleattributetype; nsattributedescription *time = [[nsattributedescription alloc] init]; time.name = @"time"; time.attributetype = nsdoubleattributetype; noticeentity.properties = @[distance, time]; nsmanagedobjectmodel *model = [[nsmanagedobjectmodel alloc] init]; model.entities = @[noticeentity]; nspersistentstorecoordinator *psc = [[nspersistentstorecoordinator alloc] initwithmanagedobjectmodel:model]; [psc addpersistentstorewithtype:nssqlitestoretype configuration:nil url:url options:nil error:null]; moc = [[nsmanagedobjectcontext alloc] initwithconcurrencytype:nsmainqueueconcurrencytype]; moc.persistentstorecoordinator = psc; (nsuinteger count = 0; count < num_objects; ) { @autoreleasepool { (nsuinteger batchcount = 0; batchcount < init_batch_size && count < num_objects; ++batchcount, ++count) { nsmanagedobject *notice = [nsentitydescription insertnewobjectforentityforname:@"notice" inmanagedobjectcontext:moc]; double distance = ((double)arc4random_uniform(100000)) / (arc4random_uniform(100)+1); double time = distance / (arc4random_uniform(100)+1); [notice setvalue:@(distance) forkey:@"distance"]; [notice setvalue:@(time) forkey:@"time"]; } [moc save:null]; [moc reset]; } } [moc save:null]; [moc reset]; } }
cleanup after test...
- (void)teardown { [super teardown]; [[nsfilemanager defaultmanager] removeitematurl:[url urlbydeletinglastpathcomponent] error:null]; }
a couple of helper methods...
- (nsarray*)executefetchwithbatchsize:(nsuinteger)batchsize { nsfetchrequest *fetchrequest = [nsfetchrequest fetchrequestwithentityname:@"notice"]; fetchrequest.sortdescriptors = @[[nssortdescriptor sortdescriptorwithkey:@"distance" ascending:yes], [nssortdescriptor sortdescriptorwithkey:@"time" ascending:no]]; fetchrequest.fetchbatchsize = batchsize; return [moc executefetchrequest:fetchrequest error:null]; } - (nsuinteger)numberoffaults { nsuinteger numfaults = 0; (nsmanagedobject *object in moc.registeredobjects) { if (object.isfault) ++numfaults; } return numfaults; }
a test using default batch size
- (void)testthatfetchrequestwitdefaultbatchsizefetcheseverything { xctassertequal(0, moc.registeredobjects.count); nsarray *fetched = [self executefetchwithbatchsize:0]; xctassertequal(num_objects, moc.registeredobjects.count); xctassertequal(num_objects, fetched.count); xctassertequal(num_objects, [self numberoffaults]); [[fetched objectatindex:1] valueforkey:@"distance"]; xctassertequal(num_objects, moc.registeredobjects.count); xctassertequal(num_objects, fetched.count); xctassertequal(num_objects-1, [self numberoffaults]); }
a test using non-default batch size
- (void)testthatfetchrequestwithexplicitbatchsizeonlyfetchesthenumberrequested { xctassertequal(0, moc.registeredobjects.count); nsuinteger const batch_size = 20; nsarray *fetched = [self executefetchwithbatchsize:batch_size]; xctassertequal(0, moc.registeredobjects.count); xctassertequal(num_objects, fetched.count); xctassertequal(0, [self numberoffaults]); [[fetched objectatindex:1] valueforkey:@"distance"]; xctassertequal(batch_size, moc.registeredobjects.count); xctassertequal(num_objects, fetched.count); xctassertequal(batch_size-1, [self numberoffaults]); }
thanks code! have problem nsfetchresultcontroller , nsfetchrequest, have batch size(it's fetches entities – serd
habits die hard. instead of making assertion, should have taken code gave , modified test fetched results controller, , confirm assertion "it's fetches entities" not true. example...
modify helpers tad...
- (nsfetchrequest*)fetchrequestwithbatchsize:(nsuinteger)batchsize { nsfetchrequest *fetchrequest = [nsfetchrequest fetchrequestwithentityname:@"notice"]; fetchrequest.sortdescriptors = @[[nssortdescriptor sortdescriptorwithkey:@"distance" ascending:yes], [nssortdescriptor sortdescriptorwithkey:@"time" ascending:no]]; fetchrequest.fetchbatchsize = batchsize; return fetchrequest; } - (nsarray*)executefetchwithbatchsize:(nsuinteger)batchsize { return [moc executefetchrequest:[self fetchrequestwithbatchsize:batchsize] error:null]; } - (nsfetchedresultscontroller*)executefetchusingfetchedresultscontrollerwithbatchsize:(nsuinteger)batchsize { nsfetchedresultscontroller *frc = [[nsfetchedresultscontroller alloc] initwithfetchrequest:[self fetchrequestwithbatchsize:batchsize] managedobjectcontext:moc sectionnamekeypath:nil cachename:nil]; [frc performfetch:null]; return frc; }
and change existing tests add tests frc.
- (void)testthatfetchrequestwitdefaultbatchsizefetcheseverythingevenwithfetchedresultscontroller { xctassertequal(0, moc.registeredobjects.count); nsfetchedresultscontroller *frc = [self executefetchusingfetchedresultscontrollerwithbatchsize:0]; xctassertequal(num_objects, moc.registeredobjects.count); xctassertequal(num_objects, frc.fetchedobjects.count); xctassertequal(num_objects, [self numberoffaults]); [[frc.fetchedobjects objectatindex:1] valueforkey:@"distance"]; xctassertequal(num_objects, moc.registeredobjects.count); xctassertequal(num_objects, frc.fetchedobjects.count); xctassertequal(num_objects-1, [self numberoffaults]); } - (void)testthatfetchrequestwithexplicitbatchsizeonlyfetchesthenumberrequestedevenwithfetchedresultscontroller { xctassertequal(0, moc.registeredobjects.count); nsuinteger const batch_size = 20; nsfetchedresultscontroller *frc = [self executefetchusingfetchedresultscontrollerwithbatchsize:20]; xctassertequal(moc, frc.managedobjectcontext); xctassertequal(0, moc.registeredobjects.count); xctassertequal(num_objects, frc.fetchedobjects.count); xctassertequal(0, [self numberoffaults]); [[frc.fetchedobjects objectatindex:1] valueforkey:@"distance"]; xctassertequal(batch_size, moc.registeredobjects.count); xctassertequal(num_objects, frc.fetchedobjects.count); xctassertequal(batch_size-1, [self numberoffaults]); }
Comments
Post a Comment