Swift - Grand Central Dispatch Class
Swift - Grand Central Dispatch Class
我在中心点 (class) 工作,请求和检索我的数据。
这个class可以从任何视图调用:
override func viewDidLoad() {
super.viewDidLoad()
let group = dispatch_group_create()
var data = RestApiManager(group)
dispatch_group_notify(group, dispatch_get_main_queue()) {
return data
}
}
class RestApiManager {
let user = "user"
let password = "password"
var result: JSON = []
init(group) {
dispatch_group_enter(group)
Alamofire.request(.GET, "http://example.com/api/")
.authenticate(user: user, password: password)
.responseJSON { _, _, result in
switch result {
case .Success(let data):
self.result[] = JSON(data)
case .Failure(_, let error):
print("Request failed with error: \(error)")
}
dispatch_group_leave(group)
}
}
}
但是初始化class时已经失败了。
谁能帮我设计一个合适的方法??
谢谢和问候
import XCPlayground
XCPlaygroundPage.currentPage.needsIndefiniteExecution = true
import Foundation
class C {
var txt: String = "start"
init(group: dispatch_group_t) {
dispatch_group_enter(group)
print(txt)
dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0)) { () -> Void in
usleep(200000)
self.txt = "end"
print(self.txt)
dispatch_group_leave(group)
}
}
}
func f() {
let group = dispatch_group_create()
let c = C(group: group)
dispatch_group_notify(group, dispatch_get_main_queue()) { () -> Void in
print("notify: \(c) created")
}
}
f()
print("continue running ...")
这对我来说没有任何问题。你写了
"But it already fails when initializing the class."
这到底是什么意思?详细解释你的 'failure' ...
顺便说一下,我不明白你如何从组通知处理程序中 return 你的数据(return 数据)...(通常来自任何异步代码)
我在中心点 (class) 工作,请求和检索我的数据。
这个class可以从任何视图调用:
override func viewDidLoad() {
super.viewDidLoad()
let group = dispatch_group_create()
var data = RestApiManager(group)
dispatch_group_notify(group, dispatch_get_main_queue()) {
return data
}
}
class RestApiManager {
let user = "user"
let password = "password"
var result: JSON = []
init(group) {
dispatch_group_enter(group)
Alamofire.request(.GET, "http://example.com/api/")
.authenticate(user: user, password: password)
.responseJSON { _, _, result in
switch result {
case .Success(let data):
self.result[] = JSON(data)
case .Failure(_, let error):
print("Request failed with error: \(error)")
}
dispatch_group_leave(group)
}
}
}
但是初始化class时已经失败了。
谁能帮我设计一个合适的方法??
谢谢和问候
import XCPlayground
XCPlaygroundPage.currentPage.needsIndefiniteExecution = true
import Foundation
class C {
var txt: String = "start"
init(group: dispatch_group_t) {
dispatch_group_enter(group)
print(txt)
dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0)) { () -> Void in
usleep(200000)
self.txt = "end"
print(self.txt)
dispatch_group_leave(group)
}
}
}
func f() {
let group = dispatch_group_create()
let c = C(group: group)
dispatch_group_notify(group, dispatch_get_main_queue()) { () -> Void in
print("notify: \(c) created")
}
}
f()
print("continue running ...")
这对我来说没有任何问题。你写了 "But it already fails when initializing the class." 这到底是什么意思?详细解释你的 'failure' ... 顺便说一下,我不明白你如何从组通知处理程序中 return 你的数据(return 数据)...(通常来自任何异步代码)