2

I am trying to dowload simultaneously several files from my own server while using this code :

- (void) connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response
{
    // create
    [[NSFileManager defaultManager] createFileAtPath:strFilePath contents:nil attributes:nil];
    _file = [NSFileHandle fileHandleForUpdatingAtPath:strFilePath];// read more about file handle
    if (_file)   {
        [_file seekToEndOfFile];
    }
}

- (void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)receivedata
{
    //write each data received
    if( receivedata != nil){
        if (_file)  {
            [_file seekToEndOfFile];
        }
        [_file writeData:receivedata];
    }
}

- (void)connectionDidFinishLoading:(NSURLConnection*)theConnection {
    //close file after finish getting data;
    [_file closeFile];
}

- (void) connection:(NSURLConnection *)connection didFailWithError:(NSError *)error
{
    //do something when downloading failed
}
- (IBAction)downloadFromServer:(id)sender {

        NSLog(@"File now being downloaded");
    while (i<=3) {

    NSMutableURLRequest *request = [[NSMutableURLRequest alloc] init];
    NSURL *strFileURL =[NSURL URLWithString:[NSString stringWithFormat:@"SomePath/pic%d.png", i]];

    [request setURL:strFileURL]; 
    NSURLConnection *conection = [[NSURLConnection alloc] initWithRequest:request delegate:self startImmediately:NO];
    [conection start];
    strFilePath = [[NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) objectAtIndex:0] stringByAppendingPathComponent:[NSString stringWithFormat:@"pic%d.png", i]];

    }

}

I have 3 pictures , pic1.png , pic2.png and pic3.png. Now if i run this code, the app will only save one corrupted file named pic3.png and automatically crashes. I need to download all three files, any pointers where i am going wrong?

Elias Rahme
  • 2,226
  • 3
  • 28
  • 53

3 Answers3

0

It's quite obvious that this cannot work. You start three asynchronous operations. Each time after starting the asynchronous operation you store a filename in an unknown location, but probably the same location. So what filename will be used when the first asynchronous operation finishes? Hint: Not the one you think it will use.

gnasher729
  • 51,477
  • 5
  • 75
  • 98
  • thank you for your reply... this is my first time dealing with this kind of situation...can you please help me out with a code snippet ? many thanks for your time – Elias Rahme Oct 06 '14 at 09:30
  • I am not asking you to write me my software, i was only asking for a snippet that could clarify your point in order to help me out since its my first time in those situations – Elias Rahme Oct 06 '14 at 09:33
0

Don't use simple while loop to iterate no.of REQUEST operations. Actually Apple itself they have so many built-in libraries. so Here best one i am suggesting you to try "NSOPERATION QUEUE". So, better you can check with Apple developer site Notes, then you get a clear idea: [1]https://developer.apple.com/library/IOs/documentation/Cocoa/Reference/NSOperationQueue_class/index.html

For How can handle different Request using NSOperation - Like AddOperation,or RemoveOperation,WaitUntilItFinishTheThread..

iTag
  • 409
  • 3
  • 19
0

The best way to do so was actually putting all the pictures in one zip file, downloading it and unzipping it on the real device using this code :

    dispatch_queue_t queue = dispatch_get_global_queue(
                                                       DISPATCH_QUEUE_PRIORITY_DEFAULT, 0);
    dispatch_async(queue, ^{
        NSURL *url = [NSURL URLWithString:@"someDirectory/newPics.zip"];
        NSError *error = nil;
        // 2
        NSData *data = [NSData dataWithContentsOfURL:url options:0 error:&error];

        if(!error)
        {
            // 3
            NSArray *paths = NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES);
            NSString *path = [paths objectAtIndex:0];
            NSString *zipPath = [path stringByAppendingPathComponent:@"newPics.zip"];

            [data writeToFile:zipPath options:0 error:&error];

            if(!error)
            {

                ZipArchive *za = [[ZipArchive alloc] init];
                // 1
                if ([za UnzipOpenFile: zipPath]) {
                    // 2
                    BOOL ret = [za UnzipFileTo: path overWrite: YES];
                    if (NO == ret){} [za UnzipCloseFile];

                    // 3
                    NSString *imageFilePath = [path stringByAppendingPathComponent:@"newPics/pic1.png"];
                    //[self removeImage:zipPath];


                    [[NSFileManager defaultManager] removeItemAtPath:zipPath error: &error];

                    dispatch_async(dispatch_get_main_queue(), ^{

                            NSURL *fileURL = [NSURL fileURLWithPath:imageFilePath];
                            [_webV loadRequest:[NSURLRequest requestWithURL:fileURL]];



                    });

                }

            }
            else
            {
                NSLog(@"Error saving file %@",error);
            }
        }
        else
        {
            NSLog(@"Error downloading zip file: %@", error);
        }

    });

It's the best way to do it , fast and reliable.

Elias Rahme
  • 2,226
  • 3
  • 28
  • 53