首页 文章

核心数据和关系谓词

提问于
浏览
1

我几个月前开始快速核心数据通常我在这个网站上找到了我的答案但是我第一次真的陷入“关系”和“谓词”

我已经创建了一个带有tableview的第一个视图控制器,该视图由用户填充,这部分工作正如我所希望的那样 . 用户可以“点击”一个单元格并使用新的tableview打开一个新的视图控制器,我想用这个表视图填充与用户点击的单元格相关的数据 .

我正在使用CoreData,我设置了2个实体:“Compte”和“Operation”它们通过ONE TO MANY关系(一个compte用于MANY操作)

我在哪里:

  • 当用户点击单元格时我正在使用segue将“Compte”发送到第二个视图控制器:

// Segue公司

override func prepare(for segue: UIStoryboardSegue, sender: Any?) {

    let guest = segue.destination as! OperationsViewController
    let indexPath = tableView.indexPathForSelectedRow
    let operation = fetchedResultController.object(at: indexPath!)
    guest.compteTestRelation = operation
}
  • OperationsViewController 我设置了这个变量:
var compteTestRelation: Compte!
  • 用于测试我的数据我创建了一个像这样的FOR LOOP和一个函数:
for index in 1 ... 10 {

    let newOp = Operation(context: context)
    newOp.nom = "Test Compte \(index)"
    newOp.date = NSDate()
    newOp.moyenPaiement = "Test"

    compteTestRelation.addToRelationCompte(newOp) // RelationShip
}

do {
    try context.save()
} catch {
    print(error.localizedDescription)
}
  • 功能
func displayOperation() {

    if let opList = compteTestRelation.relationCompte as? Set<Operation> {

        sortedOperationArray = opList.sorted(by: { (operationA:Operation, operationB:Operation) -> Bool in
            return operationA.date!.compare(operationB.date! as Date) == ComparisonResult.orderedAscending
        })
    print(sortedOperationArray)
    }
}

在带有“打印”的控制台中它工作就像我希望依赖于单元格被点击打印(sortedOperationArray)出现与否

我现在的问题是如何使用这些数据填充我的tableview,当我在我的FetchResultController中使用谓词我有错误或空的tableview但在控制台中一切似乎都工作所以我认为这种关系是正常的..

如果我不使用PREDICATE,我可以使用数据填充我的tableview,但我总是看到所有数据

我在stackoverflow.com上看到过其他类似的问题和答案,但暂时没有任何效果 .

谢谢! :)

1 回答

  • 0

    我找到了另一种方法来预测我的数据,现在它对我有用

    我在我的OPERATION实体中创建了一个名为“id”的新属性,当我创建数据时,我将ID归属于:

    for index in 1 ... 10 {
    
    let newOp = Operation(context: context)
    newOp.nom = "Test Compte \(index)"
    newOp.date = NSDate()
    newOp.moyenPaiement = "Test"
    newOp.id = "id123\(compteTestRelation.nom!)"
    
    compteTestRelation.addToRelationCompte(newOp) // RelationShip
    }
    
    do {
    try context.save()
    } catch {
    print(error.localizedDescription)
    }
    

    然后我在我的FetchResultController中预测我的数据:

    func setupFetchedResultController () {
    
        let operationsRequest: NSFetchRequest<Operation> = Operation.fetchRequest()
        let sortDescriptor = NSSortDescriptor(key: "nom", ascending: true)
        let keyPath = "id"
        let searchString = "id123\(compteTestRelation.nom!)"
        let operationsPredicate = NSPredicate(format: "%K CONTAINS %@", keyPath, searchString)
    
        operationsRequest.returnsObjectsAsFaults = false
        operationsRequest.sortDescriptors = [sortDescriptor]
        operationsRequest.predicate = operationsPredicate
    
    
        fetchedResultController = NSFetchedResultsController(fetchRequest: operationsRequest, managedObjectContext: context, sectionNameKeyPath: nil, cacheName: nil)
    
        do {
            try fetchedResultController.performFetch()
    
        } catch {
            print(error.localizedDescription)
        }
    
    }
    

相关问题