Am I using the kernel data correctly, retrieving the object once at startup and causing the save when I want to save the data?

I dive into iOS programming and use Core Data in my application to save game data, however I wonder if my approach to using Core Data is appropriate. I have three tables in my database, the first two having a one-to-many relationship with another table (for example, "UserProfile →> Puzzle Packs →> Puzzles").

The approach I take to use and save data is simple, get an instance of UserProfile using NSFetchedResultsController and save the UserProfile object as an instance of var in my application delta. Then I use this UserProfile object in the rest of the code to access and change the state of puzzles and puzzles (representing the user's progress in the game), and whenever I make changes to objects, I simply call the NSManagedObjectContext save method to update the database, which is also stored in the application delegate.

My question is, should I retrieve data from the database at any time when I need to access or change it, or my current approach, fetching the top object once and often calling the save method, the correct way to use Core Data?

Thank you for your wisdom! I apologize if my question is odd, I'm still a noob.

+4
source share
1 answer

While you work with these objects with only one NSManagedObjectContext (that is, you never have to merge changes between contexts), then I do not see a problem with extraction once and save it if necessary.

However, as soon as you work with several contexts (for example, using NSOperation / NSOperationQueue or other multithreading methods that require separate contexts), you will want to make sure that you merge the changes and update the other contexts so that changes from one thread do not break that something different.

+2
source

All Articles