您知道如何与允许读取和写入同一个 .sqlite 的两个应用程序(您是其所有者)共享核心数据吗?
我曾尝试使用 App Groups :
1) Bank.xcdatamodeld BankInfo.swift BankDetails.swift
我已将这些文件复制到此应用程序 B 的项目的目录中(从应用程序 A 的项目中检索),然后将它们拖到 Xcode 中。
2) 我已经从应用组的公共沙箱中检索到 sqlite 文件
应用程序委托:核心数据堆栈 [Swift]
lazy var applicationDocumentsDirectory: NSURL = {
let urls = NSFileManager.defaultManager().URLsForDirectory(.DocumentDirectory, inDomains: .UserDomainMask)
return urls[urls.count-1] as NSURL
}()
lazy var managedObjectModel: NSManagedObjectModel = {
let modelURL = NSBundle.mainBundle().URLForResource("shareapps", withExtension: "momd")!
return NSManagedObjectModel(contentsOfURL: modelURL)!
}()
lazy var persistentStoreCoordinator: NSPersistentStoreCoordinator? = {
// Create the coordinator and store
var coordinator: NSPersistentStoreCoordinator? = NSPersistentStoreCoordinator(managedObjectModel: self.managedObjectModel)
let directory = NSFileManager.defaultManager().containerURLForSecurityApplicationGroupIdentifier("com.sd.shareapps");
let url = directory?.URLByAppendingPathComponent("shareapps.sqlite")
//Sarting frehs every time
NSFileManager.defaultManager().removeItemAtURL(url!, error: nil)
var error: NSError? = nil
var failureReason = "There was an error creating or loading the application's saved data."
if coordinator!.addPersistentStoreWithType(NSSQLiteStoreType, configuration: nil, URL: url, options: nil, error: &error) == nil {
coordinator = nil
// Report any error we got.
let dict = NSMutableDictionary()
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()
}
println("\(coordinator?.persistentStores)")
return coordinator
}()
lazy var managedObjectContext: NSManagedObjectContext? = {
let coordinator = self.persistentStoreCoordinator
if coordinator == nil {
return nil
}
var managedObjectContext = NSManagedObjectContext()
managedObjectContext.persistentStoreCoordinator = coordinator
return managedObjectContext
}()
我收到此错误消息:
"*** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '+entityForName: nil is not a legal NSPersistentStoreCoordinator for searching for entity name 'Entity'"
问候