AFNetworking を使い始めたばかりですが、これまでのところ、Web からデータを取得するだけでうまくいきました。
しかし今、デバイスにダウンロードする必要があるファイルのリストがあります
for(NSDictionary *issueDic in data)
{
Issue *issue = [[Issue alloc] init];
...
[self getOrDownloadCover:issue];
...
}
getOrDownloadCover:issue は、ファイルが既にローカルに存在するかどうかをチェックし、存在する場合はそのパスを保存し、存在しない場合は指定された URL からファイルをダウンロードします
- (void)getOrDownloadCover:(Issue *)issue
{
NSLog(@"issue.thumb: %@", issue.thumb);
NSString *coverName = [issue.thumb lastPathComponent];
NSLog(@"coverName: %@", coverName);
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
__block NSString *filePath = [documentsDirectory stringByAppendingPathComponent:coverName];
if([[NSFileManager defaultManager] fileExistsAtPath:filePath])
{
// Cover already exists
issue.thumb_location = filePath;
NSLog(@"issue.thumb_location: %@", filePath);
}
else
{
NSLog(@"load thumb");
// Download the cover
NSURL *url = [NSURL URLWithString:issue.thumb];
AFHTTPClient *httpClient = [[[AFHTTPClient alloc] initWithBaseURL:url] autorelease];
NSMutableURLRequest *request = [httpClient requestWithMethod:@"GET" path:issue.thumb parameters:nil];
AFHTTPRequestOperation *operation = [[[AFHTTPRequestOperation alloc] initWithRequest:request] autorelease];
operation.outputStream = [NSOutputStream outputStreamToFileAtPath:filePath append:NO];
[operation setCompletionBlockWithSuccess:^(AFHTTPRequestOperation *operation, id responseObject) {
issue.thumb_location = filePath;
NSLog(@"issue.thumb_location: %@", filePath);
} failure:^(AFHTTPRequestOperation *operation, NSError *error) {
NSLog(@"FAILED");
}];
[operation start];
}
}
getOrDownloadCover:issue は連続して 20 回呼び出すことができるので、すべてのリクエストをキューに入れる必要があります。キューが完了しても、パスを保存できるはずです (または通知を送信するだけです。私はすでにパスが何であるかを知っています)
これに関する提案はありますか?