Downloading multiple files writes same data to all files

旧巷老猫 提交于 2019-12-06 21:25:29
Josh Caswell

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];
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!