iphone - CoreData - select specific entry from one to many relationship -


damn short titles! :p

basically have entity named "threads" , have entity named "messages". have 1 many relationship threads ->> messages (and 1 one inverse relationship).

what need records threads entity , each 1 need message latest timestamp (this int in "lastupdated" attribute, highest number guess suffice).

i'm not sure other information may want, here screenshot of entities:

alt text

i'm sure there must better way iterating through records , comparing threadids?

thank you.

once have individual thread objects, need sort on messages objects , take topmost message object.

the easiest solution create method in thread class this:

- (messages *) lastupdatedmessage{     nssortdescriptor *sort=[nssortdescriptor sortdescriptorwithkey:@"lastupdated" ascending:no];     nsarray *sortedmessages=[self.messagesinthread sortedarrayusingdescriptors:[nsarray arraywithobject:sort]];     return [sortedmessages objectatindex:0]; } 

then have ask each thread object lastupdatedmessage.

a more complex more efficient solution create intermediate linking entity link 1 message object 1 thread object , set link object message objects update. so:

messagethreadlink{     message<--(required)-->message.lastupdatelink     thread<--(required)-->thread.lastupdatedmessage } 

whenever message object updated, tell it's related thread object delete existing link object , create 1 pointing it. put code in custom setter lastupdated property.

with method have ask each thread lastupdatedmessage , appears automatically without lot of searching , sorting.


Comments

Popular posts from this blog

asp.net - repeatedly call AddImageUrl(url) to assemble pdf document -

java - Android recognize cell phone with keyboard or not? -

iphone - How would you achieve a LED Scrolling effect? -