duemunk / Async

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

Added support in AsyncGroup for dispatch_group_notify() #95

Closed JoshHrach closed 8 years ago

JoshHrach commented 8 years ago

While AsyncGroup currently supports calling wait(), that doesn't always work when dealing with a group. An example is trying to call a series of nested functions that eventually perform an asynchronous network call. The group seems to finish, but it appears to complete immediately, leading to no action within the async method and no actual 'waiting'. Thus, if you were performing a segue after waiting and were setting up data for the segue in the group, the segue would complete without any of the data actually being passed along.

Creating a dispatch_group_notify call lets you wait until something is done and calling the action that needs to be done after the group is complete. For instance, you can create a group that calls various async methods within the group and trigger a segue after the group has completed its tasks.

I created this in the AsyncGroup struct. notify() is private and called by public notify() functions. For my purposes, I'm only calling items on the main thread in this response, so notifyMain is what I need. But if you want to trigger something in another queue, you can do so.

JoshHrach commented 8 years ago

I just saw the older pull request for something similar. I created this for our needs and decided to share it before looking at an existing request.