我有一堆帐户对象,它们与“度量”有一对多的关系……Account.measures<->measure.measureaccount.measure帐户
我试图为任何特定的帐户返回不同的度量类型-在核心数据模型中指定为String:

但是我得到了所有单独的度量类型:
- (NSArray *)getDistinctMeasuresForAccount: (BM_Account *)account
{
NSManagedObjectContext *myContext=[self managedObjectContext];
NSEntityDescription *entity=[NSEntityDescription entityForName:@"BM_Measure" inManagedObjectContext:myContext];
[findObjects setEntity:entity];
NSDictionary *entityProperties = [entity propertiesByName];
[findObjects setPropertiesToFetch:[NSArray arrayWithObject:[entityProperties objectForKey:@"measuretype"]]];
[findObjects setReturnsDistinctResults:TRUE];
findObjects.fetchBatchSize=1;
findObjects.fetchLimit=0;
NSPredicate *myObjectPredicate = [NSPredicate predicateWithFormat:@"measureAccount = %@",account];
findObjects.predicate=myObjectPredicate;
findObjects.resultType = NSDictionaryResultType;
NSSortDescriptor *sortReturnedObjects=[NSSortDescriptor sortDescriptorWithKey:@"measuretype" ascending:FALSE];
findObjects.sortDescriptors = [NSArray arrayWithObjects:sortReturnedObjects, nil];
NSError *findError;
NSArray *databaseObjects=[myContext executeFetchRequest:findObjects error:&findError];
if(databaseObjects==nil){
NSLog(@"BM.DB.getDistinctMeasuresForAccount: Unable to execute query to get Objects");
return nil;
}
else
{
NSLog(@"About to return %ld %@",[databaseObjects count],databaseObjects);
return(databaseObjects);
}
NSLog(@"BM.DB.getDistinctMeasuresForAccount: Returning zero because of open query");
return 0;
}输出:
About to return 347 (
{
measuretype = Weight;
},
{
measuretype = Weight;
},
{
measuretype = Weight;
},
{
measuretype = Weight;
},
{
measuretype = Weight;
},
{
measuretype = Weight;
},
{
measuretype = Weight;
},
{
measuretype = Weight;
},
{
measuretype = Weight;
},
{
measuretype = Weight;
},
{
measuretype = Weight;
},
{
measuretype = Weight;
},
{
measuretype = Weight;
},
{
measuretype = Weight;
},
{
measuretype = Weight;
},
...发布于 2013-11-03 21:00:55
事实证明,最新版本的Xcode中的默认构造函数创建了一个xml数据库-因此不支持distinct。
https://stackoverflow.com/questions/19715619
复制相似问题