當有人登錄我的應用程序時,會創建一個帶有JSON響應的調用,然後使用此數據將其導入到使用MagicalRecord的Core Data中。 所有導入完成後,調用一個名爲success的塊,讓我的LoginViewController知道一切正常,並停止UIActivityIndicatorView
,這樣就可以創建一個segue。MagicalRecord完成塊在導入時保存所有內容
它指向一個新的ViewController,它有一個UITableView
來顯示剛導入的數據。
我的問題是,這個成功塊被稱爲之前 MagicalRecord完成與節省。因此,UITableView
尚未收到要顯示的訂單對象。
這是我實現saveWithBlockAndWait
塊是如下:
[MagicalRecord saveWithBlockAndWait:^(NSManagedObjectContext *localContext) {
NSDictionary *users = [responseObject valueForKeyPath:@"responses.user"];
[User importFromObject:users inContext:localContext];
NSArray *orders = [responseObject valueForKeyPath:@"responses.orders"];
[Order importFromArray:orders inContext:localContext];
NSLog(@"SAVING DONE");
}];
在我OrdersViewController
(女巫是ViewController中有許多人也賽格瑞)是另一個NSLog
,並且它是這裏的問題可能是。
Xcode的輸出顯示SAVING DONE已被調用,但在我的AFNetworking調用之後。這意味着所有的數據都不會保存,並且不準備顯示 - 即使是saveWithBlockAndWait
。此外,輸出顯示有0行。
該應用程序在我的第二次啓動時完美運行。它僅在第一次登錄我有這個問題
2015-05-04 19:16:31.617 xxxxx[35555:5569667] +[NSManagedObjectContext(MagicalRecord) MR_contextWithStoreCoordinator:](0xff4a04) -> Created Context UNNAMED
2015-05-04 19:16:31.618 xxxxx[35555:5569667] +[NSManagedObjectContext(MagicalRecord) MR_setRootSavingContext:](0xff4a04) Set Root Saving Context: <NSManagedObjectContext: 0x7aecdf10>
2015-05-04 19:16:31.618 xxxxx[35555:5569667] +[NSManagedObjectContext(MagicalRecord) MR_newMainQueueContext](0xff4a04) Created Main Queue Context: <NSManagedObjectContext: 0x7aff4280>
2015-05-04 19:16:31.618 xxxxx[35555:5569667] +[NSManagedObjectContext(MagicalRecord) MR_setDefaultContext:](0xff4a04) Set Default Context: <NSManagedObjectContext: 0x7aff4280>
2015-05-04 19:16:46.104 xxxxx[35555:5569667] POST 'secret'
2015-05-04 19:16:47.237 xxxxx[35555:5569667] 200 'secret' [1.1324 s]
2015-05-04 19:16:47.258 xxxxx[35555:5569667] POST 'secret'
2015-05-04 19:16:47.880 xxxxx[35555:5569667] 200 'secret' [0.6215 s]
2015-05-04 19:16:47.901 xxxxx[35555:5569667] SAVING DONE
2015-05-04 19:16:47.902 xxxxx[35555:5569667] -[NSManagedObjectContext(MagicalSaves) MR_saveWithOptions:completion:](0x7ae251e0) → Saving <NSManagedObjectContext (0x7ae251e0): *** UNNAMED ***> on *** MAIN THREAD ***
2015-05-04 19:16:47.903 xxxxx[35555:5569667] -[NSManagedObjectContext(MagicalSaves) MR_saveWithOptions:completion:](0x7ae251e0) → Save Parents? 1
2015-05-04 19:16:47.903 xxxxx[35555:5569667] -[NSManagedObjectContext(MagicalSaves) MR_saveWithOptions:completion:](0x7ae251e0) → Save Synchronously? 1
2015-05-04 19:16:47.903 xxxxx[35555:5569667] -[NSManagedObjectContext(MagicalRecord) MR_contextWillSave:](0x7ae251e0) Context UNNAMED is about to save. Obtaining permanent IDs for new 2 inserted objects
2015-05-04 19:16:47.906 xxxxx[35555:5569667] -[NSManagedObjectContext(MagicalSaves) MR_saveWithOptions:completion:](0x7aecdf10) → Saving <NSManagedObjectContext (0x7aecdf10): *** BACKGROUND SAVING (ROOT) ***> on *** MAIN THREAD ***
2015-05-04 19:16:47.906 xxxxx[35555:5569667] -[NSManagedObjectContext(MagicalSaves) MR_saveWithOptions:completion:](0x7aecdf10) → Save Parents? 1
2015-05-04 19:16:47.906 xxxxx[35555:5569667] -[NSManagedObjectContext(MagicalSaves) MR_saveWithOptions:completion:](0x7aecdf10) → Save Synchronously? 1
2015-05-04 19:16:47.907 xxxxx[35555:5569667] -[NSManagedObjectContext(MagicalRecord) MR_contextWillSave:](0x7aecdf10) Context BACKGROUND SAVING (ROOT) is about to save. Obtaining permanent IDs for new 2 inserted objects
2015-05-04 19:16:47.909 xxxxx[35555:5569667] __70-[NSManagedObjectContext(MagicalSaves) MR_saveWithOptions:completion:]_block_invoke21(0x7aecdf10) → Finished saving: <NSManagedObjectContext (0x7aecdf10): *** BACKGROUND SAVING (ROOT) ***> on *** MAIN THREAD ***
2015-05-04 19:16:47.909 xxxxx[35555:5569667] All data has been setup sucessfully
2015-05-04 19:16:47.909 xxxxx[35555:5569667] Perform Segue To LoginSucessSegue from LoginViewController
2015-05-04 19:16:47.945 xxxxx[35555:5570918] -[NSManagedObjectContext(MagicalSaves) MR_saveWithOptions:completion:](0x7ae19130) → Saving <NSManagedObjectContext (0x7ae19130): *** UNNAMED ***> on *** BACKGROUND THREAD ***
2015-05-04 19:16:47.952 xxxxx[35555:5569667] Number of rows: 0
2015-05-04 19:16:47.957 xxxxx[35555:5570918] -[NSManagedObjectContext(MagicalSaves) MR_saveWithOptions:completion:](0x7ae19130) → Save Parents? 1
2015-05-04 19:16:47.957 xxxxx[35555:5570918] -[NSManagedObjectContext(MagicalSaves) MR_saveWithOptions:completion:](0x7ae19130) → Save Synchronously? 0
2015-05-04 19:16:47.958 xxxxx[35555:5570918] -[NSManagedObjectContext(MagicalRecord) MR_contextWillSave:](0x7ae19130) Context UNNAMED is about to save. Obtaining permanent IDs for new 4 inserted objects
2015-05-04 19:16:47.962 xxxxx[35555:5570918] -[NSManagedObjectContext(MagicalSaves) MR_saveWithOptions:completion:](0x7aecdf10) → Saving <NSManagedObjectContext (0x7aecdf10): *** BACKGROUND SAVING (ROOT) ***> on *** BACKGROUND THREAD ***
2015-05-04 19:16:47.963 xxxxx[35555:5570918] -[NSManagedObjectContext(MagicalSaves) MR_saveWithOptions:completion:](0x7aecdf10) → Save Parents? 1
2015-05-04 19:16:48.023 xxxxx[35555:5570918] -[NSManagedObjectContext(MagicalSaves) MR_saveWithOptions:completion:](0x7aecdf10) → Save Synchronously? 0
2015-05-04 19:16:48.024 xxxxx[35555:5570924] -[NSManagedObjectContext(MagicalRecord) MR_contextWillSave:](0x7aecdf10) Context BACKGROUND SAVING (ROOT) is about to save. Obtaining permanent IDs for new 4 inserted objects
2015-05-04 19:16:48.026 xxxxx[35555:5570924] __70-[NSManagedObjectContext(MagicalSaves) MR_saveWithOptions:completion:]_block_invoke21(0x7aecdf10) → Finished saving: <NSManagedObjectContext (0x7aecdf10): *** BACKGROUND SAVING (ROOT) ***> on *** BACKGROUND THREAD ***
是對saveWithBlockAndWait
得到執行錯誤的,因爲它不是爲我工作?關於如何解決這個問題的建議或解決方案將非常棒。
我同意。最好使用異步方法。在我得到一個異常之前,因爲我試圖阻止成功,但該變量也被用作此塊中的BOOL - 我今天早上剛剛看到它,這就是爲什麼我沒有使用'saveWithBlock'。我現在已經更改了我的代碼,因此該塊被稱爲成功。 感謝您的幫助 –