I have an application that requires the executing of a relatively slow (15-30 s开发者_运维百科econd) task after launching (importing to core data). I'm looking for a good way to execute the task without causing the interface to appear slugish or frozen. I've tried:
- Chunking up the import into short operations and adding them to the main NSOperationQueue
- Executing the import using the
performselectorinbackground
to try to speed up the process
However, neither lead to a significant improvement. Any ideas?
Chunking up the import into short operations and adding them to the main NSOperationQueue
[my emphasis]
If you put the operations on the main queue they will run on the main thread and impact the UI. You should create a new queue, set the maximum concurrency to 1 and then just add all the operations.
Of course, on most iDevices there's only one CPU core so you might still see issues but if your queue is not using the main thread, fiddling with the operation's thread priority might help.
If you are trying to prevent the interface from freezing, you are going to need to use a background thread. You could use performSelectorInBackground
or you could use [NSThread detachThreadSelector:
. However these will not actually speed up the process, they simply free up your main thread to do other stuff. If you do decide to use a second thread you may want to read about it first if you haven't used it before.
精彩评论