如何根据与另一个实体的关系从一个实体获取行?

时间:2013-10-11 21:43:53

标签: ios core-data nspredicate

我有两个实体: 跟踪:      createdDate

位置:      高度      ...(“位置”中的另外7个属性)

Track有一个属性'createdDate',其中To-Many为Location。

我需要选择Track.createdDate ==“whatever-date-i-want”的所有位置 (在SQL中它是一个连接,很容易做到。) 在这里,我不知道该怎么做......

Location实体没有'createdDate'属性,因为我相信foreignKeys是由CoreData本身处理的。如果我查看sqlite文件,数据将完美无缺,我可以看到每个轨道的一堆位置(因为GPS将它们放入CD中)

我猜这是做谓词的一种特殊方式。我尝试了以下这一行无济于事:

NSPredicate *predicate = [NSPredicate predicateWithFormat: @"(Track.createdDate == %@)", currentTrack.createdDate];

但它不起作用。我也试过把房子放在那里:

@property (nonatomic,retain) Track *lookupTrack;

并尝试:

NSPredicate *predicate = [NSPredicate predicateWithFormat: @"(lookupTrack.createdDate == %@)", currentTrack.createdDate];

也无济于事......想法?感谢

编辑: 刚才我试过了:

NSPredicate * predicate = [NSPredicate predicateWithFormat:@“(ANY track.createdDate ==%@)”,currentTrack.createdDate];

给了我错误'keypath createdDate not found in entity'

NSPredicate * predicate = [NSPredicate predicateWithFormat:@“(ALL track.createdDate ==%@)”,currentTrack.createdDate];

给了我错误'Unsupported predicate(null)'

编辑: 这里有一些截图 enter image description here

enter image description here

2 个答案:

答案 0 :(得分:1)

你必须在“位置”到“跟踪”中定义“反向关系” 核心数据模型检查员。

  • 定义从“位置”到“跟踪”的一对一关系,称之为“跟踪”。
  • 将“track”定义为与“Track”的to-many关系成反比关系 到“位置”。

那么谓词就是

[NSPredicate predicateWithFormat: @"track.createdDate == %@", currentTrack.createdDate]

答案 1 :(得分:0)

我道歉......这是我的......

我有

NSSortDescriptor *sortDescriptor = [[NSSortDescriptor alloc] initWithKey:@"createdDate" ascending:NO];

应该在哪里

NSSortDescriptor *sortDescriptor = [[NSSortDescriptor alloc] initWithKey:@"track.createdDate" ascending:NO];
相关问题