supertommy / craft Goto Github PK
View Code? Open in Web Editor NEWA promise library based on the Promises/A+ standard written in Swift for iOS and OSX
License: MIT License
A promise library based on the Promises/A+ standard written in Swift for iOS and OSX
License: MIT License
Hi,
I'm creating an iOS app with a table view and a spinner to load data over the network (which is mocked with a call to sleep, so far).
The problem is that the UI is blocked until the promise returns.
Here is my code :
/**
* Called when the spinner animates
*/
func refresh(sender:AnyObject)
{
println("DataViewController.refresh() : begin")
API.retrievePosts().then({(value: Value) -> Value in
dispatch_async(dispatch_get_main_queue()) {
self.data += ["It", "Does", "Work"]
self.tableView.reloadData()
self.refreshControl.endRefreshing()
}
println("DataViewController.refresh() : data successfully refreshed")
return nil
}, reject: {(value: Value) -> Value in
dispatch_async(dispatch_get_main_queue()) {
self.refreshControl.endRefreshing()
self.setMessage(NSLocalizedString("loading_posts_failed", comment: "text saying that posts could not be loaded"))
}
println("DataViewController.refresh() : Error while refreshing data : \(value)")
return nil
})
// if I wait 3 seconds with the method below instead, the UI does not block
// var delayInSeconds = 3.0;
// var popTime = dispatch_time(DISPATCH_TIME_NOW, Int64(delayInSeconds * Double(NSEC_PER_SEC)));
// dispatch_after(popTime, dispatch_get_main_queue()) { () -> Void in
// // When done requesting/reloading/processing invoke endRefreshing, to close the control
// self.refreshControl!.endRefreshing()
// }
println("refreshing")
return
}
And here is the mock method to retrieve posts :
public static func retrievePosts() -> Promise {
++j
return Craft.promise({(resolve: (value: Value) -> (), reject: (value: Value) -> ()) -> () in
sleep(3)
if self.j % 3 == 0 {// fails once every 3 attempt
reject(value: "Network failure")
} else {
resolve(value: nil)
}
})
}
So it seems that the promise runs on the UI thread. Or at least the sleep statement.
in the sample, i see you have 1 chaining for async, how to chain multiple async?
Hi, thanks for providing this library. Would you plan to update the library to support Xcode 6.1? It currently has the following compilation error.
promise.then({
(value: Value) -> Value in
let res = SettledResult(state: PromiseState.FULFILLED, value: value)
results[index] = res
// *** cannot reference a local function within captures from another local function ***
return response(value)
}, reject: {
(value: Value) -> Value in
let res = SettledResult(state: PromiseState.REJECTED, value: value)
results[index] = res
// *** cannot reference a local function within captures from another local function ***
return response(value)
})
Thanks
Charles
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.