내 웹 서비스에서 파일 (.json)을 다운로드하고 다운로드가 완료되었습니다. 로컬 SQLlite 데이터베이스에 내용을 압축 해제해야합니다.파일 다운로드가 완료되고 사용할 준비가되었는지 알려주는 방법은 무엇입니까?
NSURLConnection이 다른 스레드에서 파일을 비동기 적으로 다운로드하여 주 스레드를 열어 둘 수 있음을 나타냅니다. 문제는 파일 다운로드가 시작될 때 주 스레드가 분명히 자신이하는 일을 수행한다는 것입니다.이 경우 파일의 내용을 데이터베이스에로드하려고합니다. 이 때 파일이 아직 존재하지 않거나 존재하지만 아직 완료되지 않았습니다.
파일을 실제로 완전히 다운로드하여 사용할 준비가 된 후에 만 파일 내용을 데이터베이스에 입력하는 방법을 어떻게 실행합니까? 있는 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