IOS app initialization or upgrade, involving local database migration issues

Source: Internet
Author: User

General Ideas

After the General app is launched, there is an initialization process. In addition to subsequent app upgrades, data migration also needs to be considered. So the framework of initialization and data migration should be considered in the initial version.

Summarize the scenarios that our app takes:

1, in the persistent folder (such as Userdefaults or documents directory), with a field to save the old version number

2, before starting initialization, read the old version number, as well as the current version number

3, if the application is loaded for the first time, then the old version number will not be taken (because it is the initial load, this field has not been saved), then the initialization process can be performed, if the old version number is taken, do not perform initialization

4. After initialization is complete, perform data migration. Because of the old version number and the new version number, the incremental migration can be achieved by contrast.

5, after the above actions are completed, refresh the old version number

6, the next normal startup, will not be initialized, and will not perform data migration; If the new version is installed, the data migration will be triggered by the current version number refresh

Scenarios where users switch accounts

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

The field that identifies the old version number first cannot be saved in Userdefaults because the userdefaults is shared by the user. This way, when a user initializes, the old version number is present. Switch to the B user, found that the old version number already exists, will not perform initialization, in fact, this time the B user's data file has not been created well. So you need to put the old version number in a separate place, such as each user's own SQLite file

Then, when reading the old version number, but also according to the user's independent identity to query

Improved

For the time being, the old version number is stored in SQLite, but the logic of judgment is troublesome when it is first read. You need to determine if the SQLite file exists, and then you have to determine if the table has, and finally can be evaluated. It might be a little easier to save with text, a step less than the presence of SQLite to determine if a table exists

Schematic code

?
1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162 -(BOOL) needInit{    return [oldVersion isEqual: @"0"];}-(NSString*) oldVersion{    return oldVersion;}-(NSString*) currentVersion{    return currentVersion;}#pragma mark - private method-(void) initOldVersion{    // 数据库文件不存在,oldVersion设为0    NSFileManager *fileManager = [NSFileManager defaultManager];    NSString *dbFilePath = [YLSGlobalUtils getDatabaseFilePath];    if(![fileManager fileExistsAtPath:dbFilePath]){        oldVersion = @"0";        return;    }        // 数据库文件打开失败,oldVersion设为0    FMDatabase *db = [FMDatabase databaseWithPath:dbFilePath];    if(![db open]){        oldVersion = @"0";        return;    }        // tb_clientstage表不存在,oldVersion设为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;    }         // 设置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 judgment:

?
12345678 clientInfo = [YLSClientInfo new];if([clientInfo needInit]){    [self createEverythingForFirstTime];// 初始化时才执行}[self allTheTime];// 任何时候都执行[migrationHelper doMigration:clientInfo];


Incremental migration:

?
1234567891011121314151617181920212223242526 -(void) doMigration:(YLSClientInfo*)clientInfo{    NSString *oldVersion = [clientInfo oldVersion];    NSString *currentVersion = [clientInfo currentVersion];        // 正常登陆,不需要数据迁移    if([oldVersion isEqualToString:currentVersion]){        return;    }        // 全新安装,非升级,不需要数据迁移    if([oldVersion isEqualToString:@"0"]){        return;    }        // 以下均是版本升级,需要数据迁移    if([oldVersion isEqualToString:@"1.0.0"]){        [script1 doMigration];        [script2 doMigration];        [script3 doMigration];        [script4 doMigration];        return;    }        // 其他的情况}

 

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.