我有一个应用程序与1.0版的应用程序商店,其中使用sqlite数据库读取数据。
现在我想在数据库文件中更新我的版本到1.1。
使用开发者证书时,当我在设备上安装应用程序时,它没有更新数据库,因为数据库文件已经存在于documents文件夹中,所以我必须手动删除应用程序并重新安装它。
我的问题是,当任何用户更新应用程序时,数据库是否也会根据当前版本进行更新。
欢迎提出任何建议。
谢谢
发布于 2012-01-10 04:27:35
我相信有很多方法可以做到这一点(而且有很多方法比我的方法更好),但我处理这些问题的方法如下:
首先,我在应用程序的第一个.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语句捕获,因此在它内部,您可以执行任何您想要的操作,例如删除旧的表,并使用新的形式重新创建它们。
再说一次,不是很聪明,但解决了问题!
发布于 2013-10-15 18:18:36
这种方法依赖于NSUserDefaults
。这个想法是从NSUserDefaults
获取以前的应用程序版本号(如果存在),并将其与当前版本进行比较。
如果以前的应用程序版本比当前版本更高,或者如果以前的版本是nil
,则代码将执行数据库升级。这意味着,即使应用程序已经发布在AppStore上,也可以使用这种方法。它将在应用程序更新期间将数据库升级到新版本。
这是一个plist文件:
有一个数组,它由相应升级版本的版本号和一组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;
}
发布于 2012-08-13 15:46:43
您也可以使用.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];
}
}
您可以在应用程序启动时或在主视图控制器的视图控制器中调用该函数。你自己选吧。
希望能有所帮助。
https://stackoverflow.com/questions/8794397
复制相似问题