2012-01-09 81 views
17

我在应用商店有1.0版本的应用程序,它使用sqlite数据库读取数据。
现在我想用数据库文件中的更新将我的版本更新到1.1。
在设备上安装应用程序时使用开发人员证书时,由于数据库文件已存在于文档文件夹中,因此未更新数据库,因此我必须手动删除应用程序并重新安装该应用程序。
我的问题是,当任何用户更新应用程序时,数据库是否也会根据当前版本进行更新。
欢迎任何建议。
感谢当iPhone上的应用程序版本更改时,sqlite数据库更新

+0

数据是只读还是读/写? – 2012-01-09 20:04:40

+0

数据是只读的。 – 2012-01-09 20:06:03

回答

14

我相信有很多方法可以做到这一点(和很多方面优于矿一样),但我处理这些问题的方式如下:

首先,我在定义一个常量应用程序(一个将首先加载)的第一个.h文件中,以指示首次加载,并将其设置为0:

#define FirstTime 0 

现在你要知道,我有意向保存此常数的值在Documents文件夹中以备将来参考,因此我使用共享数据实例。在viewDidLoad我做如下测试:

//if first time run of this version 
if([MyDataModel sharedInstance].count < (FirstTime + 1)) 
{ 
    //do what you need to do as the first time load for this version 

    [MyDataModel sharedInstance].count++ 
    //save the count value to disk so on next run you are not first time 
    //this means count = 1 
} 

现在关键是你的新的应用程序版本(比如1.1)。我改变FirstTime以2:

#define FirstTime 2 

由于光盘上保存的第一时间值是1,这意味着你会被if语句上面被抓住,因此在它里面,你可以做任何你想要的喜欢删除旧表并用新编组重新创建它们。

再次不是那么辉煌,而是解决了案子!

6

您可以使用.plist还有:

- (void)isItTheFirstTimeAfterUpdate { 
    NSString *versionnum; 
    NSError *error; 
    NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, 
                 NSUserDomainMask, 
                 YES); 
    NSString *documentsDirectory = [paths objectAtIndex:0]; 
    NSString *path = [documentsDirectory stringByAppendingPathComponent:@"yourplist.plist"]; 
    NSFileManager *fileManager = [NSFileManager defaultManager]; 

    if(![fileManager fileExistsAtPath:path]) { 
     NSString *bundle = [[NSBundle mainBundle] pathForResource:@"yourplist" ofType:@"plist"]; 
     [fileManager copyItemAtPath:bundle toPath:path error:&error]; 
    } 

    NSMutableDictionary *savedStock = [[NSMutableDictionary alloc] initWithContentsOfFile:path]; 
    versionnum = @""; 
    //it can be installed by user (for ex. it is 1.3 but first installed), no plist value is set before 
    if(([savedStock objectForKey:@"versionnum"]) && (![[savedStock objectForKey:@"versionnum"] isEqualToString:@""])){ 
     versionnum = [savedStock objectForKey:@"versionnum"]; 
    } 

    //to get the version of installed/updated-current app 
    NSString *myversion = [NSString stringWithFormat:@"%@",[[[NSBundle mainBundle] infoDictionary] objectForKey:@"CFBundleVersion"]]; 
    //if no version has been set-first install- or my version is the latest version no need to do sth. 
    if ([versionnum isEqualToString:myversion] || [versionnum isEqualToString:@""]) { 
     NSLog(@"Nothing has to be done"); 
    } 
    else { 
     [self cleanDB];//i have clean tables and create my new db tables maybe logout the user etc. 
     [savedStock setObject:[NSString stringWithString:myversion] forKey:@"versionnum"];//setting the new version 
     [savedStock writeToFile:path atomically:YES]; 
    } 
} 

而且你可以调用函数在应用程序启动或在您的主视图控制器的视图控制器..你的选择。

希望它有帮助。

12

该方法依赖于NSUserDefaults。这个想法是从NSUserDefaults获得以前的应用程序版本号(如果存在),并将其与当前版本进行比较。

如果以前的应用程序版本是<而不是当前版本,或者以前的版本是nil,代码将执行数据库升级。这意味着即使应用已经发布在AppStore上,也可以使用这种方法。它会在应用程序更新期间将数据库升级到新版本。

这是一个plist文件:

enter image description here

有其由版本号和相应的升级版本的一组SQL查询的阵列。 假设以前的版本是1.2并且实际版本是1.4,代码仅从版本1.2执行升级到1.4。如果以前的版本是1.3并且当前版本是1.4,那么代码只能从1.3升级到1.4。 如果以前的版本是零代码执行升级到1.1然后到1.2然后到1.3,最后到1.4。

NSString * const VERSION_KEY = @"version"; 

-(void)upgradeDatabaseIfRequired{ 
    NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults]; 
    NSString *previousVersion=[defaults objectForKey:VERSION_KEY]; 
    NSString *currentVersion=[self versionNumberString]; 

    if (previousVersion==nil || [previousVersion compare: currentVersion options: NSNumericSearch] == NSOrderedAscending) { 
     // previous < current 
     //read upgrade sqls from file 
     NSString *plistPath = [[NSBundle mainBundle] pathForResource:@"UpgradeDatabase" ofType:@"plist"]; 
     NSArray *plist = [NSArray arrayWithContentsOfFile:plistPath]; 


     if (previousVersion==nil) {//perform all upgrades 
      for (NSDictionary *dictionary in plist) { 
       NSString *version=[dictionary objectForKey:@"version"]; 
       NSLog(@"Upgrading to v. %@", version); 
       NSArray *sqlQueries=[dictionary objectForKey:@"sql"]; 
       while (![DB executeMultipleSql:sqlQueries]) { 
        NSLog(@"Failed to upgrade database to v. %@, Retrying...", version); 
       }; 
      } 
     }else{ 
      for (NSDictionary *dictionary in plist) { 
       NSString *version=[dictionary objectForKey:@"version"]; 
       if ([previousVersion compare: version options: NSNumericSearch] == NSOrderedAscending) { 
        //previous < version 
        NSLog(@"Upgrading to v. %@", version); 
        NSArray *sqlQueries=[dictionary objectForKey:@"sql"]; 
        while (![DB executeMultipleSql:sqlQueries]) { 
         NSLog(@"Failed to upgrade database to v. %@, Retrying...", version); 
        }; 
       } 

      } 
     } 

     [defaults setObject:currentVersion forKey:VERSION_KEY]; 
     [defaults synchronize]; 
    } 

} 


- (NSString *)versionNumberString { 
    NSDictionary *infoDictionary = [[NSBundle mainBundle] infoDictionary]; 
    NSString *majorVersion = [infoDictionary objectForKey:@"CFBundleShortVersionString"]; 
    return majorVersion; 
} 
相关问题