好的,在我的應用程序中,我正在獲取包含需要下載的某些圖像的url的json對象。我使用下面的代碼完成所有這些工作,圖像被下載並保存到我創建的tmp/Assets /目錄中,但NSFileManager在我重新啓動應用程序之前不會看到它們。我的意思是,文件在那裏,我可以使用組織者看到它們,無論我做什麼,filemanager都不會看到它們,直到我再次啓動應用程序。是否有某種nsfilemanager的刷新功能?NSURLRequest下載,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]);
}
}];
}
好吧,這是現在的工作。我只想得到這些文件一次,但是當我搜索文件之前,如果((i + 1)== [[object count]),我沒有得到任何東西。記錄後,我看到,如果我把如果(我= 0)它完美的作品? O.o不太確定如何將此與異步請求相關聯,也許當我是零時請求正在完成最後,不太確定: – jovanjovanovic
我編輯我的答案也許可以幫助你清除事情。 – Moy
謝謝,現在事情更清晰了! – jovanjovanovic