私は自分のWebサービスからファイル(.json)をダウンロードしていますし、それは私が内容はローカルSQLliteデータベースへの「アンパック」する必要がありますダウンロードが終了するWHE。ファイルのダウンロードが完了し、すぐに使用できるようになる方法を教えてください。
私はNSURLConnectionがオープンメインスレッドを維持するために、別のスレッドで非同期にファイルをダウンロードすることをunderstannd。問題は、ファイルのダウンロードが開始されたときに、メインスレッドが、実行中の処理(この場合はファイルの内容をデータベースにロードしようとしている)を実行していることです。これが起こるまでに、ファイルはまだ存在しないか、または存在していますが、まだ完了していません。
はどうすればファイルのみAFTERデータベースに入力ファイルの内容は、実際に完全にダウンロードして使用する準備ができているというメソッドを実行するのですか?
-(void)downloadTheFileFrom:(NSString*)url withToken:(NSString*)token
{
NSString *conURL = [NSString stringWithFormat:@"%@/%@", apiURL, url];
NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:[NSURL URLWithString:conURL]];
[request setHTTPMethod:@"GET"];
[request addValue:@"application/json" forHTTPHeaderField:(@"content-type")];
[request addValue:token forHTTPHeaderField:(@"X-TOKEN")];
NSURLConnection *conn = [[NSURLConnection alloc] initWithRequest:request delegate:self startImmediately:NO];
[conn scheduleInRunLoop:[NSRunLoop mainRunLoop] forMode:NSDefaultRunLoopMode];
[conn start];
[self saveJsonToDatabase];
}
データベースにファイルを保存する方法:NSURLConnectionため
-(void)saveJsonToDatabase
{
NSString *jsonFileToSave = @"drug.json";
NSString *documentsPath = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) firstObject];
NSError *jsonError = nil;
NSString *jsonFile = [documentsPath stringByAppendingPathComponent:[NSString stringWithFormat:@"/content/formulary/files/%@", jsonFileToSave]];
NSData *jsonData = [NSData dataWithContentsOfFile:jsonFile];
NSArray *jsonArray = [NSJSONSerialization JSONObjectWithData:jsonData options:0 error:&jsonError];
NSString *formularyTable = [NSString stringWithFormat:@"formulary_%@", [jsonFileToSave stringByDeletingPathExtension]];
FormularyDBManager *formularyDBManagerInstance = [FormularyDBManager new];
[formularyDBManagerInstance insertInto:formularyTable arrayOfDicts:jsonArray];
}
デリゲートメソッド:あなたはNSURLSessionDownloadTaskを使用している場合、それは完了ですので、
-(void)connection:(NSURLConnection*)connection didReceiveResponse:(NSURLResponse *)response {
[UIApplication sharedApplication].networkActivityIndicatorVisible = YES;
NSString *apiUrl = [NSString stringWithFormat:@"%@",[response URL]];
NSArray *pathComponents = [apiUrl pathComponents];
NSString *areaFolder = [pathComponents objectAtIndex:3];
NSString *fileName = [response suggestedFilename];
NSString *fileType = [fileName pathExtension];
NSString *docsfolderPath = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) objectAtIndex:0];
NSString *folderPath = [docsfolderPath stringByAppendingString:[NSString stringWithFormat:@"/content/%@/%@", areaFolder, contentType]];
BOOL isDir;
NSFileManager *fileManager= [NSFileManager defaultManager];
if(![fileManager fileExistsAtPath:folderPath isDirectory:&isDir])
if(![fileManager createDirectoryAtPath:folderPath withIntermediateDirectories:YES attributes:nil error:NULL])
NSLog(@"FileDownloader - connection - Error: Create folder failed %@", folderPath);
NSString *filePath = [folderPath stringByAppendingPathComponent:fileName];
[[NSFileManager defaultManager] createFileAtPath:filePath contents:nil attributes:nil];
connection.file = [NSFileHandle fileHandleForUpdatingAtPath:filePath];
_currentFile = fileName;
}
-(void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data {
[connection.file writeData:data];
}
-(NSCachedURLResponse *)connection:(FileURLConnection *)connection willCacheResponse:(NSCachedURLResponse*)cachedResponse {
return nil;
}
-(void)connectionDidFinishLoading:(NSURLConnection *)connection
{
[connection.file closeFile];
}
-(void)connection:(NSURLConnection *)connection didFailWithError:(NSError *)error {
NSLog(@"GetData - connection - error - %@", error);
}
を使用するためのリンゴのドキュメントを読むことができます。ファイルを保存する場所を定義できますか?私はこのクラスを使ってたくさんのファイルを保存しています。私は「interecpt」してその内容をデータベースに入れなければなりません。 – Ryan
ファイルを渡された場所に移動できます。 '[[NSFileManager defaultManager] copyItemAtPath:tempLocation toPath:passedFilePath error:nil];'このトリックを行うべきです。 – Amset
コードを書き直し、提案された変更を追加した後、これは意図したとおりに動作します。また、このメソッドはデリゲートメソッドの負荷を心配するだけのことよりもはるかに簡単です。ありがとう! – Ryan