duemunk / async Goto Github PK
View Code? Open in Web Editor NEWSyntactic sugar in Swift for asynchronous dispatches in Grand Central Dispatch
License: MIT License
Syntactic sugar in Swift for asynchronous dispatches in Grand Central Dispatch
License: MIT License
Hi,
Would it be possible to tag a release for swift 2.0, so that we can add Async as a dependency in our pods?
Async-feature-ArgumentAndReturn/Async.swift:400:15: Cannot invoke 'boost' with an argument list of type '(block: R -> T, chainTo: AsyncO)'
any help ?
In Xcode 6.1.1 Swift public classes need public on each function that is externally accessed. Currently methods like Async.main both static and instance have default access qualifiers, which is now internal meaning only the module defining Async can use them.
when i use Cartfile
github "duemunk/Async" then has an error
Project "AsyncExample.xcodeproj" has no shared schemes
Add Code Coverage to verify that tests cover all the bases.
I made a new Swift project (single view controller) and added Async.swift file. I used the following sample code in my viewDidLoad:
Async.background {
println("A: This is run on the \(qos_class_self().description) (expected \(QOS_CLASS_BACKGROUND.description))")
}.main {
println("B: This is run on the \(qos_class_self().description) (expected \(qos_class_main().description)), after the previous block")
}
Running the project multiple times, the results I get are either:
A: This is run on the User Initiated (expected Background)
B: This is run on the Main (expected Main), after the previous block
Or:
A: This is run on the Main (expected Background)
B: This is run on the Main (expected Main), after the previous block
Never do I get the first block reporting that it is running on the background queue.
Xcode 6.1.1 (6A2008a)
It seems podspec is bumped, but git tag is not set yet.
Can you please add it?
when i run the test project,qos_class_self() func can't get the current thread Qos class,and then the test failed。
If I understand right, the answer is no, because Async is a struct. But I'm not sure...
Thanks for this great project!
public extension qos_class_t {
/**
Description of the `qos_class_t`. E.g. "Main", "User Interactive", etc. for the given Quality of Service class.
*/
var description: String {
get {
switch self {
case qos_class_main(): return "Main"
case QOS_CLASS_USER_INTERACTIVE: return "User Interactive"
case QOS_CLASS_USER_INITIATED: return "User Initiated"
case QOS_CLASS_DEFAULT: return "Default"
case QOS_CLASS_UTILITY: return "Utility"
case QOS_CLASS_BACKGROUND: return "Background"
case QOS_CLASS_UNSPECIFIED: return "Unspecified"
default: return "Unknown"
}
}
}
}
ERROR: binary operator '~=' cannot be applied to two 'qos_class_t' operands
I can't make Travis CI work (spoiler: I haven't used it before). Check it our here.
For every single test, the log states:
Test did not run: the test bundle stopped running or crashed before the test suite started.
The tests are only run for the OS X target.
Hi guys,
I noticed this lib wasn't working anymore. I fixed it this way
public struct Apply {
// DSL for GCD dispatch_apply()
//
// Apply runs a block multiple times, before returning.
// If you want run the block asynchounusly from the current thread,
// wrap it in an Async block,
// e.g. Async.main { Apply.background(3) { ... } }
public static func userInteractive(iterations: UInt, block: UInt -> ()) {
dispatch_apply(Int(iterations), GCD.userInteractiveQueue()) { (iteration) -> Void in
block(UInt(iteration))
return
}
}
public static func userInitiated(iterations: UInt, block: UInt -> ()) {
dispatch_apply(Int(iterations), GCD.userInitiatedQueue()) { (iteration) -> Void in
block(UInt(iteration))
return
}
}
public static func utility(iterations: UInt, block: UInt -> ()) {
dispatch_apply(Int(iterations), GCD.utilityQueue()) { (iteration) -> Void in
block(UInt(iteration))
return
}
}
public static func background(iterations: UInt, block: UInt -> ()) {
dispatch_apply(Int(iterations), GCD.backgroundQueue()) { (iteration) -> Void in
block(UInt(iteration))
return
}
}
public static func customQueue(iterations: UInt, queue: dispatch_queue_t, block: UInt -> ()) {
dispatch_apply(Int(iterations), queue) { (iteration) -> Void in
block(UInt(iteration))
return
}
}
}
installed the pod and when 'Import Async' in the main via controller got this error.
Cannot load underlying module for 'Async'
can build and run the 'AsyncExample iOS' w/o error
total noob here, any advice is appreciated
Unless I add a sleep after the waitForExpectations(...)
block2
doesn't get a chance to fire even if it isn't cancelled. Try removing the block2.cancel()
and see if it still passes.
I've added this to my version to make it fail and then added the block2.cancel
back to make it pass again:
NSThread.sleepForTimeInterval(1.0)
Generally I'm getting a lot of failures on your master when I run your tests. I don't know if it is because I'm on 10.9 so the simulator isn't handling everything properly or something like that. I don't think that this general issue is a problem with your tests.
BTW do you know what happens to blocks chained on a cancelled block? Are they cancelled too or do they run immediately on the cancel? I'm currently implementing cancel behaviour but wasn't sure how the main library (or GCD itself behaves). I'll have another look for the docs later.
https://www.dropbox.com/s/gdk4so5uaq39bsu/Screenshot%202015-11-22%2001.29.16.png?dl=0
What could be the reason for this message?
Async.main(after: 1) {}
Async.customQueue(queue: _, after: 1)
xCode 7 is out so maybe this is the right time :)?
Not sure if this is an issue in Carthage, or the tvOS scheme needs to code sign the framework in the Async project.
*** Building scheme "Async tvOS" in Async.xcodeproj
** BUILD FAILED **
The following build commands failed:
Check dependencies
(1 failure)
CodeSign error: code signing is required for product type 'Framework' in SDK 'tvOS 9.0'
A shell task failed with exit code 65:
** BUILD FAILED **
The following build commands failed:
Check dependencies
(1 failure)
the problem is like the title. When I use carthage to build Async, it shows "Build failed"
carthage_update
produces the following error:
Failed to discover shared schemes in project AsyncPodsExample.xcodeproj—either the project does not have any shared schemes, or xcodebuild never returned
I am using Async 1.6.4 with the following Cartfile:
github "duemunk/Async" ~> 1.6
Hello,
Thanks for the library, this is great!
I am using the library like this:
var lock = NSObject()
self.jobs = [Job]()
Async.utility {
for detailedResponse in responses {
Async.background {
var job = self.readJob(detailedResponse)
self.synced(lock) {
self.jobs!.append(job)
}
return
}
}
while self.jobs?.count != reponses.count {
println("Waiting")
}
return
}.main {
println("JOBS.COUNT: \(self.jobs?.count)")
return
}
where the synced function is found here: https://gist.github.com/Kaelten/7914a8128eca45f081b3
What I'm talking about is line with:
while self.jobs?.count != responses.count {
println("Waiting")
}
This ensures that all iterations of the loop complete before continuing to the next chain.
Maybe have a wait() function with timeout and a condition as well? So that you can pass it a function that returns a bool, and it will wait until either timeout is reached, or condition is true?
Just a thought, and I'm new to swift and GCD so please pardon any newbieness
The pod spec is saying source files are in the Sources directory but they aren't, they're in a Async.swift in the root.
This results in a pod being adding with no source files and no functionality.
I found this bug when trying to use Async in my custom func with background operation and completion block as last parameter, but problem seems to exist in any situation of this kind:
var request = NSMutableURLRequest(URL: kSomeURL)
let session = NSURLSession.sharedSession()
let task = session.dataTaskWithRequest(request, completionHandler: { (data, response, error) -> Void in
dispatch_async(dispatch_get_main_queue(), {
println("this works (even if Async block below is deleted")
})
Async.main {
println("this doesn't work when dispatch_async block above is deleted")
}
})
Any idea?
Unfortunately the dispatch_block_testcancel()
doesn't behave as expected. It just return whether or not you called dispatch_block_cancel()
on a block, not if the block was successfully cancelled before being dispatched to a queue.
If the behavior of dispatch_block_testcancel()
changes, the solution would look as follows:
func cancel() -> Bool {
dispatch_block_cancel(block) // Cancel block
let success = dispatch_block_testcancel(block) // Test if block was successfully cancelled
return success != 0
}
Hello.
I like Async a lot. I fork it and try to make it support Carthage. When I try to create build targets. Interesting things happened.
The module name 'Async' is causing a could not build Objective-C module 'Async'
bug. When I try other names such as SwiftAsync. It works fine.
I'm wondering what is the best choice to support Carthage... What do you think?
Wait for multiple blocks to finish
let block1 = Async.utility {}
let block2 = Async.background {}
Async.main(when: block1, block2) {}
using dispatch_group()
Hi i have this error ...
[!] Error installing Async
[!] /usr/bin/git clone https://github.com/duemunk/Async.git /var/folders/7n/7h94xgjj3c13jc_nhr6xkbl00000gn/T/d20150802-45792-13l6qgp --single-branch --depth 1
Cloning into '/var/folders/7n/7h94xgjj3c13jc_nhr6xkbl00000gn/T/d20150802-45792-13l6qgp'...
fatal: unable to access 'https://github.com/duemunk/Async.git/': Couldn't connect to server
Just a small question does Async honor Swift Capture Lists if you use self
references in an Async.background closure?
I added a podspec to my fork of the repo https://github.com/ksmandersen/Async.
I doesn't lint yet because RC versions of CocoaPods are not publisehd via RubyGems. To use Swift cocoapods you must install CocoaPods from source http://guides.cocoapods.org/using/unreleased-features.
I will hold off on a Pull Request support is official for swift
Currently Async supports passing parameters through an external var like:
var date: NSDate?
Async.background {
date = NSDate()
}.main {
// Access date
}
Possible syntax for a better solution:
Async.background {
let date = NSDate()
}.main(date) {
// Access date
}
I have no idea if or how this is possible – please let me know if you do!
My environment is OS 10.9.4 and Xcode 6 beta 5. I have tried to use Async in my iOS app, but it failed in the simulator (iOS 8).
I didn't modify anything, just run the two projects "AsyncExample iOS" and "AsyncExample iOSTests".
"AsyncExample iOS" printed these:
A: This is run on the Main (expected Background)
B: This is run on the Main (expected Main), after the previous block
"AsyncExample iOSTests" just succeed for the functions "testAsyncMain", "testAfterMain" and "testCancel". Other functions failed because qos_class_self() returned 0, which wasn't equal to any qos class.
Is MAC OS version the reason?
It seems to not work with Swift 2.0, I have corrected the errors however it crashes everytime I use Async.
Issue is here :
private func chain(block chainingBlock: dispatch_block_t, runInQueue queue: dispatch_queue_t) -> Async {
// See Async.async() for comments
let _chainingBlock = dispatch_block_create(DISPATCH_BLOCK_INHERIT_QOS_CLASS, chainingBlock)
dispatch_block_notify(self.block, queue, _chainingBlock)
return Async(_chainingBlock)
}
it crashes on the dispatch_block_notify
Current 23aaf8d (feature/Swift_2.0 branch) doesn't contain any xcodeproj or xcworkspace to build Async.framework, so carthage build
will never get started.
It seems current master branch (v1.2.1) already has one, so please merge it into feature/Swift_2.0 so that I can test Swift 2 using Carthage.
I was wondering if you intended to push this library to Cocoapod's trunk. There is currently an Async
library that would conflict with this one so a name change might be needed. This would allow users to use pod 'AsyncSwift'
in their Podfile
Thanks for the amazing work 👍
It can be very useful if Async can use with async operations.
Right now, I think Async can use only with sync operations.
Async.background {
// do some "sync" stuff 1
}.background {
// do some "sync" stuff 2 after sync stuff 1
}
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.
Async.background {
// async upload
Alamofire.upload(.POST, "http://httpbin.org/post", file: fileURL)
.reponseJSON { (_, _, JSON, _) in
// upload done!!
}
}.background {
// do some stuff S after upload done!! ------>> Sorry S() is doing right away and not wait
S()
}
An idea for accomplish this is something like passing a done
block and waiting for done()
to be called to continue to the next Async chain. I saw Quick/Nimble use this for waitUntil
operation here.
So, here is a use case proposal.
Async.background { done in
// async upload
Alamofire.upload(.POST, "http://httpbin.org/post", file: fileURL)
.reponseJSON { (_, _, JSON, _) in
// upload done!!
done()
}
}.background {
// do some "sync" stuff S1 after upload done!!
S1()
}.background { done in
// do some "async" stuff A1 after S1
A1 { done() }
}.main {
// All uploading, S1 and A1 are done, updates UI here
}
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?
I just added this project via Cocoapods and it won't compile (haven't tried to use it yet, so I haven't written any code that uses it). It gives me the error in the title.
This happens here:
// MARK: - qos_class_t
public extension qos_class_t {
// Convenience description of qos_class_t
// Calculated property
var description: String {
get {
switch self {
case qos_class_main(): return "Main"
case QOS_CLASS_USER_INTERACTIVE: return "User Interactive"
case QOS_CLASS_USER_INITIATED: return "User Initiated"
case QOS_CLASS_DEFAULT: return "Default"
case QOS_CLASS_UTILITY: return "Utility"
case QOS_CLASS_BACKGROUND: return "Background"
case QOS_CLASS_UNSPECIFIED: return "Unspecified"
default: return "Unknown"
}
}
}
}
The error shows up in all cases.
I will see if I can fix this. I may just delete the description method in the extension (but don't worry, I won't be pushing that).
I will suggest to add an option to call a block either sync on main thread if already on main thread, or else async dispatch on the main thread.
I have this usecase quite offen.
It would be great if we can get Carthage (https://github.com/Carthage/Carthage) support for this
When I install it with a cocoapods, I need to use import AsyncSwift
instead of import Async
set .module_name for podspec
spec.module_name = 'Async'
Hi,
You said "I've found no need for dispatch_once in Swift" in #2 .
Could you please explain why no need to use dispatch_one in Swift?
Thanks.
A declarative, efficient, and flexible JavaScript library for building user interfaces.
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
An Open Source Machine Learning Framework for Everyone
The Web framework for perfectionists with deadlines.
A PHP framework for web artisans
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
Some thing interesting about web. New door for the world.
A server is a program made to process requests and deliver data to clients.
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
Some thing interesting about visualization, use data art
Some thing interesting about game, make everyone happy.
We are working to build community through open source technology. NB: members must have two-factor auth.
Open source projects and samples from Microsoft.
Google ❤️ Open Source for everyone.
Alibaba Open Source for everyone
Data-Driven Documents codes.
China tencent open source team.