Jason Renaldo
Jason Renaldo

Reputation: 2822

Why is using dispatch_async helping with performance so much?

I will admit, I found this piece of code online - used it - and boom! I had a big performance boost.

My code looked as follows:

enter image description here

And using Time Profiler, I found that becomingFirstResponder was going nuts, shown here (it's in createOrEditItem):

enter image description here

So I hit the googles and changed it like so:

enter image description here

And Time Profiler isn't even showing it as a "hot" method!

My question is simply, why? What is the technical magic taking place here?

Thanks.

enter image description here

Upvotes: 1

Views: 278

Answers (1)

Léo Natan
Léo Natan

Reputation: 57060

When you asynchronously schedule code to run on the same thread (as you do in your example), you are scheduling it to run on a following runloop cycle.

It does not help performance as much as it shuffles code from one runloop cycle to the next, spreading the workload across multiple runloop cycles, making it appear smoother and faster. If you have side-effects from dummyTextField becoming first responder, this could also explain a bit why it seems more performant, as, again, the side-effects are pushed to the next runloop cycle.

Upvotes: 1

Related Questions