2010-07-27 12 views
6

मैं कोर डेटा सीख रहा हूं और विशेष रूप से एकत्रीकरण पर काम कर रहा हूं।कोर्डटा (एकत्रीकरण) में कैसे गिनना है?

वर्तमान मैं जो करना चाहता हूं: कुछ मानदंडों पर व्यस्त संबंधों के साथ-साथ कई रिश्तों में मौजूद रिकॉर्ड्स की संख्या की गणना करें। जेफ lemarche से

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

जेफ लामारे बस इसे एक साधारण उदाहरण के रूप में उपयोग कर रहा है। व्यावहारिक रूप से, यह आवश्यकता इतनी आम है कि कुंजी-मूल्य कोडिंग मैक्रो में इसे और अन्य सामान्य संग्रह संचालन को संभालने के लिए बनाया गया है।

देखें: The Key-Value Programming Guide: Set and Array Operators

इस मामले में आप अपने विधेय में @count ऑपरेटर का प्रयोग करेंगे।

बेशक, हाथ अपनी अभिव्यक्ति को ट्यून करने से आपको अपने भविष्यवाणियों पर अच्छा नियंत्रण मिल जाता है लेकिन ऑपरेटर 80% ऐसे कार्य को संभालते हैं।

5

मैं के बारे में 10 000 संस्थाओं गिनती करने के लिए था और यह धीमा मेरी इंटरफ़ेस एक बहुत जवाबदेही, जबकि यह countForFetchRequest के साथ क्या कर ..

यहाँ NSExpression wth यह करने का एक तरीका है:

- (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; 
} 
संबंधित मुद्दे