當執行下面的代碼時,我會看到回調execute(通過「success!」驗證),但UI activityIndicator上的activityIndicator不會停止旋轉。回調中的Swift UI更新有時不會執行
有時它會在10-20秒後停止,其他時間會永久運行。回調應該在主線程上運行,所以我不確定是什麼導致它延遲/失敗。
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函數保存使用核心數據執行塊:
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)
})
}
這裏的任何幫助深表感謝!
「應該在主線程中運行。」 - 你用調試器確認是這種情況? – nhgrif 2015-04-01 20:53:00
@nhgrif可能是對的。所有UI代碼都應該在主線程上執行。 – 2015-04-01 21:01:08
是的@nhgrif,你是對的......我的錯誤假設。我做了下面的更新Christos Hadjikyriacou建議,現在一切正常。謝謝您的幫助! – StCleezy 2015-04-02 02:28:54