views:

287

answers:

2

Core Animation uses a background thread to do it's job. Now the problem is this: I have a heavy calculation going on in the main thread. Core Animation immediately freezes until that calculation is done. And then it continues to finish it's animations. I remember reading in a document that CA has a low priority in processing time, meaning that whatever the main thread wants to do is high-prio and will be done more likely than any fancy animation at the same time.

I want to force Core Animation to schedule it's background thread nicely with the main thread under any circumstances. Or alternatively a separate thread that will run the heavy calculation outside the main thread. I tried that already, but CA still freezes until that's done. I expect the scheduler to switch processing time quickly between CA and that calculation.

How could CA be forced to keep on working? If things go just a bit slower than, that's fine. But most important thing is that all things keep on going from the users point of view.

+1  A: 

I'm not sure, but you can try to call [NSThread setThreadPriority:1.0] if you running your CA in another thread.

Morion
does it matter if I call CA in another thread? as far as I know, CA maintains it's own process and thread for this kind of stuff (or at least it's own thread, with a very low priority setting)
HelloMoon
+4  A: 

Don't run heavy calculations on the main thread, because they will block the UI and lead to a bad user experience. Run them in a background thread.

That said, the iPhone is a single-core system, so if a calculation is pegging the CPU in one thread, the performance of every other thread may grind to a near halt. If you can, try breaking your calculation into smaller elements and running them as NSOperations in an NSOperationQueue. If you make sure that the calculation segments aren't so small that the overhead of creating an NSOperation for them becomes too large, this might provide a means of throttling the calculation a bit so that your animations aren't being slowed down.

Core Animation tends to perform a number of calculations upfront, before an animation may run, so those may be getting slowed down by your heavy calculation thread. You might also be able to start your heavy calculation within the -animationDidStart: delegate method for your CAAnimation, making the calculation only kick off when the animation is in progress. I believe the progress of an animation uses fewer calculations than its start, so it may be better able to coexist with your heavy calculation.

Brad Larson
Thanks. Is NSOperations capable of making timeouts? My calculation is already broken down in pieces. Each one is scheduled with a delay of 0.05 seconds. So actually there is some spare time inbetween. However, when CA has to do precalclations at the start, your idea of waiting for -animationDidStart is goog. I tried it by just delaying the call for starting with the first block of calculations with 1 second. CA begins animating, then the heavy calclations come into place, and CA freezes. It doesn't help. I'm running the calculations in a new thread.
HelloMoon
The point is, that after calculating each block of data, there's an update in the UI happening. These updates come fast and smooth just as expected. It would be just perfect when there was a way to schedule all this in such a way that CA shares CPU time with this... going to have a look at these NSOperationQueue things, although they don't sound like they would provide timeouts like delayed performSelector calls do (?)
HelloMoon
NSOperationQueue creates threads for your NSOperations, as needed. It is reasonably intelligent about managing what tasks are performed when, but I don't know if it has the same kind of system-level awareness on the iPhone that it does on Snow Leopard (where it's overlaid on top of GCD). It also supports dependencies between tasks, which you might be able to use to your advantage here.
Brad Larson
sounds good. did you use it on the iphone already? and how would it interact with core animation? maybe i'll have to get rid of CA here and do the animations all manually so I can force it to share cpu time in a balanced way...
HelloMoon
Even if you were to implement your own manual animation code (which would take you a very long time to do), you will run into the same issue. If your heavy calculation is taking up so much processor time that it's causing Core Animation to stutter, what makes you think that your manual animation wouldn't have problems as well? I'd recommend spending some quality time with Shark and Instruments to try to work on your heavy calculation first.
Brad Larson