Xcode从服务器下载数据库

时间:2015-10-12 09:04:28

标签: ios sqlite fmdb

我正在创建一个使用db来显示数据的应用程序,但是当前db通过FMDB在本地读取它。我想知道的是如何直接从服务器下载数据库并将其显示在表视图中?

代码段: -

-(void)viewWillAppear:(BOOL)animated{
    NSString *path = [[NSBundle mainBundle] pathForResource:@"Climbing" ofType:@"db"];

    FMDatabase *db = [FMDatabase databaseWithPath:path];
    if (![db open]) {
        NSLog(@"Error Opening DB");
        return;
    }
    NSString *queryString;
    CDData *regioni;

    queryString = @"SELECT * , (SELECT count (*) FROM citta t WHERE t.id_regione = a.id_regione) as city FROM regione a ";

    FMResultSet *s = [db executeQuery:queryString];
    while ([s next]) {
        regioni = [[CDData alloc] initWithRecordSet:s];
        [self.data addObject:regioni];

    }
    [db close];
}

1 个答案:

答案 0 :(得分:0)

使用URL会话创建下载任务:

NSString *urlString = @"http://server_DB_URL_here";
NSURL *url = [NSURL URLWithString:urlString];
NSURLRequest *request = [NSURLRequest requestWithURL:url];
[[NSURLSession sharedSession] downloadTaskWithRequest:request];

然后在下载任务完成后使用委托实现将DB文件移动到其中:

- (void)URLSession:(NSURLSession *)session downloadTask:(NSURLSessionDownloadTask *)downloadTask
                              didFinishDownloadingToURL:(NSURL *)location;

然后使用您发布的代码从保存的路径中打开数据库。

   FMDatabase *db = [FMDatabase databaseWithPath:....

旁注:我不会在viewWillAppear中执行以上所有操作: