NSExpression Example


The relatively new NSExpression training is very highly effective, yet not really used very often. Aspect of that is that it's not very well reported. Although the API records for NSExpression is pretty well specific, the outlined "companion guide" (Introduction to Predicates Programming) has very little details about how to actually use NSExpression.

NSExpression is deserving of to be better reported, because it provides to predicate computer encoding (including Primary Data), a lot of functions from the relational details source community that individuals often grumble are losing, like unioning, intersecting, and subtracting resultsets and accomplishing mixture functions without launching maintained physical objects or problems into recollection space.

The aggregates features is especially essential on iOS given the restricted recollection space on most iOS gadgets. If you've got a big dataset, and you want to get a matter of physical objects, or determine a typical or sum for one of the features, you really don't want to have to take the complete dataset into recollection space. Even if they're just problems, they're going to eat up recollection space you don't need to use because the hidden SQLite prolonged keep can determine that products out without the subject cost.

I don't have a chance to do a complete NSExpression article, but I imagined it at least truly worth publishing a classification on NSManagedObject that allows you take benefits of some of its more useful functions.

With this classification, to get a sum of the credit bar on business Foo, you would do this:

I don't have time to do a full NSExpression tutorial, but I thought it at least worth posting a category on NSManagedObject that lets you take advantage of some of its more useful features.

With this category, to get a sum of the attribute bar on entity Foo, you would do this:
NSNumber *fooSum = [Foo aggregateOperation:@"sum:" onAttribute:@"bar" withPredicate:nil inManagedObjectContext:context];



This will calculate it for you using the database features, NOT by loading all the managed objects into memory. Much more memory and processor efficient than doing it manually.

Cheers. Category follows:

Header File:
@interface NSManagedObject(MCAggregate)
+(NSNumber *)aggregateOperation:(NSString *)function onAttribute:(NSString *)attributeName withPredicate:(NSPredicate *)predicate inManagedObjectContext:(NSManagedObjectContext *)context
@end



Implementation File:
+(NSNumber *)aggregateOperation:(NSString *)function onAttribute:(NSString *)attributeName withPredicate:(NSPredicate *)predicate inManagedObjectContext:(NSManagedObjectContext *)context
{
    NSExpression *ex = [NSExpression expressionForFunction:function 
        arguments:[NSArray arrayWithObject:[NSExpression expressionForKeyPath:attributeName]]];

    NSExpressionDescription *ed = [[NSExpressionDescription alloc] init];
    [ed setName:@"result"];
    [ed setExpression:ex];
    [ed setExpressionResultType:NSInteger64AttributeType];

    NSArray *properties = [NSArray arrayWithObject:ed];
    [ed release];

    NSFetchRequest *request = [[NSFetchRequest alloc] init];
    [request setPropertiesToFetch:properties];
    [request setResultType:NSDictionaryResultType];

    if (predicate != nil)
        [request setPredicate:predicate];

    NSEntityDescription *entity = [NSEntityDescription entityForName:[self className] 
        inManagedObjectContext:context];
    [request setEntity:entity];

    NSArray *results = [context executeFetchRequest:request error:nil];
    NSDictionary *resultsDictionary = [results objectAtIndex:0];
    NSNumber *resultValue = [resultsDictionary objectForKey:@"result"];
    return resultValue;

}
@end

0 comments:

Post a Comment

Powered by Blogger.
Twitter Delicious Facebook Digg Stumbleupon Favorites More