跟踪 ASIHTTPRequest 的多个实例的最佳方法是什么?

发布于 2024-12-04 03:56:33 字数 1537 浏览 1 评论 0原文

我有一些代码发送多个 ASIHTTPRequest 以在视图控制器中上传和下载数据。当视图控制器被释放时,它应该通过将委托设置为 nil 来清理所有未完成的请求。

- (void)viewDidLoad
{
        // send multiple requests
        [self sendRequest:someURL];
        [self sendRequest:someURL];
        [self sendRequest:someURL];
        [self sendRequest:someURL];
}

- (void)sendRequest:(NSString*)url
{
        ASIHTTPRequest *request = [ASIHTTPRequest requestWithURL:url];
        [request setDelegate:self]; 

        ASINetworkQueue *requestQueue = [ASINetworkQueue queue];
        [requestQueue setMaxConcurrentOperationCount:2];
        [requestQueue setDelegate:self];
        [requestQueue addOperation:request];
        [requestQueue go];
}

- (void)dealloc
{
    NSLog(@"cancel all operations");
    for (ASIHTTPRequest *req in ASIHTTPRequest.sharedQueue.operations)
    {
        [req cancel];
        [req setDelegate:nil];
    }

    [super dealloc];
}

但是,如果我在所有操作完成之前弹出此视图控制器,我会在 ASIHTTPRequest.m 中收到一条“消息发送到已释放的实例”,抱怨委托在下面的代码中消失了。

/* ALWAYS CALLED ON MAIN THREAD! */
- (void)reportFailure
{
    ***crash here --> if (delegate && [delegate respondsToSelector:didFailSelector]) {
        [delegate performSelector:didFailSelector withObject:self];
    }
    if (queue && [queue respondsToSelector:@selector(requestFailed:)]) {
        [queue performSelector:@selector(requestFailed:) withObject:self];
    }
    #if NS_BLOCKS_AVAILABLE
    if(failureBlock){
        failureBlock();
    }
    #endif
}

我该如何解决这个问题?

I have some code that sends multiple ASIHTTPRequests to upload and download data in a view controller. When the view controller gets dealloc'd it should clean up all unfinished requests by setting the delegate to nil.

- (void)viewDidLoad
{
        // send multiple requests
        [self sendRequest:someURL];
        [self sendRequest:someURL];
        [self sendRequest:someURL];
        [self sendRequest:someURL];
}

- (void)sendRequest:(NSString*)url
{
        ASIHTTPRequest *request = [ASIHTTPRequest requestWithURL:url];
        [request setDelegate:self]; 

        ASINetworkQueue *requestQueue = [ASINetworkQueue queue];
        [requestQueue setMaxConcurrentOperationCount:2];
        [requestQueue setDelegate:self];
        [requestQueue addOperation:request];
        [requestQueue go];
}

- (void)dealloc
{
    NSLog(@"cancel all operations");
    for (ASIHTTPRequest *req in ASIHTTPRequest.sharedQueue.operations)
    {
        [req cancel];
        [req setDelegate:nil];
    }

    [super dealloc];
}

However, if I pop this view controller before all operations have finished, I get a "message sent to deallocated instance" in ASIHTTPRequest.m complaining that the delegate went away in the code below.

/* ALWAYS CALLED ON MAIN THREAD! */
- (void)reportFailure
{
    ***crash here --> if (delegate && [delegate respondsToSelector:didFailSelector]) {
        [delegate performSelector:didFailSelector withObject:self];
    }
    if (queue && [queue respondsToSelector:@selector(requestFailed:)]) {
        [queue performSelector:@selector(requestFailed:) withObject:self];
    }
    #if NS_BLOCKS_AVAILABLE
    if(failureBlock){
        failureBlock();
    }
    #endif
}

How can I work around this?

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

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

发布评论

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

评论(2

も让我眼熟你 2024-12-11 03:56:33

您正在围绕这行代码为每个请求创建一个新队列:

ASINetworkQueue *requestQueue = [ASINetworkQueue queue];

因此,这里的循环不会在请求上循环,因为它在共享队列上循环,而不是您创建的新队列:

for (ASIHTTPRequest *req in ASIHTTPRequest.sharedQueue.operations)

请求只会得到如果您使用[request startAynchronous]而没有显式设置不同的队列,则添加到sharedQueue。

You're creating a new queue for each request around this line of code:

ASINetworkQueue *requestQueue = [ASINetworkQueue queue];

So the loop here won't loop over the requests as it's looping over the sharedQueue, not the new one(s) you've created:

for (ASIHTTPRequest *req in ASIHTTPRequest.sharedQueue.operations)

Requests would only get added to the sharedQueue if you use [request startAynchronous] without explicitly setting a different queue.

微凉 2024-12-11 03:56:33

我可能错过了一些东西,但我认为等到 dealloc 已经太晚了,你想取消对 viewWillDisappearviewDidUnload 的操作

I may be missing something, but I think waiting until dealloc is too late, you want to cancel your operations on viewWillDisappear or viewDidUnload

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