在这里,我尝试在 GCD 中调用我的 ASIHTTP 请求。但是完成块和失败块没有执行

发布于 2024-12-09 09:49:44 字数 946 浏览 1 评论 0原文

在这里,我尝试在 GCD 中调用我的 ASIHTTPRequest 。有时,完成块和失败块不会执行。我想要做的是,在这个请求完成后,我必须在另一个 ASIHTTPRequest 中使用返回的数据。那么如何改进这段代码:

 ASIHTTPRequest *request = [ASIHTTPRequest requestWithURL:[NSURL URLWithString:url]];

    [request setCompletionBlock:^{
        dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_LOW, 0), ^{
            NSData *_responseData = [request responseData];
            NSString *response = [[NSString alloc] initWithData:_responseData encoding:NSASCIIStringEncoding] ;
            self.albumDic = [response JSONValue];
            [response release];

            dispatch_async(dispatch_get_main_queue(), ^{
                [self GetDictionary:self.albumDic];
            });
        });
    }];
    [request setFailedBlock:^{
        NSError *error = [request error];
        NSLog(@"Error : %@", error.localizedDescription);
    }];
    [request startSynchronous];

Here i am trying to call my ASIHTTPRequest in a GCD. Sometimes the comletion blocks and failed blocks are not executing. What i want to do is, after this request finished, i have to use the returned data in a another ASIHTTPRequest. So how to improve this code:

 ASIHTTPRequest *request = [ASIHTTPRequest requestWithURL:[NSURL URLWithString:url]];

    [request setCompletionBlock:^{
        dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_LOW, 0), ^{
            NSData *_responseData = [request responseData];
            NSString *response = [[NSString alloc] initWithData:_responseData encoding:NSASCIIStringEncoding] ;
            self.albumDic = [response JSONValue];
            [response release];

            dispatch_async(dispatch_get_main_queue(), ^{
                [self GetDictionary:self.albumDic];
            });
        });
    }];
    [request setFailedBlock:^{
        NSError *error = [request error];
        NSLog(@"Error : %@", error.localizedDescription);
    }];
    [request startSynchronous];

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

っ〆星空下的拥抱 2024-12-16 09:49:44

别走那条路。您正在线程上进行线程(GCD 使用线程,异步使用时 ASIHTTPRequest 也使用线程)。

请改用 ASINetworkQueue - 在此处阅读有关它的内容,

这是您可以使用它的简单方法:

- (void)addRequestsToNetworkQueue:(NSArray *)requests {    

// Stop anything already in the queue before removing it
[[self networkQueue] cancelAllOperations];

// Creating a new queue each time we use it means we don't have to worry about clearing delegates or resetting progress tracking
[self setNetworkQueue:[ASINetworkQueue queue]];
[[self networkQueue] setDelegate:self];
[[self networkQueue] setRequestDidFinishSelector:@selector(requestFinished:)];
[[self networkQueue] setRequestDidFailSelector:@selector(requestFailed:)];
[[self networkQueue] setQueueDidFinishSelector:@selector(queueFinished:)];

//Add all requests to queue
for (ASIHTTPRequest *req in requests) {

    [[self networkQueue] addOperation:req];

}

//Start queue 
[[self networkQueue] go];
}

ASINetworkQueue 提供了许多委托方法(大多数也是可自定义的),因此您可以在请求完成时更新 GUI 等。它是异步的,因此不需要 GCD。

Don't go that way. You're doing threading on threading (GCD uses threading and so does ASIHTTPRequest when used asynchronously).

Use ASINetworkQueue instead - read about it here

Here is a simple way you could use it:

- (void)addRequestsToNetworkQueue:(NSArray *)requests {    

// Stop anything already in the queue before removing it
[[self networkQueue] cancelAllOperations];

// Creating a new queue each time we use it means we don't have to worry about clearing delegates or resetting progress tracking
[self setNetworkQueue:[ASINetworkQueue queue]];
[[self networkQueue] setDelegate:self];
[[self networkQueue] setRequestDidFinishSelector:@selector(requestFinished:)];
[[self networkQueue] setRequestDidFailSelector:@selector(requestFailed:)];
[[self networkQueue] setQueueDidFinishSelector:@selector(queueFinished:)];

//Add all requests to queue
for (ASIHTTPRequest *req in requests) {

    [[self networkQueue] addOperation:req];

}

//Start queue 
[[self networkQueue] go];
}

ASINetworkQueue provides many delegate methods (most are also customizable), so you can update the GUI when a request is finished and so forth. It is asynchronous, so GCD is unnecessary.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文