2017-09-14 37 views
0

我正在从我自己的web服务下载一个文件(.json),当它完成下载时,我需要将内容'解包'到本地SQLlite数据库中。如何判断文件何时完成下载并准备使用?

我强调NSURLConnection在另一个线程上异步下载文件以保持主线程打开。问题是,当文件下载已经开始时,主线程显然会继续做它正在做的事情 - 在这种情况下,试图将文件的内容加载到数据库中。在发生这种情况时,文件或者不存在或者存在但尚未完成。

如何运行将文件内容输入到数据库中的方法仅在文件实际完全下载并准备使用后?

-(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]; 
} 

方法是将文件保存到数据库:

-(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]; 
} 

为NSURLConnection的委托方法:

-(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); 
} 

回答

1

我会坚持使用NSURLSession和NSURLSessionDownloadTask,因为NSURLConnection已被弃用。

NSURLSessionDownloadTask可以在块或代理中回答,让我们坚持块变体。

// Your request 
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")];  

// Create a simple Session 
NSURLSession *session = [NSURLSession sharedSession]; 
// Create the downloadTask 
NSURLSessionDownloadTask *downloadTask = [session downloadTaskWithRequest:request completionHandler:^(NSURL * _Nullable tempLocation, NSURLResponse * _Nullable response, NSError * _Nullable error) { 
    // this block will be called on a async thread when the task is finished. 
    // if no error occurred and we got a path lets store the data. 
    if (!error && tempLocation) { 
     // I pass the location to your function, so it can read the file. 
     [self saveJsonToDatabase:tempLocation]; 
    } 
}]; 
[downloadTask resume]; // don't forget to start the task. 

任务在完成或失败时调用其块。 tempLocation中的文件仅为临时文件,之后将被删除,如果您想保留该文件,则需要将其移至其他路径。

有关详细信息你可以阅读苹果文档使用NSURLSession + Tasks

+0

这看起来可能适合什么我要找的。我可以定义文件的保存位置吗?我使用这个类来保存大量文件,这只是一个我必须'interecpt'并将他们的内容放入数据库的情况。 – Ryan

+0

您可以将文件移到传递的位置。 '[[NSFileManager defaultManager] copyItemAtPath:tempLocation toPath:passedFilePath error:nil];'应该这样做。 – Amset

+0

重写我的代码并添加建议的更改后,现在按预期工作。此外,这种方法似乎比委托方法的负载也要担心得多。谢谢! – Ryan

0

如果使用NSURLSessionDownloadTask它会更容易处理,因为它是一个完成块方法。当它完成下载的代码块将被执行。加上NSURLConnection已被弃用。

相关问题