如何在核心数据Swift 3中使用persistentStore(for:url)

吴大卫

我正在尝试执行以下操作:

点击一个单元格中的一个UITableView单元格,然后segue移至下一个UIViewController并显示数据库结果。但是有多个持久性存储,因此指定的存储由单元标签文本指定。

问题是:如何使用该方法persistentStore(for: url)还是有其他方法为?指定一个持久存储fetchRequest

这是我的代码不起作用:

func wordFetchRequest() -> NSFetchRequest<Word> {
    let fr = NSFetchRequest<Word>(entityName: "Word")
    fr.fetchBatchSize = 100

    // Assigning sort descriptors
    let firstLetterSort = NSSortDescriptor(key: #keyPath(Word.firstLetter), ascending: true)
    let spellSort = NSSortDescriptor(key: #keyPath(Word.spell), ascending: true)
    fr.sortDescriptors = [firstLetterSort, spellSort]

    // Get URL of the designated store to fetch
    let libname = (AppDelegate.nameDict as NSDictionary).allKeys(for: nameToFetch).first!

// I'm not sure the following line: which file should I use? I've tried
//.sqlite, .sqlite-shm and .sqlite-wal but none worked.
    let url = AppDelegate.coreDataStack.storeDirectory.appendingPathComponent("\(libname).sqlite-wal")

    // Specify affected store for the fetch request
    var pss = [NSPersistentStore]()
    print(url)

// The following line fails:
    if let ps = coreDataStack.psc.persistentStore(for: url) {
        pss.append(ps)
    } else {

    }

    fr.affectedStores = pss
    print(fr.affectedStores ?? "No stores available.")
    return fr
}

任何帮助都感激不尽。

桑迪普·班达里(Sandeep Bhandari)

我不得不处理类似的情况,即我拥有不同的持久性存储(具体来说,一种类型为NSInMemoryStoreType,另一种类型为NSSQLiteStoreType

我发现为每个商店创建单独的持久性存储协调器和使用这些持久性存储(如父存储)创建单独的管理对象上下文更加容易

这是用iOS 9 swift 3编写的代码,因此具有较旧的核心数据栈操作,我已经看过iOS 10 Swift 3 Core数据栈,我相信这些方法仍然可以使您了解这里所说的内容:)

这就是你将默认在Coredata堆栈看到,吸气persistentStoreCoordinator

lazy var persistentStoreCoordinator: NSPersistentStoreCoordinator = {
        // The persistent store coordinator for the application. This implementation creates and returns 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
        let coordinator = NSPersistentStoreCoordinator(managedObjectModel: self.managedObjectModel)
        let url = self.applicationDocumentsDirectory.appendingPathComponent("SingleViewCoreData.sqlite")
        var failureReason = "There was an error creating or loading the application's saved data."
        do {
            try coordinator.addPersistentStore(ofType: NSSQLiteStoreType, configurationName: nil, at: url, options: nil)
            log.debug(url)
        } catch let error as NSError {
            // Report any error we got.
            var dict = [String: AnyObject]()
            dict[NSLocalizedDescriptionKey] = "Failed to initialize the application's saved data" as AnyObject?
            dict[NSLocalizedFailureReasonErrorKey] = failureReason as AnyObject?

            dict[NSUnderlyingErrorKey] = error
            let wrappedError = 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 \(wrappedError), \(wrappedError.userInfo)")
            abort()
        }
        catch{

        }
        return coordinator
    }()

重要的声明在这里是

try coordinator.addPersistentStore(ofType: NSSQLiteStoreType, configurationName: nil, at: url, options: nil)

如您所见,它将持久性存储类型指定为Sqlite,并将configurationName指定为nil,这意味着默认配置:)

您可以在Coredata中创建多个配置,并在此语句中指定那里的名称,以便为每个配置创建单独的持久性存储协调器:)

您可以在我的博客中查看是否可以使用敏感信息信任核心数据,以了解如何创建多个配置和存储:)

因此,假设您创建了另一个配置并向其中添加了实体,并将其称为“ Test1”配置,您将为此创建一个单独的持久性存储协调器,

lazy var test1PersistentStoreCoordinator: NSPersistentStoreCoordinator = {
    // The persistent store coordinator for the application. This implementation creates and returns 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
    let coordinator = NSPersistentStoreCoordinator(managedObjectModel: self.managedObjectModel)
    let url = self.applicationDocumentsDirectory.appendingPathComponent("SingleViewCoreData.sqlite")
    var failureReason = "There was an error creating or loading the application's saved data."
    do {
        try coordinator.addPersistentStore(ofType: NSInMemoryStoreType, configurationName: "Test1", at: url, options: nil)
        log.debug(url)
    } catch let error as NSError {
        // Report any error we got.
        var dict = [String: AnyObject]()
        dict[NSLocalizedDescriptionKey] = "Failed to initialize the application's saved data" as AnyObject?
        dict[NSLocalizedFailureReasonErrorKey] = failureReason as AnyObject?

        dict[NSUnderlyingErrorKey] = error
        let wrappedError = 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 \(wrappedError), \(wrappedError.userInfo)")
        abort()
    }
    catch{

    }
    return coordinator
}()

现在,您有两个与两种不同配置相关联的持久性存储协调器,只需使用这些持久性存储协调器作为其父存储创建两个托管对象上下文即可:)

   lazy var managedObjectContext: NSManagedObjectContext = {
        // Returns the managed object context for the application (which is already bound to the persistent store coordinator for the application.) This property is optional since there are legitimate error conditions that could cause the creation of the context to fail.
        let coordinator = self.persistentStoreCoordinator
        var managedObjectContext = NSManagedObjectContext(concurrencyType: .mainQueueConcurrencyType)
        managedObjectContext.persistentStoreCoordinator = coordinator
        return managedObjectContext
    }()


    lazy var managedObjectContextForBackTracking : NSManagedObjectContext = {
        let coordinator = self.test1PersistentStoreCoordinator
        var managedObjectContext = NSManagedObjectContext(concurrencyType: .privateQueueConcurrencyType)
        managedObjectContext.persistentStoreCoordinator = coordinator
        return managedObjectContext
    }()

就是这样 :)

现在,在相应的managedObject上下文中运行获取请求:),并确保没有混乱您的核心数据:)

希望能帮助到你 :)

本文收集自互联网,转载请注明来源。

如有侵权,请联系[email protected] 删除。

编辑于
0

我来说两句

0条评论
登录后参与评论

相关文章

来自分类Dev

如何在Swift中创建核心数据关系

来自分类Dev

如何在核心数据中存储数组(Swift)

来自分类Dev

如何在swift 3中使用核心数据为多个部门保存一条记录

来自分类Dev

如何在Swift 2中从核心数据中删除值

来自分类Dev

Swift-如何在OS X中定义核心数据上下文

来自分类Dev

核心数据:如何在Swift中删除具有属性的所有对象?

来自分类Dev

如何在Swift 2.0中滑动删除核心数据表视图

来自分类Dev

如何在ForEach SwiftUI中使用核心数据关系

来自分类Dev

核心数据如何在多线程中使用NSMangedObjectContext

来自分类Dev

Swift中的核心数据

来自分类Dev

如何在核心数据中顺序存储数据?

来自分类Dev

如何在iOS的核心数据中存储JSON数据

来自分类Dev

Swift核心数据:核心数据中的枚举

来自分类Dev

SWIFT的核心数据:如何从关系实体中删除对象?

来自分类Dev

iOS Swift-如何使用核心数据存储数组?

来自分类Dev

如何在核心数据中存储NSDate?

来自分类Dev

如何在核心数据中存储CGPoint?

来自分类Dev

如何在核心数据中存储快速枚举?

来自分类Dev

如何在核心数据中连接实体

来自分类Dev

如何在核心数据中存储NSDate?

来自分类Dev

如何在核心数据中存储UIButton值?

来自分类Dev

如何在 Swift 3 中重新排序核心数据对象数组并重新排序它的 Int 属性

来自分类Dev

Swift 3中的新核心数据

来自分类Dev

核心数据使用Swift保存数据

来自分类Dev

Swift 3核心数据删除对象

来自分类Dev

Swift 3核心数据指南

来自分类Dev

核心数据中的Swift元组

来自分类Dev

使用Swift检索核心数据

来自分类Dev

使用Swift检索核心数据

Related 相关文章

热门标签

归档