下载多个文件将相同的数据写入所有文件 [英] Downloading multiple files writes same data to all files

查看:109
本文介绍了下载多个文件将相同的数据写入所有文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

正在尝试从XML资讯提供下载多个档案。有几个类别,每个类别具有未知数量的图像。我为每个类别创建一个子目录,没有问题。当我尝试将每个文件下载到相应的类别时,每个文件都使用相同的数据为Feed中的每个图像写入。

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];
} 


推荐答案

在我的评论中提到 。每次调用 connectionDidFinishLoading:时,您都得到了一个连接的结果。如果你循环遍历所有的文件名,你将重复写入相同的数据块到所有这些名字。每次通过 parsingComplete:中的for循环,您将创建一个新连接,获取一个新的数据对象,然后多次写入同一对象。在解析... 循环结束后,您将看到一个文件列表,其中包含上次连接的数据。

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.

我累了,我不确定我是否清楚?

处理您的评论:

您必须为代理方法提供当前连接的正确文件名,它在一个ivar或者走同步路由。放入一些象$ currFileName 这样的类,以便这个类中的所有方法都可以访问它可能是最简单的方式来完成工作。

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];

这篇关于下载多个文件将相同的数据写入所有文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆