Referring to NSPersistentStore instances created in AppDelegate

丶灬走出姿态 提交于 2019-12-25 04:27:07

问题


I modified the boilerplate core data stack code you get with a core data application to add two NSPersistentStores to the NSPersistentStoreCoordinator instead of just one.

// 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()
        }
    }
}

I need to specify the store when adding NSManagedObject objects like below.

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

And specifying the stores which I need to fetch data from like this.

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

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

My question is how can I get a reference to those NSPersistentStores created in the AppDelegate?

Just to be clear, I already know how to get a reference to the AppDelegate itself.

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

It's the getting the NSPersistentStore part is where I'm stuck.


I tried creating it as a separate property in the AppDelegat like this but I don't know how to call it/add it from 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
}

Besides whenever I call this property, wouldn't it add a new instance of a store to the coordinator?


回答1:


There is a persistentStores property on NSPersistentStoreCoordinator you can use for this purpose. It returns an array of (presumably) NSPersistentStore instances.

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



回答2:


To get the reference of property in app delegate you will need to get app delegate reference first by doing so:

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

and then you can get and use any property from your appDelegate object

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


来源:https://stackoverflow.com/questions/29074157/referring-to-nspersistentstore-instances-created-in-appdelegate

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!