好的,在我的应用程序中,我正在获取包含我需要下载的一些图像的 url 的 json 对象。我使用下面的代码完成所有这些操作,正在下载图像并将其保存到我创建的目录 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]);
}
}];
}