Reputation: 7883
I'm not using Core Data currently, but I'm about to switch to it from Realm, if I'm sure a certain thing will work, which is vital to the way my application currently works. I have a model with two objects:
Message:
date - NSDate
text - String
images - One-to-many > Image
Image:
imageURL - String
message - Many-to-one > Image
I have a table view which displays both Images and Messages. I need to know what should go at a certain index path, for when UITableView wants to know. So I need to be able to perform a query akin to:
"Give me an ordered list of images and messages, messages sorted by their date
and images sorted by their message.date
".
How could I do this?
Upvotes: 0
Views: 81
Reputation: 4218
As far as I know you can't sort a mixed array with messages and images during coredata fetch request. Here is one possible way:
- (NSArray *)sortedArrayUsingFunction:(NSInteger (*)(id, id, void *))comparator context:(void *)context
the function
NSInteger sort(id item1, id item2, void *context) {
int v1 = [item1 iskindofClass[message class]]?item1.data:item1.message.date;
int v2 = [item2 iskindofClass[message class]]?item1.data:item1.message.date;
if (v1 < v2)
return NSOrderedAscending;
else if (v1 > v2)
return NSOrderedDescending;
else
return NSOrderedSame;
}
Upvotes: 1