我有一堆与“度量”有一对多关系的帐户对象... account.measures<--->measure.measureaccount
我正在尝试为任何特定帐户返回不同的度量类型 - 在核心数据模型中指定为字符串:
但我得到了所有单独的度量类型:
- (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;
},
...