Ios app initialization and data migration design ideas

Source: Internet
Author: User

General idea

Generally, an app has an initialization process after it is started. In addition, data migration needs to be considered for subsequent app upgrades. Therefore, the initialization and data migration frameworks should be considered in the initial version.

Summarize the solutions adopted by our app:

1. In a persistent folder (such as the UserDefaults or Documents directory), use a field to save the old version number.

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

3. If the application is loaded for the first time, the old version number cannot be obtained (because this is the first time the application is loaded and this field has not been saved), then the initialization process can be executed; if the old version number is obtained, Initialization is not performed.

4. Execute data migration after Initialization is complete. Because there are old and new versions, you can compare them to achieve incremental migration.

5. After the above actions are completed, refresh the old version number

6. The next normal start will not initialize or execute data migration. If a new version is installed, the data migration will be triggered due to the refreshing of the current version number.

User Account switching scenario

The above is a simple scenario. If the application allows multiple users to switch accounts and the data of different users is separated, it is more complicated.

First, the field that identifies the old version number cannot be saved in UserDefaults, because UserDefaults is shared by users. In this way, after user A is initialized, the old version number will exist. Switch to user B. If the old version number already exists, initialization will not be performed. In fact, the data file of user B has not been created yet. Therefore, the old version number must be stored separately, for example, in each user's sqlite File

Then, when reading the old version number, you must query it based on the user's independent ID.

Improvement

Currently, the old version number is saved in sqlite, but the logic is difficult to judge during the first read. You must determine whether the sqlite file exists and whether the table exists before obtaining the value. It may be a little more convenient to save the data in text. Compared with sqlite, there is less than a step to determine whether the table exists.

Sample Code

-(BOOL) needInit {return [oldVersion isEqual: @ "0"];}-(NSString *) oldVersion {return oldVersion;}-(NSString *) currentVersion {return currentVersion ;} # pragma mark-private method-(void) initOldVersion {// The database file does not exist. The oldVersion is set to 0 NSFileManager * fileManager = [NSFileManager defamanager]; NSString * dbFilePath = [YLSGlobalUtils getDatabaseFilePath] if (! [FileManager fileExistsAtPath: dbFilePath]) {oldVersion = @ "0"; return;} // The database file fails to be opened. The oldVersion is set to 0 FMDatabase * db = [FMDatabase databaseWithPath: dbFilePath]; if (! [Db open]) {oldVersion = @ "0"; return;} // The tb_clientstage table does not exist. The 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 _ clientstage '"]; 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, determine whether to perform initialization:

ClientInfo = [YLSClientInfo new]; if ([clientInfo needInit]) {[self createEverythingForFirstTime]; // executed only during initialization} [self allTheTime]; // execute [migrationHelper doMigration: clientInfo] at any time;

Incremental migration:

-(Void) doMigration :( YLSClientInfo *) clientInfo {NSString * oldVersion = [clientInfo oldVersion]; NSString * currentVersion = [clientInfo currentVersion]; // normal login, if ([oldVersion isEqualToString: currentVersion]) {return;} // completely installed, non-upgraded, no data migration if ([oldVersion isEqualToString: @ "0"]) {return;} // if ([oldVersion is1_tostring: @ "1.0.0"]) {[script1 doMigration]; [script2 doMigration]; [script3 doMigration]; [script4 doMigration]; return ;}// other situations}


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.