Swift UI 回调中的更新有时不执行
Swift UI update in callback is sometimes not executed
当执行下面的代码时,我看到回调执行(由 "success!" 验证),但是 UI activityIndicator 上的 activityIndicator 没有停止旋转。
有时会在 10-20 秒后停止,有时会 运行 永远停止。回调应该在主线程上 运行ning,所以我不确定是什么导致它延迟/失败。
activityIndicator.startAnimating()
loadData("data") {(success: Bool) in
if success {
// I see this being printed
println("success!")
}
// This isn't updating on the UI! The wheel keeps spinning
self.activityIndicator.stopAnimating()
}
loadData 函数正在使用 Core Data 执行块保存:
public func loadData(completionHandler: (success: Bool) -> Void) {
let appDelegate = UIApplication.sharedApplication().delegate as AppDelegate
let appContext = appDelegate.managedObjectContext!
let managedContext = NSManagedObjectContext(concurrencyType: .PrivateQueueConcurrencyType)
managedContext.persistentStoreCoordinator = appContext.persistentStoreCoordinator
managedContext.performBlock({
//NSManagedObject inserts - working...
if !managedContext.save(&error) {
completionHandler(success: false)
}
completionHandler(success: true)
})
}
非常感谢任何帮助!
activityIndicator.startAnimating()
loadData("data") {(success: Bool) in
if success {
// I see this being printed
println("success!")
}
dispatch_async(dispatch_get_main_queue()) {
// This isn't updating on the UI! The wheel keeps spinning
self.activityIndicator.stopAnimating()
}
}
我还注意到,如果 error.So 在第一个完成块之后添加 return,您将调用完成块两次。像这样:
public func loadData(completionHandler: (success: Bool) -> Void) {
let appDelegate = UIApplication.sharedApplication().delegate as AppDelegate
let appContext = appDelegate.managedObjectContext!
let managedContext = NSManagedObjectContext(concurrencyType: .PrivateQueueConcurrencyType)
managedContext.persistentStoreCoordinator = appContext.persistentStoreCoordinator
managedContext.performBlock({
//NSManagedObject inserts - working...
if !managedContext.save(&error) {
completionHandler(success: false)
return
}
completionHandler(success: true)
})
}
我有一个类似的问题并通过首先为 activity 指示器设置动画,然后将其余代码放到后台线程,然后在主线程上调用停止动画来解决它。
activityIndicator.startAnimating()
dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_HIGH, 0), { ()->() in
loadData("data") {(success: Bool) in
if success {
// I see this being printed
println("success!")
}
// This isn't updating on the UI! The wheel keeps spinning
dispatch_async(dispatch_get_main_queue(), {
self.activityIndicator.stopAnimating()
})
}
我没有更新这里的右括号,假设你有更多,因为你正在使用闭包。
当执行下面的代码时,我看到回调执行(由 "success!" 验证),但是 UI activityIndicator 上的 activityIndicator 没有停止旋转。
有时会在 10-20 秒后停止,有时会 运行 永远停止。回调应该在主线程上 运行ning,所以我不确定是什么导致它延迟/失败。
activityIndicator.startAnimating()
loadData("data") {(success: Bool) in
if success {
// I see this being printed
println("success!")
}
// This isn't updating on the UI! The wheel keeps spinning
self.activityIndicator.stopAnimating()
}
loadData 函数正在使用 Core Data 执行块保存:
public func loadData(completionHandler: (success: Bool) -> Void) {
let appDelegate = UIApplication.sharedApplication().delegate as AppDelegate
let appContext = appDelegate.managedObjectContext!
let managedContext = NSManagedObjectContext(concurrencyType: .PrivateQueueConcurrencyType)
managedContext.persistentStoreCoordinator = appContext.persistentStoreCoordinator
managedContext.performBlock({
//NSManagedObject inserts - working...
if !managedContext.save(&error) {
completionHandler(success: false)
}
completionHandler(success: true)
})
}
非常感谢任何帮助!
activityIndicator.startAnimating()
loadData("data") {(success: Bool) in
if success {
// I see this being printed
println("success!")
}
dispatch_async(dispatch_get_main_queue()) {
// This isn't updating on the UI! The wheel keeps spinning
self.activityIndicator.stopAnimating()
}
}
我还注意到,如果 error.So 在第一个完成块之后添加 return,您将调用完成块两次。像这样:
public func loadData(completionHandler: (success: Bool) -> Void) {
let appDelegate = UIApplication.sharedApplication().delegate as AppDelegate
let appContext = appDelegate.managedObjectContext!
let managedContext = NSManagedObjectContext(concurrencyType: .PrivateQueueConcurrencyType)
managedContext.persistentStoreCoordinator = appContext.persistentStoreCoordinator
managedContext.performBlock({
//NSManagedObject inserts - working...
if !managedContext.save(&error) {
completionHandler(success: false)
return
}
completionHandler(success: true)
})
}
我有一个类似的问题并通过首先为 activity 指示器设置动画,然后将其余代码放到后台线程,然后在主线程上调用停止动画来解决它。
activityIndicator.startAnimating()
dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_HIGH, 0), { ()->() in
loadData("data") {(success: Bool) in
if success {
// I see this being printed
println("success!")
}
// This isn't updating on the UI! The wheel keeps spinning
dispatch_async(dispatch_get_main_queue(), {
self.activityIndicator.stopAnimating()
})
}
我没有更新这里的右括号,假设你有更多,因为你正在使用闭包。