duemunk / Async

Syntactic sugar in Swift for asynchronous dispatches in Grand Central Dispatch
MIT License
4.59k stars 316 forks source link

Support for dispatch_group #8

Closed duemunk closed 8 years ago

duemunk commented 10 years ago

Wait for multiple blocks to finish

let block1 = Async.utility {}
let block2 = Async.background {}
Async.main(when: block1, block2) {}

using dispatch_group()

duemunk commented 10 years ago

Might already work with:

let block1 = Async.utility {}
let block2 = Async.background {}
block1.wait()
block2.wait()
Async.main {}
josephlord commented 10 years ago

@duemunk At least in some code I've been writing waiting on a group was faster than multiple wait calls. My proposal for handling dispatch groups would be to have [dispatch_block_t] argument versions of the static methods that dispatch all provided blocks to the same dispatch group and return a different object/struct more like the one in Async.legacy again with the same methods.

Most of the method behaviours are obvious:

  1. chaining - dispatch on group completion (dispatch_group_notify)
  2. wait - wait for group completion

Cancel is the one that is less clear. Should it only cancel the group if it hasn't started at all or should it cancel any blocks that have not yet started. I think the later is actually easier to implement and probably better.

Let me know what you think, I don't want .legacy to have diverging behaviour.

@chriseidhof Not sure if this will get to you but you suggested a GCD wrapper, do you have a view on this.

chriseidhof commented 10 years ago

I think it looks cool. What about being able to compose blocks with return types? e.g.

blockA : A -> B blockB : B -> C

And then you can do Async.start(x).background(blockA).background(blockB). If A doesn't have an input parameter, you could use () instead. Would that make sense?

duemunk commented 10 years ago

I'm currently on vacation for two weeks and the hotel wifi is very spotty here in San Francisco. Feel free to discuss without me :)

josephlord commented 10 years ago

Enjoy your holiday! I find San Francico lacking good internet slightly ironic but you can concentrate on the sights. If you have time in your trip do visit Yosemite for a couple of days. I haven't tried the OS yet but the scenery in the park is amazing and we saw a couple of bears too.

duemunk commented 9 years ago

@josephlord I managed to get to Yosemite. Very nice, but veeery dry :)

Sorry for not getting back to this earlier. Hopefully I can get a look on all your contributions over the holidays.

duemunk commented 9 years ago

@chriseidhof Please refer to https://github.com/duemunk/Async/issues/3 for the current work on bringning return types to Async. Spoiler, it's not working yet :)

duemunk commented 9 years ago

Refer to branch https://github.com/duemunk/Async/tree/feature/Groups for current work in progress

harlanhaskins commented 9 years ago

Sorry to reopen an old discussion, but I'd love this in Async. I've only got one thing in my app using dispatch_group, but it'd be much simpler to be able to do them with Async.

duemunk commented 9 years ago

@harlanhaskins Please feel free to extend Async to support dispatch_groups. I haven't had the use of groups myself, so I think it would be better for you to make a suggestion.

eneko commented 8 years ago

I've gone on a different direction and added AsyncGroup (see #75). It does not support chaining, which I thought would be confusing with group blocks. It does, however, have support for arbitrary non-GCD blocks.

duemunk commented 8 years ago

Thanks @eneko! I think this simplified approach is better, and at least good for now. Hopefully Swift gets better asynchronous support by version 4.0 or 5.0 :)