-->

Which is the best way to download database initial

2019-04-02 01:56发布

问题:

I am developing an application that on the user's first login must download a lot of data from my server and store it in a local database in my iOS app.

In my server I am using MySQL. And I need to download 70MB... to my iOS app to use this data locally, having or not connection.

What's the best way to download this data? XML zipped? Export to a SQLite file, zip this file, and download?

Can you recommend any approach?

回答1:

If the database on the server is dynamic; A good approach is to build an API for accessing the Database data on the server, you can build an API through PHP and have the app just read the responses of data as JSON or XML.

Another approach if the database is static and wont update frequently is to add the SQL database to the IOS app as an SQLite database and just run local sql queries on the app.



回答2:

We use web services and SOAP for transferring data from server to device. You should look into SOAP and REST web services. You basically get back xml which you can parse and use to create appropriate objects.

Although this may be a little too much for what you're trying to do, it's worth looking into.

Also, data can be stored in an SQLite Database, which is supported on most mobile platforms.



回答3:

Yes you can export it to SQLite, zip it and download using AFNetworking.

NSString *yourFileURL=@"http://yourFileURL.zip";
NSURLRequest *request = [NSURLRequest requestWithURL:[NSURL URLWithString:yourFileURL]];
AFURLConnectionOperation *operation =   [[AFHTTPRequestOperation alloc] initWithRequest:request];

NSString *cacheDir = [NSSearchPathForDirectoriesInDomains
                          (NSCachesDirectory, NSUserDomainMask, YES) objectAtIndex:0];
NSString *filePath = [cacheDir stringByAppendingPathComponent:
                      @"youFile.zip"];

operation.outputStream = [NSOutputStream outputStreamToFileAtPath:filePath append:NO];

[operation setDownloadProgressBlock:^(NSUInteger bytesRead, long long totalBytesRead, long long totalBytesExpectedToRead) {
   //show here your downloading progress if needed
}];

[operation setCompletionBlock:^{
    NSLog(@"File successfully downloaded");
}];

[operation start];