Design ideas for iOS app initialization and data migration

Source: Internet
Author: User

Holistic approach

After the General app is launched, there is an initialization process.

You might also need to consider data migration when you upgrade your app. So the framework of initialization and data migration. The initial version number should be considered.

Summarize the scenarios that our app takes:

1, in the persistent folder (for example, Userdefaults or Documents folder), with a field to save the old version

2. Read the old version before beginning initialization. and the current version

3, if the application is first loaded, then the old version is not available (because it is the initial load, this field has not been saved), then you can run the initialization process. Assume that the old version was taken. Initialization is not run

4. After the initialization is complete, run the data migration.

Because of the old version and the new version, it is possible to achieve incremental migration by comparison

5, after the above actions are finished, refresh the old version

6, the next normal startup, will not be initialized, and will not run the data migration; Assume that the new version number is installed, because the current version number is refreshed and the data migration is triggered

Scenarios where users switch accounts

It says a simpler scenario. It is more complicated to assume that the application agrees with multiple users to switch accounts, and that the data of different users is separate.

First identify the old version of the field cannot be saved in Userdefaults, because Userdefaults is shared by the user. This way, when a user initializes, the old version exists.

Switch to User B and find that the old version already exists. The initialization is not run, in fact the data file for the B user is not yet created. So there is a need to put the old version in a separate place. For example, each user's own SQLite file

Then, read the old version of the time. Also according to the user's independent identity to query

Improved

The temporary is to save the old version in SQLite, but this is the first time to read. Inference logic is more troublesome. You need to infer whether sqlite files exist. Then you have to infer if the table has. The last talent to take the value. It might be a little easier to save with text, a step less to infer if the table exists than in SQLite.

Schematic code

-(BOOL) needinit{return [oldversion isequal: @ "0"];} -(nsstring*) oldversion{return oldversion;} -(nsstring*) currentversion{return currentversion;} #pragma mark-private method-(void) initoldversion{//database file does not exist.    Oldversion set to 0 nsfilemanager *filemanager = [Nsfilemanager Defaultmanager];    NSString *dbfilepath = [Ylsglobalutils Getdatabasefilepath]; if (![        FileManager Fileexistsatpath:dbfilepath]) {oldversion = @ "0";    Return }//Database file open failed.    Oldversion set to 0 fmdatabase *db = [Fmdatabase Databasewithpath:dbfilepath]; if (![        DB Open]) {oldversion = @ "0";    Return    }//Tb_clientstage table does not exist, Oldversion is set to 0 int tablecount = 0; Fmresultset *rs = [db executequery:@ "SELECT COUNT (*) as Count from Sqlite_master where type= ' table ' and Name= ' Tb_clientsta    GE ' "];    if ([Rs next]) {Tablecount = [rs intforcolumn:@ "count"];        } if (Tablecount = = 0) {oldversion = @ "0";        [DB close];    Return }//Set OLDVErsion rs = [db executequery:@ "select * from tb_clientstage where id = ' 1 ' and Tableno = ' 0 '"];    if ([Rs next]) {oldversion = [rs stringforcolumn:@ "version"];    }else{oldversion = @ "0"; } [DB close];}    -(void) initcurrentversion{nsdictionary* infodict =[[nsbundle Mainbundle] infodictionary];    nsstring* versionnum =[infodict objectforkey:@ "Cfbundleversion"]; CurrentVersion = Versionnum;}

Then, whether to initialize the inference:

Clientinfo = [Ylsclientinfo new];if ([clientinfo needinit]) {    [self createeverythingforfirsttime];//only runs when initialized]}[self allthetime];//run at all times [Migrationhelper Domigration:clientinfo];

Incremental migration:

-(void) Domigration: (ylsclientinfo*) clientinfo{    nsstring *oldversion = [Clientinfo oldversion];    NSString *currentversion = [Clientinfo CurrentVersion];        Normal landing. Does not require data migration    if ([oldversion isequaltostring:currentversion]) {        return;    }        New installation, non-upgrade, no data migration    if ([Oldversion isequaltostring:@ "0"]) {        return;    }        The following are version number upgrades that require data migration    if ([oldversion isequaltostring:@ "1.0.0"]) {        [script1 domigration];        [Script2 domigration];        [Script3 domigration];        [Script4 domigration];        return;    }        Other situation}


Design ideas for iOS app initialization and data migration

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.