当我有异步请求时,我无法更改 TabBarViewController
I can't change TabBarViewController when I Have an assync request
在我提出要求并找到并填写我的对象之前,我无法改变我的观点。
我试图让我的代码与 GCD 异步。那行不通
override func viewDidLoad() {
getHeartStroke()
NotificationCenter.default.addObserver(forName:NSNotification.Name("HeartStrokeNotification"), object: nil, queue: nil, using: notificationFinish)
}
func getHeartStroke() {
AF.request("http://localhost:8080/heartstroke", method: .get, headers: nil).responseJSON(completionHandler: {response in
if (response.error == nil)
{
let json = JSON(response.result.value!)
DispatchQueue.global(qos: .userInitiated).async {
guard let hearstrokeArray = try? JSONDecoder().decode([HeartStroke].self, from: json.rawData()) else{
debugPrint("An error has occurred")
return
}
NotificationCenter.default.post(name:NSNotification.Name("HeartStrokeNotification"), object: hearstrokeArray, userInfo: nil)
}
}else{
NotificationCenter.default.post(name:NSNotification.Name("HeartStrokeErrorNotification"), object: nil, userInfo: nil)
}
})
}
func notificationFinish(notification:Notification) -> Void{
if (notification.name.rawValue == "HeartStrokeNotification"){
arrayHeartstroke = notification.object as! [HeartStroke]
DispatchQueue.main.async(execute: {
self.tableView.reloadData()
})
}
使用此代码,在 getHeartStroke() 结束之前,我会一直阻塞在我的页面上,我希望在获取的同时在我的应用程序中导航。
您需要一个完成处理程序来处理这个问题。使用通知中心只会让您的生活变得困难和复杂,并可能导致意外行为。这是一些示例代码:
func getHeartStroke(completionHandler: (_ heartStroke: [HeartStroke?], _ error: NSError?) -> ()) {
AF.request("http://localhost:8080/heartstroke", method: .get, headers: nil).responseJSON(completionHandler: {response in
if (response.error == nil)
{
let json = JSON(response.result.value!)
DispatchQueue.global(qos: .userInitiated).async {
guard let hearstrokeArray = try? JSONDecoder().decode([HeartStroke].self, from: json.rawData()) else{
debugPrint("An error has occurred")
return
}
completionHandler(hearstrokeArray, nil)
}
} else {
completionHandler(nil, response.error))
}
})
}
那么你可以这样称呼它:
getHeartStroke { [weak self] (heartStrokeArray, error) in
guard let self = self else {return}
if error != nil {
self.processError(error)
} else {
self.processHeartStroke(heartStrokeArray)
}
}
processError 和 processHeartStroke 将是您应该创建的函数来处理 heartStrokeArray 和错误对象。
这些是标准回调或将函数传递给函数。您在网上找到的很多课程似乎都忽略了回调,但绝对值得您花时间了解它们。
您可以在此处了解有关闭包(此处命名为 completionHandler)的更多信息:https://docs.swift.org/swift-book/LanguageGuide/Closures.html
在我提出要求并找到并填写我的对象之前,我无法改变我的观点。
我试图让我的代码与 GCD 异步。那行不通
override func viewDidLoad() {
getHeartStroke()
NotificationCenter.default.addObserver(forName:NSNotification.Name("HeartStrokeNotification"), object: nil, queue: nil, using: notificationFinish)
}
func getHeartStroke() {
AF.request("http://localhost:8080/heartstroke", method: .get, headers: nil).responseJSON(completionHandler: {response in
if (response.error == nil)
{
let json = JSON(response.result.value!)
DispatchQueue.global(qos: .userInitiated).async {
guard let hearstrokeArray = try? JSONDecoder().decode([HeartStroke].self, from: json.rawData()) else{
debugPrint("An error has occurred")
return
}
NotificationCenter.default.post(name:NSNotification.Name("HeartStrokeNotification"), object: hearstrokeArray, userInfo: nil)
}
}else{
NotificationCenter.default.post(name:NSNotification.Name("HeartStrokeErrorNotification"), object: nil, userInfo: nil)
}
})
}
func notificationFinish(notification:Notification) -> Void{
if (notification.name.rawValue == "HeartStrokeNotification"){
arrayHeartstroke = notification.object as! [HeartStroke]
DispatchQueue.main.async(execute: {
self.tableView.reloadData()
})
}
使用此代码,在 getHeartStroke() 结束之前,我会一直阻塞在我的页面上,我希望在获取的同时在我的应用程序中导航。
您需要一个完成处理程序来处理这个问题。使用通知中心只会让您的生活变得困难和复杂,并可能导致意外行为。这是一些示例代码:
func getHeartStroke(completionHandler: (_ heartStroke: [HeartStroke?], _ error: NSError?) -> ()) {
AF.request("http://localhost:8080/heartstroke", method: .get, headers: nil).responseJSON(completionHandler: {response in
if (response.error == nil)
{
let json = JSON(response.result.value!)
DispatchQueue.global(qos: .userInitiated).async {
guard let hearstrokeArray = try? JSONDecoder().decode([HeartStroke].self, from: json.rawData()) else{
debugPrint("An error has occurred")
return
}
completionHandler(hearstrokeArray, nil)
}
} else {
completionHandler(nil, response.error))
}
})
}
那么你可以这样称呼它:
getHeartStroke { [weak self] (heartStrokeArray, error) in
guard let self = self else {return}
if error != nil {
self.processError(error)
} else {
self.processHeartStroke(heartStrokeArray)
}
}
processError 和 processHeartStroke 将是您应该创建的函数来处理 heartStrokeArray 和错误对象。
这些是标准回调或将函数传递给函数。您在网上找到的很多课程似乎都忽略了回调,但绝对值得您花时间了解它们。
您可以在此处了解有关闭包(此处命名为 completionHandler)的更多信息:https://docs.swift.org/swift-book/LanguageGuide/Closures.html