Re: Cheating a synchronous call on the main thread
Re: Cheating a synchronous call on the main thread
- Subject: Re: Cheating a synchronous call on the main thread
- From: Jens Alfke <email@hidden>
- Date: Mon, 29 Jun 2015 10:33:36 -0700
> On Jun 29, 2015, at 10:13 AM, Gavin Eadie <email@hidden> wrote:
>
> The problem with the callback to “after” is that “after” is just the continuation of the program and possibly nothing to do with what happens in “fakeSyncrony” ..
Yeah, I don’t think you have any alternative but to restructure your code to run asynchronously. Luckily blocks and dispatch queues make it possible to do that without having to refactor the code too heavily. There’s a common pattern where you change
- (void) foo {
A();
B();
C();
}
into
- (void) foo {
A();
dispatch_async(bg_queue, ^{
B();
dispatch_async(main_queue, ^{
C();
});
});
}
The problem of course is that the semantics of -foo have now changed such that the final part of the work (C) doesn’t happen until later, so any caller of -foo has to be aware of this, which can require more restructuring. Lather, rinse, repeat.
Fun fact: The latest versions of C# have some cool async support that essentially do what you’re trying to do here — turn a synchronous call into an async one while still leaving the code looking synchronous. It does nasty continuation-based stack magic in the compiler to make this work.
—Jens
_______________________________________________
Cocoa-dev mailing list (email@hidden)
Please do not post admin requests or moderator comments to the list.
Contact the moderators at cocoa-dev-admins(at)lists.apple.com
Help/Unsubscribe/Update your Subscription:
This email sent to email@hidden