Chat, conversation, messaging - CoreData model

删除回忆录丶 提交于 2019-12-10 10:01:34

问题


Before you mark a question as a duplicate of this question please read a description. I don't need to continue discussion there in the comment.

So I want to create CoreData model for messaging app. Like said in this topic did i mentioned - i've had three entities:

  1. User entity define a author of message and participant in a conversation.
  2. Message entity define every text sending with app.
  3. Conversation entity defines conversation beetwen users using messages.

OK so my data model is like:

But everything is connected to each other here. The only difference between mentioned answer and my solution is that User and a Message are connected using one-to-many relationship. I think i need that, becouse without that it's impossible to know who wrote what in a conversation.

But as far as i know data model when everything is connected to each other have no sense.

So the key goals here is:

  1. In conversation screen i want to know who wrote what
  2. In one conversation can participate at least two users
  3. The message is text-only
  4. User have to be able to list all his conversations.

That's it.

Please validate the current solution and feel free to criticize.


回答1:


As far as I can tell, there is no real need for the many-to-many relationship between User and Conversation.

If a user like to get all of its conversations he could use this fetch request:

User* user = //get some user you like conversations for
NSFetchRequest* r = [NSFetchRequest fetchRequestWithEntityName:@"Conversation"];
r.predicate = [NSPredicate predicateWithFormat:@"ANY messages.author = %@",user];

In the same fashion you could get all users of a given conversation.

You could model this as a fetched property on each of these entities (User and Conversation).

You should really consider changing the chat relationship to messages



来源:https://stackoverflow.com/questions/22529175/chat-conversation-messaging-coredata-model

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!