saving array to disk in a singleton
I have a singleton which has an NSArray that is created when the first instance of the singleton is. When should I save this array to disk, in dealloc method? I checked the Apple docs and its says you really shouldn't in dealloc but fails to say where. So am I meant to write to disk everytime the array is modified? This s开发者_运维知识库eems like a waste?
In general, you should save state close to the time that state was changed. If the amount of state that you are saving is very small, then you could wait until application delegate -applicationWillResignActive: method gets called. Or you could have singleton register with NSNotificationCenter to observe UIApplicationWillResignActiveNotification. And then save.
If your array will be very large, then you may wish to save less frequently. But you must also be mindful of how long it might take to write your array to disk. Depending on the circumstances under which your application resigns as active, you may not get much time to write to disk.
Do you have a good sense of the maximum amount of data that might need to be written? Is this constrained or is it open-ended?
精彩评论