我想知道是否有人可以在升级我的应用程序时深入了解我想要的数据迁移。我正在将使用本机 objective-c 编写的应用程序更新为使用jquery mobile的基于cordova的Web视图。
现有应用程序具有使用sqllite3.h实现的“收藏夹”。我想知道是否有可能,当我们将更新发布到应用程序时,我们可以挂接到这个现有的sqlite数据库,并将旧的“收藏夹”迁移到新的数据库中,该数据库是使用localStorage实现的。
任何见解都将不胜感激!
最佳答案
我们只是做了类似的事情。我们以前的本机应用程序正在使用CoreData。我们在MainViewController的webViewDidFinishLoad委托方法中添加了一个检查,以查看是否存在现有数据库。如果是这样,则我们加载了数据库,转换了内容以通过localStorage使用,然后设置一个标志,以便它不再尝试再次导入它。这是将其转移到localStorage的代码段。首先,我们将要传输的信息放入NSMutableString中,但将其格式化为javascript字典
NSMutableString *dict = [NSMutableString string];
[dict appendString:@"{"];
[dict appendFormat:@"'notes':'%@'", notes];
[dict appendFormat:@",'date':'%f'",seconds];
[dict appendFormat:@",'count':'%d'",[ss.count intValue]];
[dict appendFormat:@",'weather':'%@'",wx];
[dict appendFormat:@",'location':'%@'",ss.event.location.name];
[dict appendFormat:@",'latitude':'%@'",[ss.event.location.latitude stringValue]];
[dict appendFormat:@",'longitude':'%@'",[ss.event.location.longitude stringValue]];
[dict appendString:@"}"];
然后将其传递到javascript中,如下所示:
[webView stringByEvaluatingJavaScriptFromString:[NSString stringWithFormat:@"importMyOldData(%@);",dict]];
这有效地调用了javascript方法,并将项的字典传递到其中。然后,您可以使用Javascript将其放入本地存储中,例如:
function importMyOldData( oldData )
{
// if you want to inspect each item
var notes = oldData.notes;
localStorage.setItem( "NoteKey", notes );
// if you want to dump the whole thing
localStorage.setItem( "MyKey", JSON.stringify(oldData) );
}