views:

352

answers:

1

I'm my program i'm loading a fairly large MutableDictionray and I would like to use a UIProgressBar. Does anyone have a suggestion of how to determine the loading time to make this work?

Thanks

A: 

I don't believe you'll find an easy answer to this, but here are some pointers. I assume you're currently loading this with -dictionaryWithContentsOfFile:.

  • First, rather that forcing the user to wait for a long load, consider changing your data storage. Core Data may be a better tool for this than a very large Dictionary.

  • Read the file into an NSData using -[NSFileHandle readDataOfLength:] in order to chunk it so you can update your progress bar. You can do this on a background thread and post notifications or call a delegate method on the main thread to let the progress indicator update.

  • You may want to experiment with -readInBackgroundAndNotify. I don't know the buffer-size off the top of my head for the iPhone, but it may be small enough to get notifications back at reasonable intervals, and it should remove the need to manage your own background thread.

  • Once you've read the data into an NSData, parse it using +[NSPropertyListSerialization propertyListFromData:mutabilityOption:format:errorDescription:. This should hopefully be fast enough not to hang anything, but if it's too slow, you can do this on a background thread and make it part of your last progress bar update. Some experimentation may be required to get the UI smooth here.

Rob Napier