-->

参照AppDelegate中创建NSPersistentStore实例(Referring to N

2019-11-03 17:12发布

我修改你一个核心数据应用程序添加二送样板核心数据栈代码NSPersistentStore S到NSPersistentStoreCoordinator而不是一个。

// MARK: - Core Data stack
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("DB", withExtension: "momd")!
    return NSManagedObjectModel(contentsOfURL: modelURL)!
}()

lazy var persistentStoreCoordinator: NSPersistentStoreCoordinator? = {
    var coordinator: NSPersistentStoreCoordinator? = NSPersistentStoreCoordinator(managedObjectModel: self.managedObjectModel)
    var error: NSError? = nil

    let firstStoreURL = self.applicationDocumentsDirectory.URLByAppendingPathComponent("first-store.sqlite")
    if coordinator!.addPersistentStoreWithType(NSSQLiteStoreType, configuration: nil, URL: firstStoreURL, options: nil, error: &error) == nil {
        coordinator = nil        
        NSLog("Unresolved error \(error), \(error!.userInfo)")
        abort()
    }

    let secondStoreURL = self.applicationDocumentsDirectory.URLByAppendingPathComponent("second-store.sqlite")
    if coordinator!.addPersistentStoreWithType(NSSQLiteStoreType, configuration: nil, URL: secondStoreURL, options: nil, error: &error) == nil {
        coordinator = nil
        NSLog("Unresolved error \(error), \(error!.userInfo)")
        abort()
    }
    return coordinator
}()

lazy var managedObjectContext: NSManagedObjectContext? = {
    let coordinator = self.persistentStoreCoordinator
    if coordinator == nil {
        return nil
    }
    var managedObjectContext = NSManagedObjectContext()
    managedObjectContext.persistentStoreCoordinator = coordinator
    return managedObjectContext
}()

// MARK: - Core Data Saving support
func saveContext() {
    if let moc = self.managedObjectContext {
        var error: NSError? = nil
        if moc.hasChanges && !moc.save(&error) {            
            NSLog("Unresolved error \(error), \(error!.userInfo)")
            abort()
        }
    }
}

我需要添加时指定商店NSManagedObject对象,如下面。

let someObject = NSManagedObject(entity: "someEntity", insertIntoManagedObjectContext: context)
context.assignObject(someObject, toPersistentStore: <store instance>)

并指定我需要从这样获取数据的存储。

let entityDescription = NSEntityDescription.entityForName("someEntity", inManagedObjectContext: context)

let fetchRequest = NSFetchRequest()
fetchRequest.entity = entityDescription
fetchRequest.affectedStores = [<store instance>]

我的问题是如何获得这些参考NSPersistentStore在AppDelegate中创建S'

只是要清楚,我已经知道怎么去的AppDelegate中自身的引用。

let appDelegate = UIApplication.sharedApplication().delegate as AppDelegate

这是获得NSPersistentStore部分就是我卡住了。


我试图创建它作为AppDelegat这样一个单独的属性,但我不知道该怎么称呼它/从添加它persistentStoreCoordinator

var firstStore: NSPersistentStore {
    var store: NSPersistentStore!
    if let coordinator = persistentStoreCoordinator {
        let url = self.applicationDocumentsDirectory.URLByAppendingPathComponent("first-store.sqlite")
        var error: NSError?
        store = coordinator.addPersistentStoreWithType(NSSQLiteStoreType, configuration: nil, URL: url, options: nil, error: &error)!
    }
    return store
}

此外,每当我把这个属性,是不是一家商店的新实例添加到协调?

Answer 1:

有一个persistentStores 财产上NSPersistentStoreCoordinator你可以使用这个目的。 它返回(大概)阵列NSPersistentStore实例。

// Get the first store, assuming it exists
let appDelegate = UIApplication.sharedApplication().delegate as AppDelegate
let firstStore = appDelegate.persistentStoreCoordinator.persistentStores[0] as NSPersistentStore


Answer 2:

为了获得在应用程序委托财产的参考,你需要这样做首先获得应用程序的委托参考:

let appDelegate = UIApplication.sharedApplication().delegate as AppDelegate

然后你就可以得到和使用任何财产从您的appDelegate对象

let someObject = NSManagedObject(entity: "someEntity",insertIntoManagedObjectContext: context)
context.assignObject(someObject, toPersistentStore: appDelegate.persistentStoreCoordinator)


文章来源: Referring to NSPersistentStore instances created in AppDelegate