Using CoreData for my first app?

Hi, I'm a Xcode newbie and I started with an online tutorial at Udemy... and now I want to start coding my first app. My idea is to make a Darts score app and an essential question for me is how should look like the data modell.
In principal I have games and players ... for each game I need two players and I would like to store the results for running and completed games persistent on the device.
I've learned in the online tutorial the possibilities plists, User defaults and CoreData... I'm guessing that I have to use CoreData for my solution...
My first approach is to have two entities (games, player) with a many-to-many relationship. I understood an one-to-one relationship, but right now not the many-to-many relationship...
My general question is... am I correct in my assumption?... or does it exist an easier way to solve my problem?

Many thanks...

P.S. my background is an automation engineer for factory automation an I know how important is good architecture of a software solution but unfortunately I'm not familiar with iOS technology for data storage.

hi,

plists and User Defaults (which, i think, is just a plist) are fine for small amounts of data, but once the data size grows, you should be looking at Core Data and/or using the file system directly (the DocumentsDirectory in iOS).

in Core Data, the two entities would be Game and Player, and it's a many-to-many relationship. assuming you want the players in a game listed in order (the first one, the second one) and indicate that in the Core Data model, the Game class would be using an NSOrderedSet for player references (perhaps named players), while the Player class would be using an NSSet for the game references (perhaps named games), assuming there's no need to keep an ordering there (but one could later sort by date played or something).

Xcode generates custom accessors to manage these relationships (keeping both ends of the relationship in sync). if you have two Players and want to create a new Game, you could write
Code Block
let player1 = // a known Player who is player #1 in this Game
let player2 = // a known Player who is player #2 in this Game
let newGame = Game(context: managedObjectContext) // you could use player1.managedObjectContext for the context
newGame.addToPlayers(player1) // addToPlayers is added by XCode for the Game class
newGame.addToPlayers(player2) // and the Game is automatically added to each of the player's games
// set other properties of newGame
try? managedObjectContext.save()

if you had a Player and wanted to look through all the Games played by that player, you could write
Code Block
let gamesPlayed = player.games as! Set<Game> // typecast needed to get from older Objective C type to Swift
for game in gamesPlayed {
// do what you need
}

when you need to look at the NSOrderedSet of Players in a Game, i think the reciprocal cast (to move from an NSOrderedSet to a Swift Array) is
Code Block
let players = game.players.array as! [Player]

to see a list of all Games and all Players is a straightforward fetch from Core Data, which many do using @FetchRequest and List in SwiftUI.

that might be enough to digest for now.

hope that helps,
DMG
Using CoreData for my first app?
 
 
Q