2017-02-20 116 views
1

我已經開始迅速&核心數據一個月前幾個平時我發現我的答案這個網站上,但對於第一次我真的堅持「關係」和「謂詞」核心數據和關係謂詞

我已經創建了第一個視圖控制器與用戶填充的tableview,這部分工作就像我希望的。 用戶可以「點擊」一個單元格,並用新的tableview打開一個新的視圖控制器,並且我想用與用戶點擊的單元格有關的數據填充這個tableview。

我使用CoreData和我設置了2個實體:「孔特」和「操作」他們是在通過關係一對多(ONE孔特對許多操作)

這裏我在哪裏:

  • 當用戶點擊我使用SEGUE發送 「孔特」 到第二視圖控制器單元:

    // 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循環像這樣和一個功能:

    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) 
        } 
    } 
    

在控制檯e與「打印」它工作就像我希望取決於細胞被挖掘打印(sortedOperationArray)出現與否

我現在的問題是如何填充我的tableview與這些數據,當我在我的FetchResultController中使用謂詞錯誤或一個空的tableview,但在控制檯中一切似乎工作,所以我認爲這種關係是OK ..

如果我不使用PREDICATE我可以填充我的tableview與數據,但我總是看到所有的數據

我已經在stackoverflow.com上看到其他類似的問題和解答,但暫時沒有任何工作。

謝謝! :)

回答

0

我找到了另一種方式來斷言我的數據,現在對我的作品

我已經創建我的操作實體,一個新的屬性叫做「身份證」,當我創建我的數據我屬性一個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) 
    } 

}