views:

48

answers:

2

Just want to make sure I'm doing right things with memory management of Core Data.

In my view controller's (which is tab bar view) viewDidLoad I use NSFetchRequest to retrieve the rows I need. Then I retain returned NSArray object, since I need to wait for user interaction to show the part of this data. In viewDidUnload I release that array.

My concern is when the data will become more, would this mechanism be inefficient? I use just part of it anyway, but need to fetch all in case user pass through all data.

+1  A: 

The answer to your question depends entirely on how your model is structured, and how many objects you are fetching. Unfaulted objects are pretty small, though not having an object is better. If you want to be clever you can limit your fetch sizes and bring in objects in ranges as needed. That is what NSFetchedResultsController does.

Having said that I don't understand why you need to fetch a potentially large set of data and hold onto it in a tab bar view, at most you could have a half a dozen buttons showing at once, and if you have a customization page how many tabs can realistically put into it before it is unwieldy for a user to scroll through it? More than maybe two dozen and you will have a UI issue, and even on a device like an iPhone that is not a significant amount of memory.

Louis Gerbarg
Ah sorry for misleading. I'm not filling this data into tab bar, instead one of the tab bar controller views, which is in fact scroll view. Each page in scroll view represents a record. User may scroll to just couple of items or the whole database.
Michael
That still doesn't give me any scope of what you are talking about. If the whole database is ~100 records it doesn't matter, if it is ~1,000,000 then it does. If it does matter then I suggest you choose a reasonable batch size for your fetch request, and fault in a fixed number of objects per fetch. If the user scrolls beyond the objects you have then you do a fetch with new limits. Checkout the `fetchOffset`, `fetchLimit`. and `fetchBatchSize` properties of NSFetchRquest.
Louis Gerbarg
A: 

That solution is fine although you might want to look at the NSFetchedResultsController as it will do a LOT of heavy lifting for you.

Otherwise you are fine and the memory is handled for you. When you do a fetch you will get back the skeleton objects (read small amount of memory used until accessed individually) and if they are not used for a while they can get turned back into skeletons. What this means is that Core Data will do most of the memory management for you. As long as you hang onto your array you are golden.

Marcus S. Zarra