首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >完成块阻止AFJSONRequestOperation执行

完成块阻止AFJSONRequestOperation执行
EN

Stack Overflow用户
提问于 2013-08-24 10:18:54
回答 1查看 164关注 0票数 0

我正在使用AFNetworking库检索JSON数据,并希望添加一个完成块,如下所示:

代码语言:javascript
复制
-(void)getData{

    NSString *link=@"http://localhost:8080/address/address/address/";

    NSURL *url= [ NSURL URLWithString:link];

    NSURLRequest *request = [NSURLRequest requestWithURL:url];

    AFJSONRequestOperation *operation = [AFJSONRequestOperation JSONRequestOperationWithRequest:request success:^(NSURLRequest *request, NSHTTPURLResponse *response, id JSON) {
        arrayofA=[JSON valueForKeyPath:@"a"];
        arrayofB=[JSON valueForKeyPath:@"b"];


        [[self techTableView] reloadData];

    } failure:nil];


    [operation setDownloadProgressBlock:^(NSUInteger bytesRead, long long totalBytesRead, long long totalBytesExpectedToRead) {
        //-- This block gets invoked periodically as the download progress

        dispatch_time_t popTime = dispatch_time(DISPATCH_TIME_NOW, (int64_t)(totalBytesRead * NSEC_PER_SEC));
        dispatch_after(popTime, dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_BACKGROUND, 0), ^(void){

            dispatch_async(dispatch_get_main_queue(), ^{
                // HUD treatment
            });
        });



    }];

    [operation setCompletionBlockWithSuccess:^(AFHTTPRequestOperation *operation, id responseObject) {
        NSLog(@"Completed:");

    } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
        NSLog(@"Error: %@", error);

    }];

    [operation start];

}

当我添加完成框时,我已经没有结果了,屏幕上什么也没有显示,并且注意到AFJSONRequestOperation块没有被执行,我在那里打印了一个日志,它没有显示出来。有什么问题吗?非常感谢。

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2013-08-24 13:29:32

扔掉这段代码:

代码语言:javascript
复制
[operation setCompletionBlockWithSuccess:^(AFHTTPRequestOperation *operation, id responseObject) {
    NSLog(@"Completed:");
} failure:^(AFHTTPRequestOperation *operation, NSError *error) {
    NSLog(@"Error: %@", error);
}];

相反,将您的日志记录放到JSONRequestOperationWithRequest:success:failure:上的块中(在这里,您目前有一个成功块,但没有失败块,并且您应该删除的代码正在删除成功块)。

setDownloadProgressBlock中,只需推到主线程并更新UI即可。不要费心制造拖延,这只会导致明显的随机行为。另外,不要尝试使用下载的结果,这应该只在success块中完成。

票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/18417463

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档