[英]cannot execute method insde dispatch_async()
I am facing something I cannot sort out. 我面临着无法解决的问题。 I am downloading json data and instantiating Core Data objects with the returned value (inside a dispatch_async(get_main_queue)).
我正在下载json数据并使用返回值实例化Core Data对象(在dispatch_async(get_main_queue)内部)。 I try to present an other view (with tableView containing these objects) after everything is completed but my tableviewcontrollers are not called.
一切完成后,我尝试呈现另一个视图(带有包含这些对象的tableView),但未调用我的tableviewcontrollers。 However everything works fine if I present my viewController from a method outside this block (which is inside connectionDidFinishing NSURLConnection).
但是,如果我从该块外部的方法(位于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.
}
}
}
[self login] does not work. [自我登录]不起作用。 But it works if I call it from outside the didFinishLoading method.
但是,如果我从didFinishLoading方法外部调用它,它将起作用。
It has to do I think with the dispatch_async() but I don't know how to call this method automatically from outside (for example with a "I have finish" notification). 我认为它必须与dispatch_async()一起使用,但我不知道如何从外部自动调用此方法(例如,使用“我已经完成”通知)。
Does anyone could help? 有人可以帮忙吗?
Thanks a lot! 非常感谢!
UPDATE 更新
It does not work either inside the block. 块内部均不起作用。 It looks like [self login] happened before the for loop instructions.
看起来[自我登录]发生在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.
});
}
} } }}
[self login]
is executed immediately after the core data save starts because it is outside the block. 由于核心数据保存在块外 ,因此在开始保存核心数据后立即执行
[self login]
。 If you put it inside, it will execute after the loop finishes. 如果将其放入内部,它将在循环完成后执行。
You can test this with a simple command line project, which this content in main.swift: 您可以使用一个简单的命令行项目对此进行测试,该项目包含在main.swift中:
import Foundation
dispatch_async(dispatch_get_main_queue()) {
var j = 0
for _ in 1...1000 { j++ }
println("The value of j is \(j).")
}
dispatch_main()
Output: The value of j is 1000.
输出:
The value of j is 1000.
The println
is clearly executed after the loop. 循环后显然会执行
println
。
After creating your objects, do not forget to save: 创建对象后,请不要忘记保存:
[managedObjectContext save:nil];
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.