Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Low Priority Long Running Task

I have an application that requires the executing of a relatively slow (15-30 second) 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?

like image 932
Stussa Avatar asked Jan 22 '26 03:01

Stussa


2 Answers

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.

like image 164
JeremyP Avatar answered Jan 24 '26 17:01

JeremyP


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.

like image 32
Chance Hudson Avatar answered Jan 24 '26 18:01

Chance Hudson



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!