好的,我在過去的1-2週中一直在尋找並嘗試這種情況,但我沒有得到它的工作。我可以在沒有NSFRC的情況下達到我想要的水平,但是出於性能原因和快速考慮,我希望能夠與NSFRC一起完成。 所以,我有2個實體的DataModel - 看到圖片 NSFetchedResultsController和多對多關係無法正常工作
有一個帳戶,一個帳戶可以有多個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或謂語 - 或者兩者兼而有之?
任何幫助或至少指示,非常感謝。 我已經嘗試了很多不同的方法,但都沒有給我正確的結果。
還有一個評論,我試過很多不同的路徑在SortDescriptor和謂語/或NSFRC的sectionNameKeyPath - 最失敗的消息是:「對許多關鍵在這裏不允許」或「(類別,標題...)的keypath不存在」從上面的代碼,謂詞似乎工作,但SortDescriptor給我最多的問題......我猜 –