带有预填充sqlite数据库的iOS ship应用程序
我想将我的ios应用程序(用swift编写)与预填充的sqlite数据库一起发布,该数据库将通过与服务器同步进行更新。在安卓系统中,我可以将数据库从assests文件夹移动到数据库和volla,但在ios系统中,我完全不知道该如何实现这一点 谢谢简单的方法是: +选择库文件夹/子文件夹(或文档,如果需要)中的某个位置的持久URL。 +在实现persistentStoreCoordinator时,首先检查上面的URL中是否存在.sqlite。如果不是,则将.sqlite文件复制到该位置,如果是,则不执行任何操作。 +使用该url初始化持久存储带有预填充sqlite数据库的iOS ship应用程序,ios,swift,sqlite,Ios,Swift,Sqlite,我想将我的ios应用程序(用swift编写)与预填充的sqlite数据库一起发布,该数据库将通过与服务器同步进行更新。在安卓系统中,我可以将数据库从assests文件夹移动到数据库和volla,但在ios系统中,我完全不知道该如何实现这一点 谢谢简单的方法是: +选择库文件夹/子文件夹(或文档,如果需要)中的某个位置的持久URL。 +在实现persistentStoreCoordinator时,首先检查上面的URL中是否存在.sqlite。如果不是,则将.sqlite文件复制到该位置,如果是,则
您必须确保.sqlite的数据与iOS中的DB模型相匹配。您可以将数据库添加到项目支持文件中。这将与二进制文件一起添加,然后您可以访问并跨二进制文件进行复制 要从中获取预填充数据库的位置,请执行以下操作: 使用以下命令获取文档目录的URL: 最后复制文件:
if let source = databasePath, destination = documentsDirectory
{
destination = destination.URLByAppendingPathComponent("database.sqlite3")
var result = NSFileManager.defaultManager().copyItemAtURL(source, toURL:destination, error:nil)
//Should have an error pointer, check that the result is true. If not check error.
}
将预填充的数据库添加到主捆绑包,加载时检查数据库是否存在于文档目录中,如果文件不存在,则从主捆绑包复制它。然后使用document directory.NSBundle中的数据库,NSFileManager将成为您的朋友:)可能的重复是否可能我只将database.sqlite3中的一个实体中的记录复制到我已经存在的数据库中?
//Should have an error pointed in case there is an error.
let documentsDirectory = NSFileManager.defaultManager().URLForDirectory(NSSearchPathDirectory.DocumentDirectory, inDomain:NSSearchPathDomainMask.UserDomainMask, url:nil, shouldCreate:false, error:nil);
if let source = databasePath, destination = documentsDirectory
{
destination = destination.URLByAppendingPathComponent("database.sqlite3")
var result = NSFileManager.defaultManager().copyItemAtURL(source, toURL:destination, error:nil)
//Should have an error pointer, check that the result is true. If not check error.
}