0

好的,我在過去的1-2週中一直在尋找並嘗試這種情況,但我沒有得到它的工作。我可以在沒有NSFRC的情況下達到我想要的水平,但是出於性能原因和快速考慮,我希望能夠與NSFRC一起完成。 所以,我有2個實體的DataModel - 看到圖片 to-many RelationshipNSFetchedResultsController和多對多關係無法正常工作

有一個帳戶,一個帳戶可以有多個accountchanges - 這是相當明顯的。 所以我希望能夠選擇一個帳戶,然後顯示該特定帳戶的所有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 
} 

所以,我能夠獲得數據,但始終只有一個項目,而不是整個集 - 我想由於該段/ numberOfRows是錯誤的。

這裏是我的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或謂語 - 或者兩者兼而有之?

任何幫助或至少指示,非常感謝。 我已經嘗試了很多不同的方法,但都沒有給我正確的結果。

+0

還有一個評論,我試過很多不同的路徑在SortDescriptor和謂語/或NSFRC的sectionNameKeyPath - 最失敗的消息是:「對許多關鍵在這裏不允許」或「(類別,標題...)的keypath不存在」從上面的代碼,謂詞似乎工作,但SortDescriptor給我最多的問題......我猜 –

回答

1

我會做相反的,我的意思是使用FRC來獲取所有與某個ID帳戶的變化,並使用以下斷言:

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

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

我會重新將Accounts實體重命名爲Account,因爲這是一對一的關係,所以將關係重命名爲Account。 這就是假設你有一個包含所有帳戶的表格視圖,並且當你點擊一個表格時,它會讓你回到它的更改。

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

乾杯

+0

我也想過,但我真的不知道不需要任何關係,因爲我可以將帳戶ID傳遞給每個帳戶更改。但既然我們可以選擇使用關係,我想這樣做。無論如何,如果沒有其他解決方案,我可能會這樣做,並重構。我已經在我的數據庫中擁有一個唯一的ID,我可以這樣做,但是再次,它可以很好地與關係和NSFRC一起工作。 感謝您的建議... –

+0

我的建議仍然涉及關係和NSFRC。你只需要反過來。除了獲取帳戶並嘗試訪問帳戶中的更改外,還可以使用您傳遞給謂詞的帳戶獲取帳戶的所有更改。 – ubiAle

+0

這不起作用,它只是帳戶更改帳戶的一對一 - 無法更改,因爲一個帳戶更改只能影響一個帳戶。從賬戶到賬戶變化只有一對多 - 見圖。正如我上面寫的,謂詞不是問題,它是SortDescriptor或NSFRC中的keyPath。我得到了正確的帳戶,但更改的次數在章節/ numberOfRows中是錯誤的......如前所述,我可以在沒有NSFRC的情況下執行所有操作 - 所以問題不在於獲取本身,而是使用NSFRC因爲你從rel中獲得一個NSSet。 –

相關問題