快速获取一对多关系的核心数据 [英] fetch coredata with one to many relationship in swift
问题描述
我有一个coredata实体的tableview(table1)显示一行用户,另一个tableview(table2)显示了另一个与(table1)实体以一对多关系连接的实体,当(table2)出现时点击(table1)选定的行,我的问题是如何在(table1)的选定行中为用户显示(table2)上的数据
I have a tableview(table1) of a coredata entity that shows a row of users, and another tableview(table2) that shows another entity that connected with (table1) entity in one to many relationship the (table2)shows up when tap in (table1) selected row, my question is how to show the data on (table2) for the user in selected row from (table1)
示例代码:
let fetchdata:NSFetchRequest<entity2> = entity2.fetchRequest()
do {
userList = try myContext.fetch(fetchdata) [of entity1 object]
}catch{
print(error)
}
我已经将(table1)对象作为nsobject转移到了(table2).有什么方法可以正确地完成我的功能
I already transferred the (table1) object to (table2) as nsobject . is there any way to do my function in the right way
推荐答案
我通过使用 NSPredicate
通过使用以下代码行将来自entry1的entity2数据与该属性对象相连接,解决了该问题:/p>
I solved the problem by using the NSPredicate
to connect the entity2 data with that attribute object from entity1 by using this line of code
let myFetch:NSFetchRequest<entity2> = entity2.fetchRequest()
let myPredicate = NSPredicate(format: "toEntity1-relationship == %@", (myTransferdObject?.name!)!)
myFetch.predicate = myPredicate
do {
usersList = try myContext.fetch(myFetch)
}catch{
print(error)
}
这篇关于快速获取一对多关系的核心数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!