Is there a way to force Core Animation to run it&#

2019-01-25 16:32发布

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.

3条回答
别忘想泡老子
2楼-- · 2019-01-25 17:13

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.

查看更多
仙女界的扛把子
3楼-- · 2019-01-25 17:13

You can use [CATransaction flush] to flush core animation if you don't let the runloop take its course

查看更多
beautiful°
4楼-- · 2019-01-25 17:20

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

查看更多
登录 后发表回答