Question

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: Data model

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.

Was it helpful?

Solution

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

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top