If I have a long running task, how can that be broken down into a sequence of steps without having to resort to using a separate thread for its execution?
For example, suppose there is a spreadsheet application, and a re-calculation of a large numer of cells is to be performed, how can the model break it down into chunks? Is there a way of ios code posting events to itself for example, so it can do a chunk, post an event, do another chunk, post an event etc.
Or is the only way to use a separate thread?
I’ve got a stack of ios books that I’ve started to read, but I can’t find a single mention of anything like this in any of them.
You can use timers NSTimer, but if anything takes a long time you’ll be left with the UI hanging. Basically you need to make sure that all the work you do is < 0.01 seconds (approximately) each iteration, and delaying each iterations a little would make the UI more responsive.
The reason to use threads is to allow the OS to schedule things instead of you having to worry about it. If you run everything on the main thread (when you’re trying to do a lot of work) you’ll either freeze the UI or be constantly worrying about scheduling. If you use threads, the main thread has the proper priority so the UI remains responsive.