核心数据和关系谓词

时间:2017-02-20 14:57:41

标签: swift core-data relationship predicate

我开始迅速而且几个月前核心数据通常我在这个网站上找到了我的答案,但这是第一次我真的被“关系”和“谓词”所困扰

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

我正在使用CoreData并且我设置了2个实体:“Compte”和“Operation”它们之间的关系是ONE TO MANY(一个compte for MANY operation)

我在哪里:

  • 当用户点击单元格时我正在使用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)
        }
    }
    

在控制台中使用“print”它的工作方式就像我希望依赖于单元格被点击打印(sortedOperationArray)或不显示

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

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

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

谢谢! :)

1 个答案:

答案 0 :(得分: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)
    }

}
相关问题