Reputation: 50717
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.
-(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];
}
Upvotes: 1
Views: 370
Reputation: 64002
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];
Upvotes: 3