BeachRunnerFred
BeachRunnerFred

Reputation: 18558

iOS Development: How can I tighten up my Core Data memory usage?

I'm very new to Core Data and I'm still learning how it all works under the hood. My Core Data model hierarchy contains a user profile, which contains a set of ten different levels using a one-to-many relationship, with each level containing 30 different puzzles also using a one-to-many relationship.

UserProfile (x1) -> Level (x10) -> Puzzle (x30), for a grand total of 311 objects per user profile

The levels and puzzles models are used to store the user's progress as the user sequentially solves the puzzles. When the game runs for the first time, the game reads all the levels and puzzles data from a plist and generates Core Data objects (table rows) that will be used store the users progress as they play the game. In other words, the list of Core Data objects doesn't grow as the user plays the game, instead it's all created at the start and the properties of the objects change as the user plays the game. I suspect this is inefficient, tho my questions remain the same...

  1. When I load a user profile from Core Data, will is also load all ten levels, as well as the 30 puzzles for each level into memory? I ask because I obviously don't need all that data if the player is only playing one level at a time.
  2. If all the level and puzzle data is loaded into memory when the user profile is loaded, how can I "lazy load" the level and puzzle data as it's needed?
  3. When I run my game using the Allocations instrument, it shows it's using 6MB of "Overall" memory when the app is fully loaded and sitting in the main menu. Obviously, the memory usage increases as the user dives deeper into the navigation stack. Is 6MB considered "a lot" of memory for an app that just started?
  4. I've noticed that when I comment out the line of code that loads the User's profile from Core Data, 6MB is still used. Does this mean the impact that my Core Data objects have on memory are negligible?

I apologize if these are outrageous questions and I apologize for their length, and thanks so much for your wisdom! I'm heading back to the Core Data docs right now, I just thought I'd post these questions while I continue the self-teaching.

Upvotes: 0

Views: 1105

Answers (1)

ox_n
ox_n

Reputation: 667

This answers your 'lazy load' question. Core Data to-many relationships. Are they Lazy Load?

I wouldn't worry about 6MB of memory unless you see a performance issue. You can always check it with the included utilities.

Upvotes: 1

Related Questions