6

私はコアデータを学んでおり、特に集計に取り組んでいます。

現在私がやりたいこと:いくつかの基準で逆関係と対多関係にあるテーブルからのレコードの数を数えます

現在、私はこれをやっています:

NSExpression *ex = [NSExpression expressionForFunction:@"count:" 
                                                 arguments:[NSArray arrayWithObject:[NSExpression expressionForKeyPath:@"ddname"]]];
    NSPredicate *pred = [NSPredicate predicateWithFormat:@"ddtype == 'Home'"];
    NSExpressionDescription *ed = [[NSExpressionDescription alloc] init];
    [ed setName:@"countDDEvents"];
    [ed setExpression:ex];
    [ed setExpressionResultType:NSInteger16AttributeType];
    NSArray *properties = [NSArray arrayWithObject:ed];
    NSFetchRequest *request = [[NSFetchRequest alloc] init];
    [request setPredicate:pred];
    [request setPropertiesToFetch:properties];
    [request setResultType:NSDictionaryResultType];
    NSEntityDescription *entity = [NSEntityDescription entityForName:@"DDEvent" inManagedObjectContext:[self.currentAccount managedObjectContext]];
    [request setEntity:entity];
    NSArray *results = [[self.currentAccount managedObjectContext] executeFetchRequest:request error:nil]; 
    NSDictionary *dict = [results objectAtIndex:0];
    NSLog(@"Average birthdate for female heroes: %@", [dict objectForKey:@"countDDEvents"]);

ジェフ・レマルシェからです。

編集:そして私は私の解決策を次のように見つけました

    NSFetchRequest *request = [[NSFetchRequest alloc] init];
    NSPredicate *pred = [NSPredicate predicateWithFormat:@"ddtype == 'Home'"];
    [request setPredicate:pred];

    NSEntityDescription *entity = [NSEntityDescription entityForName:@"DDEvent" inManagedObjectContext:[self.currentAccount managedObjectContext]];
    [request setEntity:entity];

    NSError *error = nil;
    NSUInteger count = [[self.currentAccount managedObjectContext] countForFetchRequest:request error:&error];

うまくいっていますが、こういうタイプのリクエストを一度にもっとや​​りたいです。したがって、これはカウントを取得する好ましい方法ではないと思います。

編集

だから私はアプローチが適切なものになると思います????

それで、誰でもこれを行うためのより効率的な方法を教えてもらえますか。

ありがとう 。

4

2 に答える 2

5

約 10,000 個のエンティティをカウントする必要があり、countForFetchRequest を使用している間、インターフェイスの応答性が大幅に低下しました。

NSExpression でそれを行う方法は次のとおりです。

- (NSUInteger) unfilteredFCsCount {

// Just the fetchRequest
    NSFetchRequest *fetchRequest = [self unfilteredFCsFetchRequest];

    [fetchRequest setResultType: NSDictionaryResultType];

// You can use any attribute of the entity. its important, because you are not counting 
// the properties, but actually the entities
    NSExpression *keyPathExpression = [NSExpression expressionForKeyPath: @"sortIndex_"]; // Does not really matter
    NSExpression *maxExpression = [NSExpression expressionForFunction: @"count:" 
                                                            arguments: [NSArray arrayWithObject:keyPathExpression]];
    NSExpressionDescription *expressionDescription = [[NSExpressionDescription alloc] init];
    [expressionDescription setName: @"fcCount"];
    [expressionDescription setExpression: maxExpression];
    [expressionDescription setExpressionResultType: NSInteger32AttributeType];

    [fetchRequest setPropertiesToFetch: [NSArray arrayWithObject:expressionDescription]];

    NSUInteger fcCount = 0;
    NSError *error = nil;
    NSArray *results = nil;
    results = [self.managedObjectContext executeFetchRequest: fetchRequest error: &error];
    KSLog(KSLogLevelDebug, @"unfilteredFCsCount results: %@", results);

    if([results count] > 0) {
        NSNumber *count = [[results objectAtIndex: 0] objectForKey: @"fcCount"];
        fcCount = [count intValue];
    }

    return fcCount;
}
于 2011-05-13T08:01:16.567 に答える
4

Jeff LaMarche はこれを単純な例として使用しています。実際には、この必要性は非常に一般的であるため、Key-Value Coding にはそれを処理する組み込みのマクロと、その他の一般的なコレクション操作があります。

参照:キー値プログラミング ガイド: セットおよび配列演算子

この場合@count、述語で演算子を使用します。

もちろん、独自の式を手動で調整すると、述語を細かく制御できますが、演算子はそのようなタスクの 80% を処理します。

于 2010-07-27T12:00:23.203 に答える