我有一个实体“SomeEntity”具有属性:name,type,rank,foo1,foo2.
现在,如果我们严格地用SQL语句说话,SomeEntity有几行.我想要完成的只是检索可用的类型,即使每个实例都可以有重复的类型.我也需要他们根据排名按顺序返回.所以在sql中,我正在寻找的是:
SELECT disTINCT(type)ORDER BY rank ASC
这是迄今为止我已经破解的代码:
NSError *error = NulL;NSFetchRequest *fetchRequest = [[NSFetchRequest alloc] init];[fetchRequest setReturnsdistinctResults:YES];[fetchRequest setPropertIEsToFetch:[NSArray arrayWithObjects:@"type",@"rank",nil]];NSEntityDescription *entity = [NSEntityDescription entityForname:@"SomeEntity" inManagedobjectContext:managedobjectContext];[fetchRequest setEntity:entity];// sort by rankNSSortDescriptor *rankDescriptor = [[NSSortDescriptor alloc] initWithKey:@"rank" ascending:YES];NSArray *sortDescriptors = [[NSArray alloc] initWithObjects:rankDescriptor,nil];[fetchRequest setSortDescriptors:sortDescriptors];[sortDescriptors release];[rankDescriptor release];NSArray *fetchResults = [managedobjectContext executeFetchRequest:fetchRequest error:&error];[fetchRequest release];return fetchResults;
现在这是崩溃与以下:无效的keypath部分传递给setPropertIEsToFetch:
解决方法NSManagedobjectContext * ctx ; /* some ctx */ NSFetchRequest * req; /* your request */NSEntityDescription *entity = [NSEntityDescription entityForname:@"SomeEntity" inManagedobjectContext:context];NSDictionary *entityPropertIEs = [entity propertIEsByname];[req setEntity:entity];[req setReturnsdistinctResults:YES];[req setPropertIEsToFetch:[NSArray arrayWithObject:[entityPropertIEs objectForKey:@"type"]]];[req setSortDescriptors:[NSArray arrayWithObject:[[NSSortDescriptor alloc] initWithKey:@"rank" ascending:YES]]];NSArray * result = [ctx executeFetchRequest:req error:nil];总结
以上是内存溢出为你收集整理的iphone – 使用Core Data检索唯一的结果集全部内容,希望文章能够帮你解决iphone – 使用Core Data检索唯一的结果集所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)