0

我有以下代码。任何线索为什么我偶尔会遇到死锁?大多数情况下它工作得很好,似乎当发生 wi-fi 到 3g 的转换或应用程序处于非活动状态并恢复运行时,就会发生死锁。

-(void) dictionaryFromJSONWithURL: (NSURL *) URL callback: (void (^)(NSDictionary* resp, NSError* error)) block{

#ifdef TX_DEBUG
    NSLog(@"%s", __FUNCTION__);
#endif

dispatch_async( dispatch_get_global_queue( DISPATCH_QUEUE_PRIORITY_HIGH, 0 ), ^(void){
    NSData* data = [NSData dataWithContentsOfURL:URL ];

    NSError* error = nil;
    NSDictionary* dict = [NSJSONSerialization 
                          JSONObjectWithData:data //1                              
                          options:kNilOptions 
                          error:&error];

    dispatch_sync(dispatch_get_main_queue(), ^{
        if(error || !dict) block(nil, error);
        else block(dict, nil);
    });

});
4

1 回答 1

0
dispatch_sync(dispatch_get_main_queue(), ^{
        if(error || !dict) block(nil, error);
        else block(dict, nil);
    });

可能是问题所在,您不应该同步调度到主队列。

它并不总是锁定的事实引起了我的兴趣。

于 2012-06-01T06:09:23.257 回答