带有privateQueueConcurrencyType的NSFetchedResultsController - 更改不调用委托方法

时间:2018-03-12 21:00:24

标签: ios core-data nsfetchedresultscontroller

在设置NSFetchedResultsControllerDelegate NSFetchedResultsControllerDelegate concurrencyTypeprivateQueueConcurrencyType时,我无法调用我的fileprivate lazy var fetchedResultsController: NSFetchedResultsController<Note> = { // Initialize Fetch Request let fetchRequest: NSFetchRequest<Note> = Note.fetchRequest() // Add Sort Descriptors let sortDescriptor = NSSortDescriptor(key: "updatedAt", ascending: false) fetchRequest.sortDescriptors = [sortDescriptor] // Initialize Fetched Results Controller let fetchedResultsController = NSFetchedResultsController(fetchRequest: fetchRequest, managedObjectContext: self.coreDataManager.managedObjectContext, sectionNameKeyPath: nil, cacheName: nil) // Configure Fetched Results Controller fetchedResultsController.delegate = self return fetchedResultsController }() 方法。

考虑这个例子:

coreDataManager

private(set) lazy var managedObjectContext: NSManagedObjectContext = { let managedObjectContext = NSManagedObjectContext(concurrencyType: .mainQueueConcurrencyType) managedObjectContext.persistentStoreCoordinator = self.persistentStoreCoordinator return managedObjectContext }() 类有这个:

fetchedResultsController.managedObjectContext.delete(note)

如果我调用controller:didChangeObject:atIndexPath:forChangeType:newIndexPath然后保存,则调用fileprivate lazy var fetchedResultsController: NSFetchedResultsController<Note> = { // Initialize Fetch Request let fetchRequest: NSFetchRequest<Note> = Note.fetchRequest() // Add Sort Descriptors let sortDescriptor = NSSortDescriptor(key: "updatedAt", ascending: false) fetchRequest.sortDescriptors = [sortDescriptor] let managedObjectContext = NSManagedObjectContext(concurrencyType: .concurrencyType`) managedObjectContext.persistentStoreCoordinator = self.coreDataManager.managedObjectContext.persistentStoreCoordinator // Initialize Fetched Results Controller let fetchedResultsController = NSFetchedResultsController(fetchRequest: fetchRequest, managedObjectContext: managedObjectContext, sectionNameKeyPath: nil, cacheName: nil) // Configure Fetched Results Controller fetchedResultsController.delegate = self return fetchedResultsController }() 方法,我可以响应删除注释对象。

如果我将上面的代码更改为:

self.coreDataManager.managedObjectContext

哪个特定地将托管对象上下文'使用'从使用viewDidLoad更改为应该在后台线程上运行的新版本,在删除项目时永远不会调用委托方法。

所以我的主要问题是 - 在使用此类NSManagedObjectContext时,如何调用Expr returns Expression: Or; Or returns Expression: And (=> ({And.left=current} '||') right=And)*; And returns Expression: Lit (=> ({CmdAnd.left=current} '&&') right=Lit)*; Lit returns Expression: {BoolTrue} 'true' | {BoolFalse} 'false' '(' Expression ')'; 方法?

2 个答案:

答案 0 :(得分:2)

fetchedResultsController正在监视您刚刚创建的上下文,并且该上下文未处理更新。如果您希望更改此上下文,则必须为上下文设置automaticallyMergesChangesFromParent = true

答案 1 :(得分:0)

John告诉NSFetchedResultsController仅监视关联的托管上下文,如

lazy var fetchedResultsController: NSFetchedResultsController = { () -> NSFetchedResultsController<NSFetchRequestResult> in
        let request = NSFetchRequest<NSFetchRequestResult>.init(entityName: "Repository")
        let descriptors = [NSSortDescriptor.init(key: "name", ascending: true), NSSortDescriptor.init(key: "name", ascending: true)]
        request.sortDescriptors = descriptors
        let resultsController = NSFetchedResultsController.init(fetchRequest: request, managedObjectContext: CoreDataManager.shared.managedObjectContext, sectionNameKeyPath: nil, cacheName: nil)
        
        resultsController.delegate = self
        return resultsController
    }()

.privateQueueConcurrencyType 继承 .mainQueueConcurrencyType ,如下面的代码

lazy var managedObjectContext: NSManagedObjectContext = {
        let context = NSManagedObjectContext.init(concurrencyType: .mainQueueConcurrencyType)
        context.parent = self.privateQueueManagedObjectContext
        context.automaticallyMergesChangesFromParent = true
        return context
    }()
    
    lazy var privateQueueManagedObjectContext: NSManagedObjectContext = {
        let context = NSManagedObjectContext.init(concurrencyType: .privateQueueConcurrencyType)
        context.persistentStoreCoordinator = self.persistentStoreCoordinator
        return context
    }()

不要忘记在maincontext中添加 context.automaticallyMergesChangesFromParent = true ,以通知privateQue上下文的后台操作。

我希望这会起作用