Once you have separate Thread objects, you just need to sort by the Message objects and take the topmost Message object.
The simplest solution is to create a method in the Thread class as follows:
- (Messages *) lastUpdatedMessage{
NSSortDescriptor *sort=[NSSortDescriptor sortDescriptorWithKey:@"lastUpdated" ascending:NO];
NSArray *sortedMessages=[self.messagesInThread sortedArrayUsingDescriptors:[NSArray arrayWithObject:sort]];
return [sortedMessages objectAtIndex:0];
}
Then all you have to do is ask each Thread object for its lastUpdatedMessage.
A more complex but more efficient solution would be to create an intermediate binding object that binds one Message object to one Thread object and sets this link object as an update to the message objects. So:
MessageThreadLink{
message<--(required)-->Message.lastUpdateLink
thread<--(required)-->Thread.lastUpdatedMessage
}
, , Thread , . , lastUpdated.
lastUpdatedMessage, .