下载多个文件会将相同的数据写入所有文件
尝试从 XML 源下载多个文件。有多个类别,每个类别都有未知数量的图像。我为每个类别创建一个子目录,没问题。当我尝试将每个文件下载到相应的类别时,每个文件都会针对提要中的每个图像写入相同的数据。
我想我的问题是如何一次下载 1 个文件到相应的目录而不用相同的数据覆盖所有图像?
-(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];
}
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];
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
是的,这就是我在评论中提到的内容。每次调用
connectionDidFinishLoading:
时,您都会得到一个连接的结果。如果循环遍历所有文件名,您将重复将相同的数据块写入所有这些名称。每次通过parsingComplete:
中的 for 循环,您都会创建一个新连接,获取一个新数据对象,然后多次写出同一对象。在parsing...
循环结束后,您将得到一个文件列表,其中包含上次连接的数据。我很累,我不确定:我说清楚了吗?
解决您的评论:
您要么必须为委托方法提供当前连接的正确文件名,可能将其放入 ivar 中,或者采用同步路线。将其放入诸如 currFileName 之类的 ivar 中,以便此类中的所有方法都可以访问它可能是完成工作的最简单的方法。
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 inparsingComplete:
you create a new connection, get a new data object, and then write that same object out multiple times. After the end of theparsing...
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.