代码改变世界

ASINetworkQueue使用cancelAllOperations方法来取消下载时使用的注意点

2013-01-18 15:11  三戒1993  阅读(131)  评论(0编辑  收藏  举报

在   ASIHTTPRequest V1.7前提下

正确使用:
   imgDataDownLoadQueue = [[ASINetworkQueue alloc] init];
   [imgDataDownLoadQueue setShouldCancelAllRequestsOnFailure:NO];
   [imgDataDownLoadQueue setDelegate:self];
   [imgDataDownLoadQueue setRequestDidFailSelector:@selector(imgDataRequestFail:)];
   [imgDataDownLoadQueue setRequestDidFinishSelector:@selector(imgDataRequestFinish:)];
   [imgDataDownLoadQueue setQueueDidFinishSelector:@selector(imgDataQueueDidFinish:)];

    for(int i=0; i<[imgDatasToLoadArray count]; i++)
    {
     ASIHTTPRequest* imgRq = [ASIHTTPRequest requestWithURL:[NSURL URLWithString:[[imgDatasToLoadArray objectAtIndex:i] objectForKey:@"imgurl"]]];
    
     NSString* value[2];
     NSString* key[2];
     value[0] = [[imgDatasToLoadArray objectAtIndex:i] objectForKey:@"imgtime"];
     value[1] = [[imgDatasToLoadArray objectAtIndex:i] objectForKey:@"index"];
     key[0] = @"imgtime";
     key[1] = @"index";
    
     imgRq.userInfo = [NSDictionary dictionaryWithObjects:value forKeys:key count:2];
    
     [imgDataDownLoadQueue addOperation:imgRq];
    }
    [imgDataDownLoadQueue go];

=======
这样在dealloc中取消下载
[imgDataDownLoadQueue setDelegate:nil];
[imgDataDownLoadQueue cancelAllOperations];
[imgDataDownLoadQueue release];
imgDataDownLoadQueue = nil;
不会导致crash.

#######################################################3

如果按这种方法使用:

   imgDataDownLoadQueue = [[ASINetworkQueue alloc] init];
   [imgDataDownLoadQueue setShouldCancelAllRequestsOnFailure:NO];
   [imgDataDownLoadQueue setDelegate:self];

    for(int i=0; i<[imgDatasToLoadArray count]; i++)
    {
     ASIHTTPRequest* imgRq = [ASIHTTPRequest requestWithURL:[NSURL URLWithString:[[imgDatasToLoadArray objectAtIndex:i] objectForKey:@"imgurl"]]];
    
     NSString* value[2];
     NSString* key[2];
     value[0] = [[imgDatasToLoadArray objectAtIndex:i] objectForKey:@"imgtime"];
     value[1] = [[imgDatasToLoadArray objectAtIndex:i] objectForKey:@"index"];
     key[0] = @"imgtime";
     key[1] = @"index";
    
     imgRq.userInfo = [NSDictionary dictionaryWithObjects:value forKeys:key count:2];
     [imgRq setRequestDidFailSelector:@selector(imgDataRequestFail:)];
   [imgRq setRequestDidFinishSelector:@selector(imgDataRequestFinish:)];

     [imgDataDownLoadQueue addOperation:imgRq];
    }
    [imgDataDownLoadQueue go];

在dealloc中

[imgDataDownLoadQueue setDelegate:nil];
[imgDataDownLoadQueue cancelAllOperations];
[imgDataDownLoadQueue release];
imgDataDownLoadQueue = nil;

将会导致ASIHttpRequist的Crash.