Titanium - Retrieving SQLite Data from previous version of iOS App

雨燕双飞 提交于 2019-12-31 04:19:08

问题


I've been commissioned to update a native iOS app, but as we're also going to release it to other platforms, we're are writing the new version using Appcelerator's Titanium.

The current app uses a SQLite database to store user information. How do I access the existing SQLite database when the user updates their app to the new one? We don't want to lose any data. I assumed I would be able to access the old database and then create a migration to a new database.

And what is the best way to test this? Can I install a test app with the same app id or perhaps bundle id to simulate an update?

Thanks, Tom


回答1:


This should be possible (although it can get tricky) as long as the previous developers observed Apple rules on where the user information db was stored.

As long as you have the same bundle ID, and increment the version, you can overwrite the existing app during testing to simulate an update (make sure you are using an Ad-hoc distribution), this will leave the user files in place. From there, when you load your app for the first time, load that DB in the usual way:

// Set a property so you do this only one time when they first update
if(Ti.App.Properties.getBool('isDatabaseMigrated', false)) {
    // See if the DB file exists (this may be a fresh install not an update)
    var dbfile = Ti.Filesystem.getFile(Ti.Filesystem.applicationSupportDirectory+'/database', 'nameOfDB.sql');
    if(dbfile.exists()) {
        // Open the DB
        var db = Ti.Database.open('nameOfDB');
        .... Now do your thing ....
        db.close();
    }
    // Set flag so we only check this once
    Ti.App.Properties.setBool('isDatabaseMigrated', true);
}

iOS has a specific directory where they keep DB files, and the newest version of Titanium migrates any old DB's to this directory.



来源:https://stackoverflow.com/questions/23495832/titanium-retrieving-sqlite-data-from-previous-version-of-ios-app

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!