-
Notifications
You must be signed in to change notification settings - Fork 317
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Async for non-GCD async operations #31
Comments
i'm wondering this situation too :). In some situations, we do async network request in background. |
Hi guys! I suggest you take a look at forbind by my friend @ulrikdamm. It's a future/promises implementation for Swift and looks really "promising" 😆 It's no that I don't want to add it to Async, but so far it does quite well doing what is already does + I haven't found a workable solution in Swift yet. |
I have some implementation done for the callback feature and can work with Alamofire. But I cannot think how to implement this to Async. I use classes for self reference (self.next) but Async use structs. Please take a look at DGTAsync. |
You can do this right now without adding a pull request at least as a temporary solution. So from what it looks like this project was meant to fire off tasks on background queues but won't wait for an async task (like a network request) to finish before firing off the next block in the chain. This is a common problem when using blocks and long running tasks in general. WARNING: This may be sloppy and I haven't scoped out what the overhead for doing something like this is. Also normally you could also use a This problem can be solved First without semaphores.Our network function makes a fake HTTP request and puts it on its own queue and runs asynchronously.
Then of course the meat and potatoes:
What you get is:
Which is the problem as I understand it. The code is continuing even though the async task inside hasn't finished yet. With semaphoresNow using the same async method as above with our new meat and potatoes:
We get:
So what happened? I'll let the Apple Docs explain:
After we fired off This stops code execution right before our Then our code continues on it's way in order. Also since this is all on a background thread the main thread isn't being blocked until of course you run Hope that makes sense. |
I have submitted a pull request for adding AsyncGroup to help manage multiple asynchronous operations, including custom ones based or not on GCD. The PR was not intended to solve the issue on this thread, but this code should work by using a GCD group, even if it only has one operation that we want to wait on: let group = AsyncGroup()
group.enter()
// async upload
Alamofire.upload(.POST, "http://httpbin.org/post", file: fileURL)
.reponseJSON { (_, _, JSON, _) in
// upload done!!
group.leave()
}
}
group.wait()
Async.background {
// do some stuff S in **background** after upload done :)
S()
} Hope that helps! |
I love the idea of an AsyncGroup. Unfortunately, I'm not able to get it working for my needs. I suspect is has to do with the way I'm calling it.
It simply hangs at this point. I suspect it is because my network call is happening in my other class and not in the ExternalInterface class that I'm directly referencing here. By the time the data gets to the code above, it has been returned through 2 different handlers. |
@JoshHrach I haven't actually used AsyncGroup myself :) Please open a separate issues with as much code and explanation as possible. Hopefully someone will be able to help you out! |
@duemunk I'll do that tomorrow. Thanks. |
@duemunk I never did open another issue. But I did get it resolved. I ended up adding support for dispatch_group_notify() and made a pull request with it. It was the only thing missing from AsyncGroup (mentioned earlier in this issue) that I needed for our project. |
Project is in maintenance mode, so I'm closing this issue. Thanks for everyone's work! |
It can be very useful if Async can use with async operations.
Right now, I think Async can use only with sync operations.
But if I use some async operations such as Alamofire to upload files. Now the chain is just not wait for an upload to be finished.
An idea for accomplish this is something like passing a
done
block and waiting fordone()
to be called to continue to the next Async chain. I saw Quick/Nimble use this forwaitUntil
operation here.So, here is a use case proposal.
The Async is now very smart and every operations (.background, .main, .utility, etc..) can take a parameterless block for sync operations and a "one parameter block" (that parameter is another block expose as parameter name
done
) for async operations.Any thought or possibility?
The text was updated successfully, but these errors were encountered: