【问题标题】:NSURLRequest downloads, NSFileManager doesn't seeNSURLRequest 下载,NSFileManager 看不到
【发布时间】:2013-10-08 23:59:58
【问题描述】:

好的,在我的应用程序中,我正在获取 json 对象,其中包含我需要下载的一些图像的 url。我使用下面的代码完成所有这些操作,正在下载图像并将其保存到我创建的目录 tmp/Assets/ 但 NSFileManager 在我重新启动应用程序之前看不到它们。我的意思是,文件在那里,我可以使用管理器查看它们,只是无论我做什么文件管理器在我再次启动应用程序之前都看不到它们。 nsfilemanager 是否有某种刷新功能?

- (void)updateResizeableAssets{
NSString* tempDirectory = NSTemporaryDirectory();
tempDirectory = [tempDirectory stringByAppendingString:@"Assets/"];
if(![[NSFileManager defaultManager] fileExistsAtPath:tempDirectory isDirectory:nil])
{
    [[NSFileManager defaultManager] createDirectoryAtPath:tempDirectory withIntermediateDirectories:YES attributes:nil error:nil];
}

NSURLRequest *requestResizeable = [NSURLRequest requestWithURL:[NSURL URLWithString:@"http://someurl.com/"]];
[NSURLConnection sendAsynchronousRequest:requestResizeable queue:[NSOperationQueue mainQueue] completionHandler:^(NSURLResponse *response, NSData* data, NSError *connectionError){
    NSError *jsonParsingError = nil;
    NSDictionary *arrayOfImages;
    NSArray *object = [NSJSONSerialization JSONObjectWithData:data options:0 error:&jsonParsingError];
    for(int i=0; i<[object count];i++)
    {
        arrayOfImages= [object objectAtIndex:i];
        NSString *imageStringUrl = @"http://someotherurl.com/";
        imageStringUrl = [imageStringUrl stringByAppendingString:(NSString*)[arrayOfImages objectForKey:@"name"]];
        NSURLRequest *imageUrl = [NSURLRequest requestWithURL:[NSURL URLWithString:imageStringUrl]];

        if(![[NSFileManager defaultManager] fileExistsAtPath:[tempDirectory stringByAppendingString:[arrayOfImages objectForKey:@"name"]]])
        {
            [NSURLConnection sendAsynchronousRequest:imageUrl queue:[NSOperationQueue mainQueue] completionHandler:^(NSURLResponse *response, NSData* data, NSError *connectionError){
                [[NSFileManager defaultManager] createFileAtPath:[tempDirectory stringByAppendingString:[arrayOfImages objectForKey:@"name"]] contents:data attributes:Nil];

            }];
        }
        //NSLog(@"Image: %@", [arrayOfImages objectForKey:@"name"]);
    }

    NSString* tempDirectory = NSTemporaryDirectory();
    tempDirectory = [tempDirectory stringByAppendingString:@"Assets/"];
    NSArray *files = [[NSFileManager defaultManager] contentsOfDirectoryAtPath:tempDirectory error:nil];
    for(int i=0; i<[files count]; i++)
    {
        //HERE I GOT NOTHING UNTIL I RESTART THE APP (Run App twice)
        NSLog(@"%@", [files objectAtIndex:i]);
    }
}];

}

【问题讨论】:

    标签: ios xcode nsurlconnection nsfilemanager nsurlrequest


    【解决方案1】:

    这是我的方法: 块中的代码是异步调用的,所以当你进入这部分时:

    [NSURLConnection sendAsynchronousRequest:imageUrl queue:[NSOperationQueue mainQueue]   completionHandler:^(NSURLResponse *response, NSData* data, NSError *connectionError){
                [[NSFileManager defaultManager] createFileAtPath:[tempDirectory stringByAppendingString:[arrayOfImages objectForKey:@"name"]] contents:data attributes:Nil];
    
            }];
    

    执行继续,这意味着主块在第二块完成工作之前终止。

    所以把剩下的代码放在你的 NSURLConnection 块中:

    [NSURLConnection sendAsynchronousRequest:imageUrl queue:[NSOperationQueue mainQueue]   completionHandler:^(NSURLResponse *response, NSData* data, NSError *connectionError){
                [[NSFileManager defaultManager] createFileAtPath:[tempDirectory stringByAppendingString:[arrayOfImages objectForKey:@"name"]] contents:data attributes:Nil];
    
       NSString* tempDirectory = NSTemporaryDirectory();
       tempDirectory = [tempDirectory stringByAppendingString:@"Assets/"];
       NSArray *files = [[NSFileManager defaultManager]      contentsOfDirectoryAtPath:tempDirectory error:nil];
       for(int i=0; i<[files count]; i++)
       {
           //HERE I GOT NOTHING UNTIL I RESTART THE APP (Run App twice)
           NSLog(@"%@", [files objectAtIndex:i]);
       }
    }];
    

    我希望这会有所帮助:)。

    已编辑
    在使用块时,很难理解发生了什么,问题是您在块内创建文件并且该块处于 for 循环中,所以可以说当您发出第一个请求时 i=0,然后您的request 可能需要一段时间才能给您响应,这意味着您的循环将继续(不会等待您的第一个请求),因此当 i=1 时,您的第一个请求可能尚未完成,因此您的第一个文件尚未创建。 如果您要使用块并且需要等待响应,则需要从块内部调用所有请求,比如说递归方法。 我会建议您使用委托方法,它可以做更多的工作,但您可以更好地控制正在发生的事情。 http://codewithchris.com/tutorial-how-to-use-ios-nsurlconnection-by-example/

    【讨论】:

    • 好的,目前可以使用。我只想获取这些文件一次,但是当我在搜索文件之前输入 if ((i+1) == [object count]) 时,我什么也没得到。登录后,我发现如果我输入 if(i == 0) 它可以完美运行吗? O.o 不太确定如何将其与异步请求相关联,也许我为零时的请求最后完成,不太确定:|
    • 我编辑我的答案也许可以帮助您清除问题。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-01-13
    • 2011-12-31
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多