gpt4 book ai didi

ios - 如何用应用商店中的新版本替换 iOS 应用中预先填充的核心数据?

转载 作者:行者123 更新时间:2023-11-30 13:48:50 25 4
gpt4 key购买 nike

我将预加载的核心数据作为 3 个捆绑文件(.sqlite、.sqlite-shm、.sqlite-wal)包含在我的应用程序中,并在用户下载应用程序时将它们用作主要核心数据。

这就是我在 AppDelegate.swift 中的内容

lazy var persistentStoreCoordinator: NSPersistentStoreCoordinator? = {
// The persistent store coordinator for the application. This implementation creates and return a coordinator, having added the store for the application to it. This property is optional since there are legitimate error conditions that could cause the creation of the store to fail.
// Create the coordinator and store
var coordinator: NSPersistentStoreCoordinator? = NSPersistentStoreCoordinator(managedObjectModel: self.managedObjectModel)
let url = self.applicationDocumentsDirectory.URLByAppendingPathComponent("coreDB.sqlite")




/******************** If core data is empty, import from the bundle *******************/
if !NSFileManager.defaultManager().fileExistsAtPath(url.path!) {
let sourceSqliteURLs = [NSBundle.mainBundle().URLForResource("coreDB", withExtension: "sqlite")!, NSBundle.mainBundle().URLForResource("coreDB", withExtension: "sqlite-wal")!, NSBundle.mainBundle().URLForResource("coreDB", withExtension: "sqlite-shm")!]

let destSqliteURLs = [self.applicationDocumentsDirectory.URLByAppendingPathComponent("coreDB.sqlite"),
self.applicationDocumentsDirectory.URLByAppendingPathComponent("coreDB.sqlite-wal"),
self.applicationDocumentsDirectory.URLByAppendingPathComponent("coreDB.sqlite-shm")]

var error:NSError? = nil
for var index = 0; index < sourceSqliteURLs.count; index++ {
do {
try NSFileManager.defaultManager().copyItemAtURL(sourceSqliteURLs[index], toURL: destSqliteURLs[index])
} catch var error1 as NSError {
error = error1
} catch {
fatalError()
}
}
}


var error: NSError? = nil
var failureReason = "There was an error creating or loading the application's saved data."
do {
try coordinator!.addPersistentStoreWithType(NSSQLiteStoreType, configuration: nil, URL: url, options: [NSMigratePersistentStoresAutomaticallyOption: true, NSInferMappingModelAutomaticallyOption: true])
} catch var error1 as NSError {
error = error1
coordinator = nil
// Report any error we got.
var dict = [String: AnyObject]()
dict[NSLocalizedDescriptionKey] = "Failed to initialize the application's saved data"
dict[NSLocalizedFailureReasonErrorKey] = failureReason
dict[NSUnderlyingErrorKey] = error
error = NSError(domain: "YOUR_ERROR_DOMAIN", code: 9999, userInfo: dict)
// Replace this with code to handle the error appropriately.
// abort() causes the application to generate a crash log and terminate. You should not use this function in a shipping application, although it may be useful during development.
NSLog("Unresolved error \(error), \(error!.userInfo)")
//abort()
} catch {
fatalError()
}

return coordinator
}()

现在我必须更改核心数据中的一些数据,所以这就是我所做的:

1) 使用 xcode 更改核心数据中的一些数据,以便当应用程序在 iOS 模拟器中运行时数据会发生变化

2)找到.sqlite、.sqlite-shm、sqlite-wal文件,这些文件属于iOS模拟器

3) 将这 3 个文件拖到 xcode 中并将它们包含在应用程序包中

4)当用户更新应用程序时,如何使新更新的应用程序将其核心数据替换为这3个文件? Lightmigration 是否可以解决此类问题?

最佳答案

我在我的一个应用程序中执行了相同的操作,所有 sqlite 相关文件都与该应用程序捆绑在一起,在第一次启动时,我将它们复制到一个目录中,该目录的路径是使用应用程序版本号组成的。

- (NSString *)applicationDatabaseDirectory
{
NSString * documentsDir = [self applicationDocumentsDirectory];
NSString * databasePath = [documentsDir stringByAppendingPathComponent:[NSString stringWithFormat:@"Database/%@",[self appVersionString]]];
NSFileManager * fileManager = [[NSFileManager alloc] init];
BOOL isDir = YES;
if (![fileManager fileExistsAtPath:databasePath isDirectory:&isDir]) {
[fileManager createDirectoryAtPath:databasePath withIntermediateDirectories:YES attributes:nil error:nil];
}
return databasePath;
}
- (NSString *) appVersionString {
return [[[NSBundle mainBundle] infoDictionary] objectForKey:@"CFBundleShortVersionString"];
}

请记住,每次向模型添加一些新属性时,都必须通过迁移更新 bundle 中的数据库,并再次复制文件,删除旧文件。

关于ios - 如何用应用商店中的新版本替换 iOS 应用中预先填充的核心数据?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34576077/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com