2016-10-22 4 views
0

からsqliteデータベースをダウンロードします。このコードを使用してローカルsqliteデータベースをロードしています。URLから

- (id)init 
{ 
    self = [super init]; 
    if (self) { 
     NSString *path = [[NSBundle mainBundle] pathForResource:@"db" ofType:@"sqlite3"]; 
     _db = [[MDDatabase alloc] initWithPath:path]; 
     _HTMLRenderer = [[MDHTMLRenderer alloc] init]; 
    } 
    return self; 
} 

私はオンラインデータベースを入れて、アプリが代わりにデータベースをダウンロードさせたいと思います。

NSData *dbFile = [[NSData alloc] initWithContentsOfURL:[NSURL URLWithString:@"http://www.someurl.com/DatabaseName.sqlite"]]; 

     NSString *resourceDocPath = [[NSString alloc] initWithString:[[[[NSBundle mainBundle] resourcePath] stringByDeletingLastPathComponent] stringByAppendingPathComponent:@"Documents"]]; 

     NSString *filePath = [resourceDocPath stringByAppendingPathComponent:@"Database.sqlite"]; 

     [dbFile writeToFile:filePath atomically:YES]; 
     _db = [[MDDatabase alloc] initWithPath:filePath]; 
     _HTMLRenderer = [[MDHTMLRenderer alloc] init]; 

Editted:私はにコードを変更し は私がフォローする私のコードを変更しますが、クラッシュしています。

- (id)init 
{ 
    self = [super init]; 
    if (self) { 
     [self performSelectorOnMainThread:@selector(downalod) withObject:nil waitUntilDone:YES]; 
     NSString *resourceDocPath = [[NSString alloc] initWithString:[[[[NSBundle mainBundle] resourcePath] stringByDeletingLastPathComponent] stringByAppendingPathComponent:@"Documents"]]; 

     NSString *filePath = [resourceDocPath stringByAppendingPathComponent:@"Database.sqlite"]; 
     _db = [[MDDatabase alloc] initWithPath:filePath]; 
     _HTMLRenderer = [[MDHTMLRenderer alloc] init]; 
    } 
    return self; 
} 

-(void)download 
{ 

    NSData *dbFile = [[NSData alloc] initWithContentsOfURL:[NSURL URLWithString:@"http://www.someurl.com/DatabaseName.sqlite"]]; 

    NSString *resourceDocPath = [[NSString alloc] initWithString:[[[[NSBundle mainBundle] resourcePath] stringByDeletingLastPathComponent] stringByAppendingPathComponent:@"Documents"]]; 

    NSString *filePath = [resourceDocPath stringByAppendingPathComponent:@"Database.sqlite"]; 

    [dbFile writeToFile:filePath atomically:YES]; 
} 
+0

正しくDocumentsフォルダへの参照を取得するための無数の例があります。 'NSDocumentDirectory'で検索してください。 – rmaddy

答えて

0

は、最終的に私はそれを自分で解決:

- (id)init 
{ 
    self = [super init]; 
    if (self) { 
     NSData *fetchedData = [NSData dataWithContentsOfURL:[NSURL URLWithString:@"http://www.someurl.com/db.sqlite3"]]; 
     NSString *documentsPath = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) lastObject]; 
     NSString *filePath = [documentsPath stringByAppendingPathComponent:@"db.sqlite3"]; 
     [fetchedData writeToFile:filePath atomically:YES]; 
     _db = [[MDDatabase alloc] initWithPath:filePath]; 
     _HTMLRenderer = [[MDHTMLRenderer alloc] init]; 
    } 
    return self; 
} 
0
  1. あなたのファイルに問題があります。正しいURLで確認してください。そのエンコードの問題。試してみてください(それは動作します):http://spaceflight.nasa.gov/gallery/images/apollo/apollo17/hires/s72-55482.jpg
  2. あなたのplistにアプリケーション転送セキュリティ設定(任意の負荷を許可するYES)を追加してください。
  3. バックグラウンドモードを有効にします。
  4. .Mでこのマクロを宣言

    #define DocumentsDirectory [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) objectAtIndex:0] 
    

今すぐ電話して、あなたのローカルにファイルを持っています。以下のような

コール:

長い時間の検索の後
[self performSelectorOnMainThread:@selector(downalod) withObject:nil waitUntilDone:NO]; 

    _db = [[MDDatabase alloc] initWithPath:filePath]; 
    _HTMLRenderer = [[MDHTMLRenderer alloc] init]; 

-(void)download 
{ 

    NSString *stringURL =[NSString stringWithFormat: @"url"]; 
NSURL *url = [NSURL URLWithString:[NSString stringWithFormat:@"%@",[stringURL stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding]]]; 
NSURLRequest *urlRequest = [NSURLRequest requestWithURL:url]; 
NSOperationQueue *queue = [[NSOperationQueue alloc] init]; 

NSString *filePath = [DocumentsDirectory stringByAppendingPathComponent:[url lastPathComponent]]; 
NSLog(@"success--222,%@", filePath); 

[NSURLConnection sendAsynchronousRequest:urlRequest queue:queue completionHandler:^(NSURLResponse *response, NSData *data, NSError *error) 
{ 

    NSLog(@"success--222,%@", filePath); 


    if (error) 
    { 
     NSLog(@"success--not---Error,%@", [error localizedDescription]); 
    } 
    else 
    { 
     NSLog(@"success--yes... %@", [[NSString alloc] initWithData:data encoding:NSASCIIStringEncoding]); 

     [data writeToFile:filePath atomically:YES]; 

    } 
}]; 
} 
+0

'self performSelectorOnMainThread'コードはどこに置くべきですか? ' - (id)init'の下にありますか? – user2872856

+0

はい。お知らせ下さい。 –

+0

私は誰がそれをdownvote知っていません。あなたの下投票の原因を説明してください。 –