Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/objective-c/23.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ios CoreData:使用NSFetchedResultController获取多个实体_Ios_Objective C_Core Data_Swift_Nsfetchedresultscontroller - Fatal编程技术网

Ios CoreData:使用NSFetchedResultController获取多个实体

Ios CoreData:使用NSFetchedResultController获取多个实体,ios,objective-c,core-data,swift,nsfetchedresultscontroller,Ios,Objective C,Core Data,Swift,Nsfetchedresultscontroller,我可以使用NSFetchedResultsController获取一个实体并在具有单个节的tableview中显示数据。但是如何使用NSFetchedResultsController获取两个或多个实体(无关系),并在不同的部分显示数据 我注意到,使用核心数据,我可以使用NSFetchedResultsController获得节数和行数。我在NSFetchedResultsController中设置了一个实体,它可以很好地处理tableView,包括添加和删除行 func getFetched

我可以使用
NSFetchedResultsController
获取一个实体并在具有单个节的tableview中显示数据。但是如何使用
NSFetchedResultsController
获取两个或多个实体(无关系),并在不同的部分显示数据

我注意到,使用核心数据,我可以使用
NSFetchedResultsController
获得节数和行数。我在
NSFetchedResultsController
中设置了一个实体,它可以很好地处理
tableView
,包括添加和删除行

func getFetchedResultController()->NSFetchedResultsController{
fetchedResultController=NSFetchedResultsController(fetchRequest:fetchRequest(),managedObjectContext:managedObjectContext!,sectionNameKeyPath:nil,cacheName:nil)
fetchedResultController.delegate=self
fetchedResultController.performFetch(无)
返回fetchedResultController
}
func fetchRequest()->NSFetchRequest{
let fetchRequest=NSFetchRequest(entityName:“水果”)
让sortDescriptor=NSSortDescriptor(键:“desc”,升序:true)
fetchRequest.sortDescriptors=[sortDescriptor]
返回请求
}
重写func numberOfSectionsInTableView(tableView:UITableView)->Int{
让section=fetchedResultController.sections?.count
返回区!
}
重写func tableView(tableView:UITableView,numberofrowsinssection:Int)->Int{
如果让s=fetchedResultController.sections为?[NSFetchedResultsSectionInfo]{
返回s[section].numberOfObjects
}
返回0
}
重写func tableView(tableView:UITableView,cellForRowAtIndexPath:nsindepath)->UITableView单元格{
var cell=tableView.dequeueReusableCellWithIdentifier(“cell”,forIndexPath:indexPath)作为UITableViewCell
让fruit=fetchedResultController.objectAtIndexPath(indexPath)作为结果
cell.textlab.text=fruit.desc
返回单元

}
使用两个
获取的ResultsController
,说
水果FRC
饮料FRC
,然后将这些FRC报告的
索引XPath
映射到tableView使用的
索引XPath
。例如,如果水果将出现在tableView的第0节中,您可以使用tableView
indexPath
fruitFRC
中查找水果详细信息。但是对于饮料,在tableView的第1节中,您需要创建一个新的
indexPath
,因为
drinksFRC
将使用第0节。(您可以使用
nsindepath
indexPathForRow:instation:
方法来创建新的indepath。)

同样,在
numberOfRowsInSection
中,如果
部分
为0,则返回
水果FRC
中的对象数,如果
部分
为1,则返回
饮料FRC
中的对象数


以此类推……

我找到了另一种方法,我读到了,但是如果NSFetchedResultController可以做同样的事情,我愿意尝试。是否可能以及如何实现?有关信息:如果您刚刚开始项目,请尝试领域:。Realm非常易于使用,并抽象了大量繁重的代码。相同性能的代码更少。;)王国太棒了!谢谢,这意味着我无法使用一个NSFetchedResultController获取多个实体来简化代码?顺便说一句,谢谢你的回答。基本上,这是正确的-fetch或fetchedResultController将只检索一种实体类型。但是,如果水果和饮料实体有许多共同的属性,我想您可以将它们定义为单个父实体(“食物”)的独立子实体,并获取父实体。但这可能比两个FRC解决方案更复杂。