NSFetchedResultsController 和对多关系不起作用

NSFetchedResultsController and to-many relationship not working

好的,在过去的 1-2 周里,我一直在搜索和尝试这种情况,但没有成功。如果没有 NSFRC,我将能够实现我想要的,但出于性能原因和便利性,我想使用 NSFRC 来实现。 所以,我有一个包含 2 个实体的 DataModel - 参见图片

一个帐户,一个帐户可以有多个帐户更改 - 这是很明显的。 所以我希望能够选择一个帐户,然后显示该特定帐户的所有 AccountChanges。 到目前为止,我能够获得帐户并在 cellForRow 函数中访问 NSSet,但我没有获得正确的部分和 numberOfRowsInSection - 这是主要问题。

这是一些代码:

    func numberOfSections(in tableView: UITableView) -> Int {

    print("Sections : \(self.fetchedResultsController.sections?.count)")
    if (self.fetchedResultsController.sections?.count)! <= 0 {
        print("There are no objects in the core data - do something else !!!")
    }
    return self.fetchedResultsController.sections?.count ?? 0
}

func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
    print("Section Name")
    print(self.fetchedResultsController.sections![section].name)
    let sectionInfo = self.fetchedResultsController.sections![section]
    print("Section: \(sectionInfo) - Sections Objects: \(sectionInfo.numberOfObjects)")
    return sectionInfo.numberOfObjects
}

部分打印语句仅供参考!

    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {

    let myCell = myTable.dequeueReusableCell(withIdentifier: "myCell")! as UITableViewCell
    let accountBalanceChanges = self.fetchedResultsController.object(at: indexPath)
    print("AccountBalanceChanges from cell....")
    print(accountBalanceChanges)

    let details = accountBalanceChanges.accountchanges! as NSSet
    print("Print out the details:")
    print(details)
    let detailSet = details.allObjects
    let detailSetItem = detailSet.count // Just for information!


    let myPrint = detailSet[indexPath.row] as! AccountChanges
    let myVal = myPrint.category

    myCell.textLabel?.text = myVal

    return myCell
}

因此,我能够获取数据,但始终只有一项而不是整组 - 我猜是因为部分/行数错误。

这是我的NSFRC

    var fetchedResultsController: NSFetchedResultsController<Accounts> {
    if _fetchedResultsController != nil {
        return _fetchedResultsController!
    }
    let fetchRequest: NSFetchRequest<Accounts> = Accounts.fetchRequest()
    // Set the batch size to a suitable number.
    fetchRequest.fetchBatchSize = 20
    // Edit the sort key as appropriate.

    let sortDescriptor = NSSortDescriptor(key: "aName", ascending: false)
    fetchRequest.sortDescriptors = [sortDescriptor]


    let predicate = NSPredicate(format: "(ANY accountchanges.accounts = %@)", newAccount!)
    fetchRequest.predicate = predicate
    // Edit the section name key path and cache name if appropriate.
    // nil for section name key path means "no sections".

    let aFetchedResultsController = NSFetchedResultsController(fetchRequest: fetchRequest, managedObjectContext: self.coreDataStack.context, sectionNameKeyPath: nil, cacheName: nil)
    aFetchedResultsController.delegate = self
    _fetchedResultsController = aFetchedResultsController

    do {
        try _fetchedResultsController!.performFetch()
    } catch {
        // Replace this implementation with code to handle the error appropriately.
        // fatalError() 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.
        let nserror = error as NSError
        fatalError("Unresolved error \(nserror), \(nserror.userInfo)")
    }

    return _fetchedResultsController!
}

我假设它是 SortDescriptor 或谓词 - 或者两者都有?

非常感谢任何帮助或至少指导。 我已经尝试了许多不同的方法,但 none 给了我正确的结果。

我会做相反的事情,我的意思是使用 FRC 获取具有特定 Id 的帐户的所有更改,并使用以下谓词:

let predicate = NSPredicate(format: "accounts.aId = %@", ACCOUNTID)

let predicate = NSPredicate(format: "accounts = %@", account.objectID)

我会将 Accounts 实体重命名为 Account,对于这种关系也是如此,因为它是一对一的关系。 这是假设您有一个包含所有帐户的 table 视图,当您单击一个帐户时它会返回它的更改。

 var fetchedResultsController: NSFetchedResultsController<AccountChanges> {
    if _fetchedResultsController != nil {
        return _fetchedResultsController!
    }
    let fetchRequest: NSFetchRequest<AccountChanges> = AccountChanges.fetchRequest()
    // Set the batch size to a suitable number.
    fetchRequest.fetchBatchSize = 20
    // Edit the sort key as appropriate.

    let sortDescriptor = NSSortDescriptor(key: "aName", ascending: false)
    fetchRequest.sortDescriptors = [sortDescriptor]


    let predicate = NSPredicate(format: "accounts.aId = %@", ACCOUNTID)
    fetchRequest.predicate = predicate
    // Edit the section name key path and cache name if appropriate.
    // nil for section name key path means "no sections".

    let aFetchedResultsController = NSFetchedResultsController(fetchRequest: fetchRequest, managedObjectContext: self.coreDataStack.context, sectionNameKeyPath: nil, cacheName: nil)
    aFetchedResultsController.delegate = self
    _fetchedResultsController = aFetchedResultsController

    do {
        try _fetchedResultsController!.performFetch()
    } catch {
        // Replace this implementation with code to handle the error appropriately.
        // fatalError() 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.
        let nserror = error as NSError
        fatalError("Unresolved error \(nserror), \(nserror.userInfo)")
    }

    return _fetchedResultsController!
}

干杯