跟踪 ASIHTTPRequest 的多个实例的最佳方法是什么?
我有一些代码发送多个 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您正在围绕这行代码为每个请求创建一个新队列:
因此,这里的循环不会在请求上循环,因为它在共享队列上循环,而不是您创建的新队列:
请求只会得到如果您使用
[request startAynchronous]
而没有显式设置不同的队列,则添加到sharedQueue。You're creating a new queue for each request around this line of code:
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:
Requests would only get added to the sharedQueue if you use
[request startAynchronous]
without explicitly setting a different queue.我可能错过了一些东西,但我认为等到
dealloc
已经太晚了,你想取消对viewWillDisappear
或viewDidUnload
的操作I may be missing something, but I think waiting until
dealloc
is too late, you want to cancel your operations onviewWillDisappear
orviewDidUnload