【发布时间】:2015-03-21 17:38:43
【问题描述】:
我正面临一些我无法解决的问题。我正在下载 json 数据并使用返回的值(在 dispatch_async(get_main_queue) 内)实例化 Core Data 对象。一切都完成后,我尝试呈现另一个视图(tableView 包含这些对象),但我的 tableviewcontrollers 没有被调用。但是,如果我从该块之外的方法(在 connectionDidFinishing NSURLConnection 内部)呈现我的 viewController,一切正常。
- (void)connectionDidFinishLoading:(NSURLConnection *)connection {
id jsonObject = [NSJSONSerialization JSONObjectWithData:_downloadedData options:NSJSONReadingAllowFragments error:&error];
if ([jsonObject isKindOfClass:[NSArray class]]) {
NSArray *deserializedArray = (NSArray *)jsonObject;
if (deserializedArray.count > 0) {
dispatch_async(dispatch_get_main_queue(), ^{
for (NSDictionary *jsonElement in deserializedArray)
{
// Create a new location object and set its props to JsonElement properties
Patient *syncPatient = [[PatientStore sharedStore] createPatient];
syncPatient.firstName = jsonElement[@"firstname"];
syncPatient.lastName = jsonElement[@"lastname"];
}
});
[self login]; // --> does not work.
}
}
}
[自我登录] 不起作用。但如果我从 didFinishLoading 方法之外调用它,它就可以工作。
我认为这与 dispatch_async() 有关,但我不知道如何从外部自动调用此方法(例如使用“我已完成”通知)。
有人可以帮忙吗?
非常感谢!
更新
它在块内也不起作用。看起来 [self login] 发生在 for 循环指令之前。
if (deserializedArray.count > 0) {
dispatch_async(dispatch_get_main_queue(), ^{
for (NSDictionary *jsonElement in deserializedArray)
{
// Create a new location object and set its props to JsonElement properties
Patient *syncPatient = [[PatientStore sharedStore] createPatient];
syncPatient.firstName = jsonElement[@"firstname"];
syncPatient.lastName = jsonElement[@"lastname"];
}
[self login]; // --> does not work either here. As if it was executed before what is inside the for loop.
});
}
} }
【问题讨论】:
标签: ios core-data queue grand-central-dispatch dispatch-async