0

我有一堆与“度量”有一对多关系的帐户对象... 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;
    },
...
4

1 回答 1

0

事实证明,最新版本的 Xcode 中的默认构造函数创建了一个 xml 数据库——因此不支持 distinct。

于 2013-11-03T13:00:55.327 回答