【问题标题】:Downloading multiple files writes same data to all files下载多个文件会将相同的数据写入所有文件
【发布时间】:2011-05-17 07:49:03
【问题描述】:

尝试从 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];
} 

【问题讨论】:

    标签: xml cocoa macos nsurlconnection nsurlrequest


    【解决方案1】:

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

    我很累,我不确定:我说清楚了吗?


    解决您的评论:

    您要么必须为委托方法提供当前连接的正确文件名,可能通过将其放入 ivar 中,要么采用同步路由。将它放入像 currFileName 这样的 ivar 中,以便此类中的所有方法都可以访问它,这可能是完成工作最轻松的方式。

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

    【讨论】:

    • 有点道理,我该如何解决?
    • 已更新。顺便说一句,感谢您提出一个新问题来解释您所做的更改。 方式比您试图将其塞进评论更容易阅读。我的大脑肯定很感激。
    • 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:]:无法识别的选择器发送到实例0x4bbda0 2011-05-17 13:02:25.460 APP [1086:903] - [__NSCFType getFileSystemRepresentation:maxLength:]:无法识别的选择器发送到实例 0x4bbda0
    • 看起来您以某种方式将CGImagePlus 对象分配给了fileOut,而不是NSString。你对那个作业有什么改变吗?
    • 不,还是一样fileOut = [NSString stringWithFormat:@"%@/%@_0%i.jpg",cat,catName,x];
    猜你喜欢
    • 1970-01-01
    • 2018-08-05
    • 2017-10-08
    • 1970-01-01
    • 2017-04-02
    • 2019-01-11
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多