I am building a social app that has a chatView. Message is an entity. On the usersViewController (rootViewController), I want to just display the number of unread messages from each user. But on the chatView, I want to show the messages sent between me and the selected user.
How should I fetch the messages from Core Data?
Is there a way to fetch them all into an NSMutableDictionary with userIds as keys, each pointing to an NSMutableArray of Message entities? Or, would I have to fetch all the Messages and then iterate through them with Objective-C to arrange them as described?
Either way, should I load them all upfront in the appDelegate? Or, should I only load the messages for the specific conversation upon viewing the corresponding chatView?
To get the unread counts, should I iterate through the fetched Message entities to make an NSMutableDictionary of userID keys pointing to count values? Or, should I make a predicate that acts like a SQL sum query grouped by userId (Is that even possible with Core Data?)?
Should I create a model, call it
UnreadMessageCount
, that stores the number of unread messages that I have from each user and a reference to theUser
model? I want to have all the unread messages on the first page of the app, so this would make that query faster. But receiving messages would be slower because I would not only have to add it to the Message model but also I would have to increment the count for that user in the UnreadMessageCount model. Also, the data would sort of be duplicated. Also, I'll only make the query for all message counts once per application launch, but i'll make queries to update the unread message count every time I receive a new message and am not on the chatView page with that user.
Please fill me in on iPhone memory management & Core Data possibilites and performance. And also please give your opinion on the best way to engineer this.
Thanks!
Matt