简体   繁体   中英

async download - right mode

I need to download some image from my website. I create an XML file with the url of the image from the app, and i read this file and store all url inside an array.

Now, I need to download all the image, I want to di it asyncronusly, I want to know the best way for do it, now my code is:

    for (int i=0; i<self.arrayAggiornamenti.count; i++) {

        Aggiornamento *aggiornamento = [self.arrayAggiornamenti objectAtIndex:i];
        NSMutableArray *arrayPath = aggiornamento.arrayPaths;

        for (int j=0; j<arrayPath.count; j++) {

            NSString *urlString = [NSString stringWithFormat:@"http://www.xxx.com/%@",[arrayPath objectAtIndex:j]];

            NSString *urlStringEncoded = [urlString stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];

            NSURL *urlImage = [NSURL URLWithString:urlStringEncoded];

            NSURLRequest *requestImgage = [NSURLRequest requestWithURL:urlImage
                                                        cachePolicy:NSURLRequestReloadIgnoringLocalCacheData
                                                    timeoutInterval:60];

            self.imgData = [[NSMutableData alloc] init];

            self.imgConnection = [[NSURLConnection alloc] initWithRequest:requestImgage delegate:self startImmediately:YES];
        }
}

- (void) connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response{

    // Start parsing XML
    if (connection == self.xmlConnection) {
    }
    // Start download Totale
    else if (connection == self.toatalConnection) {
    }
    // Start connessione immagini
    else {        
        [self.imgData writeToFile:pathFile atomically:YES];
    }
}

- (void) connection:(NSURLConnection *)connection didReceiveData:(NSData *)data {

    if (connection == self.xmlConnection) {
    }
    else if (connection == self.toatalConnection) {
    }
    else {
        [self.imgData appendData:data];
    }

}
- (void) connection:(NSURLConnection *)connection didFailWithError:(NSError *)error {}

- (void) connectionDidFinishLoading:(NSURLConnection *)connection {

    if (connection == self.xmlConnection) {
    }
    else if (connection == self.toatalConnection) {
    }
    else if (connection == self.imgConnection){
    }
}

With this code the image are downloaded but the problem is that if I add an if else (connection==self.imgConnection) on didReciveResponse the app download only the first image, why? Is correct this way for download multiple image?

May be because you are allocating self.imgData inside the loop. Please refer this link for downloading images inside an array: What is asynchronous image downloading and how can I download too many images?

Looks like self.imgConnection gets overwitten again and again in for loop. So by the time you receive response for any connection from internet, loop will overwrite it many times and it will finally hold the last allocated object. That way, your else if(connection==self.imgConnection) will be true only once.

Best practice to do here is to make one class for each type of connection. By that you will be able to download one image using one instance of connection class and hold it within that instance and than pass it to you caller class.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM