2013-10-08 24 views
0

好的,在我的應用程序中,我正在獲取包含需要下載的某些圖像的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]); 
    } 
}]; 

}

回答

2

這是我的方法:

[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,那麼你的請求可能需要一段時間才能給你響應,這意味着你的循環將繼續(不會等待你的第一個請求),所以當i = 1時,你的第一個請求還沒有完成,所以你的第一個文件還沒有創建。 如果您打算使用塊,並且您需要等待響應,則需要調用塊內部的所有請求,比如說遞歸方法。 我會推薦你​​使用委託方法,它可以做更多的工作,但你有更多的控制發生了什麼。 http://codewithchris.com/tutorial-how-to-use-ios-nsurlconnection-by-example/

+0

好吧,這是現在的工作。我只想得到這些文件一次,但是當我搜索文件之前,如果((i + 1)== [[object count]),我沒有得到任何東西。記錄後,我看到,如果我把如果(我= 0)它完美的作品? O.o不太確定如何將此與異步請求相關聯,也許當我是零時請求正在完成最後,不太確定: – jovanjovanovic

+0

我編輯我的答案也許可以幫助你清除事情。 – Moy

+0

謝謝,現在事情更清晰了! – jovanjovanovic