我试着找到我的简单问题的答案,但没有运气...我在CoreData上工作,我有两个实体,让我们以“照片和摄影师”一对多关系为例,这意味着一个摄影师可以有多张照片……现在,我确实使用
[NSEntityDescription insertNewObjectForEntityForName...]
我在检索某个特定摄影师的“所有照片”时遇到了问题。我正在使用这段代码
NSFetchRequest *fetchRequest = [[NSFetchRequest alloc] initWithEntityName:@"Photographer"];
fetchRequest.predicate = [NSPredicate predicateWithFormat:@"name = %@",@"photographerName"];
fetchRequest.sortDescriptors = @[[NSSortDescriptor sortDescriptorWithKey:@"timeStamp" ascending:YES]];
这个代码是返回摄影师,但我只需要所有的照片由这个摄影师拍摄。我是CoreData新手,我不确定是否应该使用“照片”实体?
提前谢谢。
发布于 2014-01-25 10:08:19
您的获取请求应请求Photo
实体。
然后,在您的谓词中,您可以像这样指定摄影师的名称:
fetchRequest.predicate = [NSPredicate predicateWithFormat:@"photographer.name = %@",@"photographerName"];
或者更好的方法是,先获取摄影师对象,然后在谓词中测试关系,这样会更快:
Photographer* photographer = ... //Get required photographer here
fetchRequest.predicate = [NSPredicate predicateWithFormat:@"photographer = %@", photographer];
发布于 2014-01-26 07:05:47
使用此选项对photographer.photos
数组进行排序。
NSSortDescriptor *sortKey = [[NSSortDescriptor alloc] initWithKey:@"timestamp" ascending:YES];
NSArray *sorters = [NSArray arrayWithObject:sortKey];
NSArray *sortedArray = [photographer.photos sortedArrayUsingDescriptors:sorters];
https://stackoverflow.com/questions/21345640
复制相似问题