当有人登录我的应用程序时,会进行带有JSON响应的调用,并使用此数据将其导入到带有MagicalRecord的Core Data中。
完成所有导入后,将调用一个名为success的块,让我的LoginViewController知道一切正常,并停止p
,这样就可以生成一个segue。
它支持新的ViewController,它有一个UIActivityIndicatorView
来显示刚刚导入的数据。
我的问题是,这个成功模块被称为之前 MagicalRecord完成了节省。因此,UITableView
尚未收到要显示的订单对象。
以下是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");
}];
(女巫是ViewController,也有segue)是另一个OrdersViewController
,问题可能在这里。
Xcode的输出显示已经调用了SAVING DONE,但是在我的AFNetworking调用之后。这意味着即使使用NSLog
,所有数据都不会保存,也无法显示。此外,输出显示有0行。
该应用程序在我的第二次发布时运作良好。只有在第一次登录时我才遇到这个问题
saveWithBlockAndWait
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 ***
是否被错误地执行了,因为它对我不起作用?关于如何解决这个问题的建议或解决方案将是非常棒的。
答案 0 :(得分:0)
您的日志似乎正确无误。首先它运行你的块,然后它将所有内容保存到数据库,然后当前线程继续执行。
您使用同步方法。同步方法阻止当前线程,直到它们完成执行。如果要更新界面,则必须在saveWithBlockAndWait
之后立即执行此操作,或使用提供完成处理程序的“save”方法的异步版本。
[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.");
通常,最好使用异步方法来避免阻塞主线程。
[MagicalRecord saveWithBlock:^(NSManagedObjectContext *localContext) {
// create or update CoreData models
} completion:^(BOOL contextDidSave, NSError *error) {
if(error) {
NSLog(@"Failed to save data");
return;
}
NSLog(@"Saving done.");
[self.activityIndicator stopAnimating];
}];