NSFetchedResultsController在删除数据时调用delegate,但在插入数据时不调用

时间:2016-03-30 09:02:27

标签: swift core-data icloud nsfetchedresultscontroller

我正在使用Core Data和iCloud开发一个日记应用。核心数据模型的简化视图如下所示:

  

[期刊]< - >> [发布]< - >> [内容]

我使用NSFetchedResultControllers使用'内容'来填充UITableView,并使用' Journals'来填充UICollectionView。从模型。

表视图工作正常,只要添加或删除内容更改,就会调用获取的结果控制器委托。但是,在集合视图控制器中,当添加新日志或其中的帖子或内容发生变化时,获取的结果控制器不会调用委托(我知道最后一部分可能是预期的,因为日志对象本身并没有改变) 。但是,在删除期刊时会调用该委托。

我希望每当插入期刊时都会调用该代表,并且在没有运气或进一步了解情况的情况下与这个问题搏斗数小时。

获取的结果控制器已初始化并配置如下:

// property declaration
lazy var fetchedResultsController: NSFetchedResultsController = self.initialFetchedResultsController()

// initialization
func initialFetchedResultsController() -> NSFetchedResultsController {
    let request = NSFetchRequest(entityName: "Journal")
    let nameSort = NSSortDescriptor(key: "name", ascending: true)
    request.sortDescriptors = [nameSort]
    return return NSFetchedResultsController(fetchRequest: request, managedObjectContext: DataModel.sharedModel().managedObjectContext, sectionNameKeyPath: nil, cacheName: nil)
}

// configuration, called in viewDidLoad()
func configureFetchedResultsController() {
    fetchedResultsController.delegate = collectionViewFetchedResultsControllerDelegate
    do {
        try fetchedResultsController.performFetch()
        print("fetching")
    } catch {
        fatalError("Failed to initialize FetchedResultsController: \(error)")
    }
}

在配置期间传递给fetchedResultsController的collectionViewFetchedResultsControllerDelegate是管理此视图控制器的集合视图的标准NSFetchedResultsControllerDelegate。

当删除数据时,在共享数据模型中调用以下方法,导致fetchedResultsController调用其委托(我只是通过向所有委托方法添加一些调试打印语句来断言)。

func deleteJournal(journal: Journal) {

    // delete all posts attached to journal
    deletePostsForJournal(journal)

    // delete journal
    managedObjectContext.deleteObject(journal)
    saveManagedObjectContext()
}

func deletePostsForJournal(journal: Journal) {

    // delete all content attached to posts of journal
    deleteContentForJournal(journal)

    // delete all posts attached to journal
    performBatchDeleteForJournal(journal, deleteEntityName: "Post", predicateFormat: "journal == %@")
}

func deleteContentForJournal(journal: Journal) {

    // delete all content attached to posts of journal
    performBatchDeleteForJournal(journal, deleteEntityName: "Content", predicateFormat: "post.journal == %@")

    // delete all images attached to posts of journal
    performBatchDeleteForJournal(journal, deleteEntityName: "Image", predicateFormat: "post.journal == %@")
}

private func performBatchDeleteForJournal(journal: Journal, deleteEntityName entityName: String, predicateFormat: String) {

    let fetchRequest = NSFetchRequest(entityName: entityName)
    fetchRequest.predicate = NSPredicate(format: predicateFormat, journal)
    let deleteRequest = NSBatchDeleteRequest(fetchRequest: fetchRequest)

    do {
        try persistentStoreCoordinator.executeRequest(deleteRequest, withContext: managedObjectContext)
    } catch let error as NSError {
        print("Could not execute batch delete fetch request \(error), \(error.userInfo)")
    }

    saveManagedObjectContext()
}

但是,当我通过使用下面的insert方法插入新日志来更新模型时,不会调用fetchedResultsController。

func createJournal(name: String) {

    // creates or returns (if already existing) a journal from the managed object context
    journalForName(name)
    saveManagedObjectContext()
}

func journalForName(name: String) -> Journal {

    // configures fetch request
    let fetchRequest = NSFetchRequest()
    let entityDescription = NSEntityDescription.entityForName("Journal", inManagedObjectContext: managedObjectContext)
    fetchRequest.entity = entityDescription

    // configures predicate to match posts with date
    fetchRequest.predicate = NSPredicate(format: "name == %@", name)

    // executes fetch request
    do {
        let result = try self.managedObjectContext.executeFetchRequest(fetchRequest)

        if let journal = result.first as? Journal {
            return journal
        }

    } catch let error as NSError {
        print("Could not execute fetch request from managedObjectContext \(error), \(error.userInfo)")
    }

    // creates a new journal since one doesn't exist with this name
    let journal = Journal(entity: entityDescription!, insertIntoManagedObjectContext: managedObjectContext)
    journal.name = name
    return journal
}  

如果已经存在具有相同名称的日志,则实现journalForName方法仅创建新日记。

插入工作并在模型中更新,因为如果重新启动应用程序,则会显示创建的日记。我非常感谢你解决这个问题的任何帮助!感谢。

0 个答案:

没有答案