-->

How can I determine if Apple methods are asynchron

2019-01-06 22:15发布

问题:

I'm binding an NSArrayController to a managedObjectContext with mainQueueConcurrency.

All that I want is to do is modify the sort properties the arrangedObjects with my own function and then call rearrangeObjects and then select some objects.

But it's looking like rearrangeObjects doesn't execute synchronously.

So how can I prove that, one way or the other?

This works:

[self.myArrayController rearrangeObjects];

// Async needed -- I believe rearrange calls a fetch, which is async
dispatch_async(dispatch_get_main_queue(), ^{
    [self.myArrayController setSelectedObjects:anArray];
});

Minus the dispatch_async, it doesn't work. The selection doesn't happen.

I'm fairly convinced that like fetch on an NSArrayController, rearrangeObjects is scheduling itself on the next runloop iteration.** So how do I prove that? I can throw breakpoints in there and examine the array controller, etc etc. And I have, and I suspect that's what's happening.

But is there some debugging trick which would just make it obvious "Eureka! This line of code's launching an asynchronous operation" ??

** It was cocoa bindings that's doing something asynchronously. Of course. rearrangeObjects just triggers it.

回答1:

I was able to catch rearrangeObjects invoking dispatch_async_f (through bindings) by breaking just before the suspect line of code and enabling symbolic breakpoints on the GCD dispatch_async functions:

And, sure enough, the dispatch_async_f symbolic breakpoint stopped on the rearrangeObjects line: