2011-08-04 43 views
1

我正在使用ASINetworkQueue排隊我的請求。我添加了12個請求,最後它下載了24次,每次下載請求兩次?這是爲什麼?我該如何預防它?Objective-C - ASINetworkQueue執行兩次添加的請求?

在呼叫進入隊列之前,我檢查已添加到隊列(NSLog(@"%@",[[self queue] operations]);)的請求數量,它顯示12個ASIHTTP請求。

編輯:添加了我的代碼。我沒有打開準確的進度。

代碼:

- (void)setupQueue { 

    DLog(@"setupQueue running"); 

    // Stop anything already in the queue before removing it 
    [[self queue] 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 setQueue:[ASINetworkQueue queue]]; 
    [[self queue] setDelegate:self]; 

    [[self queue] setRequestDidStartSelector:@selector(requestStarted:)]; 
    [[self queue] setRequestDidFinishSelector:@selector(requestFinished:)]; 
    [[self queue] setRequestDidFailSelector:@selector(requestFailed:)]; 
    [[self queue] setQueueDidFinishSelector:@selector(queueFinished:)]; 

} 

- (NSArray *)parseMapsThumbsUrls { 

    DLog(@"parseMapsThumbsUrls running"); 

    NSDictionary *results = [[self officesJSON] JSONValue]; 

    NSArray *offices = [results objectForKey:@"offices"]; 

    NSMutableArray *mapsThumbsUrls = [[[NSMutableArray alloc] init] autorelease]; 

    for (NSDictionary *office in offices) { 

     NSString *mapThumbImageString = [NSString stringWithFormat:@"http://maps.google.com/maps/api/staticmap?zoom=11&markers=color:0xFF7300|%@,%@&size=70x70&sensor=true", [office objectForKey:@"latitude"], [office objectForKey:@"longitude"]]; 

     // Make the string HTML-compatible 
     NSString *url = [mapThumbImageString stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding]; 

     [mapsThumbsUrls addObject:url]; 

    } 

    return mapsThumbsUrls; // returns an array of 6 urls 
} 

- (void)downloadMapsThumbs { 

    DLog(@"downloadMapsThumbs running"); 

    [self setupQueue]; 

    NSArray *mapsThumbsUrls = [self parseMapsThumbsUrls]; 

    for (NSString *mapThumbUrl in mapsThumbsUrls) { 

     NSURL *url = [NSURL URLWithString:mapThumbUrl]; 
     ASIHTTPRequest *mapThumbRequest = [ASIHTTPRequest requestWithURL:url]; 

     [mapThumbRequest setTag:2]; 

     [mapThumbRequest setDelegate:self]; 

     [[self queue] addOperation:mapThumbRequest]; 
    } 
} 

- (void)download { 

    [self downloadMapsThumbs]; 

    DLog(@"%@",[[self queue] operations]); 
    [[self queue] go]; 
} 

- (void)requestFinished:(ASIHTTPRequest *)request { 


    if (request.tag == 2) { // Process thumbs 

     DLog(@"%@",[[request originalURL] description]); 

     // Use when fetching binary data 
     NSData *responseData = [request responseData]; 
     [[self mapThumbs] addObject:responseData]; 

    } 

} 
+2

向我們展示一些代碼。 – VenoMKO

+0

添加了相關代碼。謝謝。 –

回答

2

我注意到我爲隊列和個人請求設置了委託,這就是爲什麼請求會被下載兩次。

1

引述文檔:

當你開始開啓準確進步的隊列中,它會首先執行隊列中的所有GET請求HEAD請求以獲得要下載的數據的總大小。一旦它具有總大小,它可以準確地顯示總進度,並且真正的請求將開始。

這將導致對隊列中每個條目的兩個HTTP請求,因此它可能解釋了您所看到的內容?