1

Attempting to download multiple files from an XML feed. There are several Categories and each category has an unknown amount of images. I create a subdirectory for each category, no problem. When I try to download each file to the respective category, every file is written with the same data for every image there is in the feed.

I guess my question is how can I download 1 file at a time to the corresponding directory without overwriting all of the images with the same data?

-(void)parsingComplete:(XMLDataSource*)theParser 
{
    /*  iterate through the Categories and create the 
        sub-directory if it does not exist  
     */

    for (int i = 0; i < [categories count]; i++) 
    {
        NSString *cat      = [NSString stringWithFormat:@"%@/%@",BASE_DIR,[[categories objectAtIndex:i] objectForKey:@"name"]];
        NSString *catName  = [[categories objectAtIndex:i] objectForKey:@"name"];
        NSArray  *catArray = [[categories objectAtIndex:i] objectForKey:@"images"];

        /*  create the sub-direcotry naming it the #category# key  */
        if (![FILEMANAGER fileExistsAtPath:cat]) {
            [FILEMANAGER createDirectoryAtPath:cat withIntermediateDirectories:NO attributes:nil error:nil];
        }

        //NSLog(@"\n\nCategory: %@",cat);
        for (int x = 0; x < [catArray count]; x++) 
        {
            /*  download each file to the corresponding category sub-directory  */

            fileOut = [NSString stringWithFormat:@"%@/%@_0%i.jpg",cat,catName,x];

            NSURLRequest * imageRequest = 
            [NSURLRequest requestWithURL:[NSURL URLWithString:[[catArray objectAtIndex:x] objectForKey:@"imageUrl"]]
                             cachePolicy:NSURLRequestReloadIgnoringLocalCacheData timeoutInterval:30.0];
            [[NSURLConnection alloc] initWithRequest:imageRequest delegate:self];
        }
    }
}

- (void)connectionDidFinishLoading:(NSURLConnection *)connection  
{  
    [receivedData writeToFile:fileOut atomically:YES];
} 
WrightsCS
  • 50,551
  • 22
  • 134
  • 186

1 Answers1

3

Yes, this is what I mentioned in my comment. Each time connectionDidFinishLoading: is called, you've got the result of just one connection. If you loop through all the file names, you will write that same chunk of data out to all those names, repeatedly. Each time through the for loop in parsingComplete: you create a new connection, get a new data object, and then write that same object out multiple times. After the end of the parsing... loop, you're left with a list of files all with the data from the last connection.

I'm pretty tired and I'm not sure: am I being clear?


Addressing your comment:

You'll either have to make the correct file name for the current connection available to the delegate methods, probably by putting it in an ivar, or go the synchronous route. Putting in it in some ivar like currFileName so that all the methods in this class can access it is probably the least painless way to get the job done.

/* In parsingCompleted: */
for (int x = 0; x < [catArray count]; x++) 
{
    /*  download each file to the corresponding category sub-directory  */
    // fileOut is an instance variable
    fileOut = [NSString stringWithFormat:@"%@/%@_0%i.jpg",cat,catName,x];
    imageRequest = [NSURLRequest etc...

- (void)connectionDidFinishLoading:(NSURLConnection *)connection {
    // No loop; just use that file name that you set up earlier; 
    // it correctly corresponds to the current NSURLConnection
    [receivedData writeToFile:fileOut atomically:YES];
Community
  • 1
  • 1
jscs
  • 63,694
  • 13
  • 151
  • 195
  • Sort of makes sense, how can I fix that? – WrightsCS May 17 '11 at 07:55
  • Updated. By the way, thanks for making a new question to explain the change you made. _Way_ easier to read than if you had tried to cram it into a comment. My brain definitely appreciates it. – jscs May 17 '11 at 08:00
  • 2011-05-17 13:02:25.458 APP[1086:903] fileOut: {w=1280 h=1024 prov=0x14e40320 props=0x14e12320} 2011-05-17 13:02:25.460 APP[1086:903] -[__NSCFType getFileSystemRepresentation:maxLength:]: unrecognized selector sent to instance 0x4bbda0 2011-05-17 13:02:25.460 APP[1086:903] -[__NSCFType getFileSystemRepresentation:maxLength:]: unrecognized selector sent to instance 0x4bbda0 – WrightsCS May 17 '11 at 19:03
  • Looks like you have somehow assigned a `CGImagePlus` object to `fileOut` instead of an `NSString`. Did you change something about that assignment? – jscs May 17 '11 at 19:07
  • no, it's still the same `fileOut = [NSString stringWithFormat:@"%@/%@_0%i.jpg",cat,catName,x];` – WrightsCS May 17 '11 at 19:08
  • Well, that's weird. One thing is that there's a memory management error -- you should retain the result of that call to `stringWithFormat:` when you put it into the ivar, and then you will need to release it the next time through the loop. However, there shouldn't be a new, valid object in that slot. You should just be dying with an EXC_BAD_ACCESS... Do the retain/release, though, and see what happens. – jscs May 17 '11 at 19:12
  • What I ended up having to do was create my own class and place all of the NSURLConnection in that new class. Then call that new class from my **for** loop. – WrightsCS May 18 '11 at 00:18