mbo*_*ogh 1 iphone core-data objective-c
在我的数据模型中,我有两个实体:
Category和Item.
类别
attr:name
rel:parent(Destination:Category,Inverse:subCategories)
rel:subCategories(Destination:Category,Inverse:parent,To-Many Relationship)
rel:items(Destination:Item,Inverse:category,To-Many Relationship)
Item
attr:title
rel:category(Destination:Category,Inverse:items,To-Many Relationship)
得到以下TVC:Root,Category和Item
Root显示所有没有父
类别的Category对象Category显示具有特定父对象的所有Category对象
Item显示具有特定类别的所有Item对象
示例数据:
Top Cat 1 - Sub cat 1 - Item 1 - Item 2 - Item 3 - Item 4 - Sub cat 2 - Item 1 - Sub cat 3 - Item 1 - Sub cat 4 - Item 1 - Item 2 - Sub cat 5 Top Cat 2 - Sub cat 1 - Item 1 - Item 2 - Item 3 - Sub cat 2 - Item 1 - Sub cat 3 - Item 1
那么如何在Root TVC中获得每个"Top Cat"的项目总数?
如果我想知道"子猫"的数量,我可以使用以下keyPath:"subCategories.@ count",但像"subCategories.items.@ count"这样的东西不起作用.
希望您能够帮助我.
干杯 - 我
有三种方法可以计算:
触发相关顶级类别的获取请求,并从该对象获取其子类别关系集中的实体计数.这种方法的缺点是它将导致CoreData从商店获取关系属性,获取与该关系相关联的每个对象.例如
NSManagedObject *topCat = [context performFetch:topCatFetchRequest];
NSInteger count = topCat.subCategories.count;
使用原始SQLiteAPI直接查询数据存储(请注意,这要快得多).
NSManagedObjectContext countForFetchRequest:,它将返回与给定获取请求匹配的对象数.| 归档时间: |
|
| 查看次数: |
1635 次 |
| 最近记录: |