2012-01-09 79 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; 
} 
相關問題