ios - FetchedResultsController NSSortDescriptor for one-to-many relationship -
i have 2 entities: chat
<->> chatmessages
(each chat has 0+ messages, each message belongs 1 chat).
i'm making fetchedresultscontroller
, i'd sort chats date of last message:
nsentitydescription *entity = [nsentitydescription entityforname:@"chatmodel" inmanagedobjectcontext:[nsmanagedobjectcontext mr_defaultcontext]]; [fetchrequest setentity:entity]; nssortdescriptor *sort = [[nssortdescriptor alloc] initwithkey:@"chatmessages.messagedate" ascending:no]; [fetchrequest setsortdescriptors:[nsarray arraywithobject:sort]];
but code get:
*** terminating app due uncaught exception 'nsinvalidargumentexception', reason: 'to-many key not allowed here'
how can sort chats in required way? (just whatsapp)
i would need @ least 2 things.
use nspredicate fetch specific chat:
nspredicate *predicate = [nspredicate predicatewithformat:@"chatidentifier == %@", achatidentifier]; [fetchrequest setpredicate:predicate];
and change nssortdescriptor this:
nssortdescriptor *sort = [[nssortdescriptor alloc] initwithkey:@"messagedate" ascending:no];
Comments
Post a Comment