ios - NSFetchRequest search for all objects from last Nth date -
my info looks like:
obja date1 objnumber2 objb date2 objnumber1 objc date2 objnumber4 objd date2 objnumber3 obje date1 objnumber7 objf date3 objnumber6 objg date1 objnumber5
i looking objects nth lastly date. result objects need sorted using objnumber (this should easy using nssortdescriptor).
so if specify n=1 (most recent date), should [objf] only. (date3 recent)
if n=3 (oldest date), should sorted [obja, objg, objf]
the sorting part easy.
my question need firstly search latest date (using combination of sortdescriptor , nsfetchrequest searchlimit) in entire data? sec search find objects date (using nspredicate) , sort it?
or there improve way perform type of search? how generalize nth date instead of lastly date? big performance nail no??
note date not known beforehand.
edit2: okay more complicated since using nsdate. pretty much objects have unique dates lol. gotta throw nsdateformatter in there in mix :(
i figured out solution problem.
instead of using date, added new int64 nsnumber attribute. everyday obj added, objects tagged number particular day.
for retriving objects nth day, firstly "fetchlimit 1" nsfetchrequest number in sorted fetchrequest. gives me lastly number.
now nth lastly objects, subtract n lastly number. perform "nspredicted" nsfetchrequest objects number attribute. sort result array.
this resolves question :d
ios core-data nsdate nspredicate nsfetchrequest
No comments:
Post a Comment