2

I have a question about core data. My entity look like this:

  • Game (GameID, GameName, Players, timestamp)
  • User (UserID, Email, status...)
  • Message (GameID, MessageText, SendersID)

In the table Game I want to store all players who are in this game. The problem is that they can be 2 or more so I have to store an array... I thought about two solutions..

My first solution would be storing a string into members which would look like this:

3,4,5,6,11

And then split it into an array

let members = fetchedData.Players.characters.split{$0 == ","}.map(String.init)

The second one: (I think this is the "cleaner" version)

I'll make my entity look like this:

Game (GameID, GameName, timestamp)

and add another entity:

Players (GameID, UserID)

What do you think? What advantages do I have if I make another Entity? (I think I'll have better performance but I also think that I'll have to much data)

4
  • 1
    I think the second method is cleaner, it makes model objects point to other object using id instead of just storing objects inside objects. Plus I discovered realm.io this morning, and I think it could be usefull to you too . Commented Nov 9, 2015 at 9:50
  • 1
    You should actually just create a relationship between Game and User. Core Data will handle a lot of the magic. Here is a tutorial code.tutsplus.com/tutorials/… and the official documentation developer.apple.com/library/ios/documentation/Cocoa/Conceptual/… Commented Nov 9, 2015 at 10:21
  • Thank you for the answers! :) I think the new entity is the better option. @Mayerz I used realm in another project and it was okay but I wanted to try Core Data because I think it's good to know how core data is working, but thanks! :) Commented Nov 9, 2015 at 12:41
  • @Godlike Ok!! Good luck man Commented Nov 9, 2015 at 13:24

2 Answers 2

5

If you are going to store an array of simple data, like ints, then using a transformable attribute is reasonable (see answer from Bartlomiej Semanczyk). Note, however, that from core data's perspective, the attribute is just a bag of bytes, with no meaning, and no searching. Also, changing one entry of the array changes the entire attribute.

Using another entity would be best advised for anything else. Don't worry about the extra storage overhead. Depending on how many players you have, you may or may not have better performance, but that's not the point.

It is cleaner, gives you more options, and fits better with the core data model.

Specifically, with the entity, you can now easily know things like "which games is player X playing?" "how many players playing all games?" "list all players", etc.

Now, when you establish the relationship, there is no need to add the "gameid" to the player entity.

Also, when you establish the relationship, you probably want to make many-to-many seeing as a game can have many players, and a player can be involved in many games.

Sign up to request clarification or add additional context in comments.

Comments

2
  1. Why do not you read How to save Array to CoreData?

  2. However you can convert array to String using:

    let arrayAsString = [1, 2, 3].description //[1, 2, 3]
    

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.