问题
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:
User
entity define a author of message and participant in a conversation.Message
entity define every text sending with app.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:
- In conversation screen i want to know who wrote what
- In one conversation can participate at least two users
- The message is text-only
- 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