좋아, 지난 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
}
는 참고 용으로 만 사용된다 일부 인쇄 문이 있습니다 여기에
은 일부 코드입니다! 때문에 섹션/numberOfRows이 잘못 사실 것 같아요 -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
}
그래서, 나는 데이터 만 항상 하나 개의 항목 만이 아닌 전체 집합을 얻을 수 있어요. 여기
내 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 또는 술어 가정입니다 - 아니면 둘 다?
어떤 도움이나 지시 사항이든 잘 부탁드립니다. 나는 여러 가지 접근법을 이미 시도했지만 아무도 정확한 결과를 제공하지 못했습니다.
let predicate = NSPredicate(format: "accounts.aId = %@", ACCOUNTID)
또는
let predicate = NSPredicate(format: "accounts = %@", account.objectID)
I :
대부분 실패 메시지는 "keypath for (category, title ...)는 존재하지 않습니다"위의 코드에서 술어는 작동하는 것처럼 보이지만 SortDescriptor가 나에게 가장 많은 문제를주고 있습니다 ... 내 추측 –