app crashes when fetching attribute values by count or sum functions #181

Closed
wants to merge 1 commit into
from

Conversation

Projects
None yet
2 participants
Contributor

alvinhu commented Apr 23, 2013

when fetching attribute values that satisfy a given function such as count, sum etc, the app will crashes with message:

*** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: 'Invalid property ((<NSAttributeDescription: 0x1f8a2750>), name countMember, isOptional 1, isTransient 0, entity Transaction, renamingIdentifier action, validation predicates (
), warnings (
), versionHashModifier (null)
 userInfo {
}, attributeType 100 , attributeValueClassName NSNumber, defaultValue 0) passed to setPropertiesToFetch: (entity mismatch)'.

The fetching snippet I used looks like below:

NSExpressionDescription *amountDescription = [[NSExpressionDescription alloc] init];
[amountDescription setName:@"amount"];
[amountDescription setExpression:[NSExpression expressionForKeyPath:@"@sum.amount"]];
[amountDescription setExpressionResultType:NSInteger32AttributeType];

NSFetchRequest *fetchTransaction = [NSFetchRequest fetchRequestWithEntityName:@"Transaction"];
[fetchTransaction setPredicate:[NSPredicate predicateWithFormat:@"shopId = %@", shop[@"shopId"]]];
[fetchTransaction setPropertiesToFetch:@[@"action", amountDescription]];
[fetchTransaction setPropertiesToGroupBy:@[@"action"]];
[fetchTransaction setResultType:NSDictionaryResultType];

[_managedObjectContext performBlockAndWait:^{
    NSError *error = nil;
    NSArray *transactionResults = [_managedObjectContext executeFetchRequest:fetchTransaction error:&error];
    if ([transactionResults lastObject]) {
        NSLog(@"%@", transactionResults);
    }
}];

This fixes the bug by transfer the propertiesToFetch and propertiesToGroupBy from fetchRequest to backingFetchRequest. fetchRequest and backingFetchRequest are in different NSManagedObjectContext.

@alvinhu alvinhu when fetching attribute values that satisfy a given function such as …
…count, sum etc, the app will crashes with message:

*** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: 'Invalid property ((<NSAttributeDescription: 0x1f8a2750>), name countMember, isOptional 1, isTransient 0, entity Transaction, renamingIdentifier action, validation predicates (
), warnings (
), versionHashModifier (null)
 userInfo {
}, attributeType 100 , attributeValueClassName NSNumber, defaultValue 0) passed to setPropertiesToFetch: (entity mismatch)'.

The fetching snippet I used looks like below:

    NSExpressionDescription *amountDescription = [[NSExpressionDescription alloc] init];
    [amountDescription setName:@"amount"];
    [amountDescription setExpression:[NSExpression expressionForKeyPath:@"@sum.amount"]];
    [amountDescription setExpressionResultType:NSInteger32AttributeType];

    NSFetchRequest *fetchTransaction = [NSFetchRequest fetchRequestWithEntityName:@"Transaction"];
    [fetchTransaction setPredicate:[NSPredicate predicateWithFormat:@"shopId = %@", shop[@"shopId"]]];
    [fetchTransaction setPropertiesToFetch:@[@"action", amountDescription]];
    [fetchTransaction setPropertiesToGroupBy:@[@"action"]];
    [fetchTransaction setResultType:NSDictionaryResultType];

    [_managedObjectContext performBlockAndWait:^{
        NSError *error = nil;
        NSArray *transactionResults = [_managedObjectContext executeFetchRequest:fetchTransaction error:&error];
        if ([transactionResults lastObject]) {
            NSLog(@"%@", transactionResults);
        }
    }];

This fixes the bug by transfer the propertiesToFetch and propertiesToGroupBy from fetchRequest to backingFetchRequest. fetchRequest and backingFetchRequest are in different NSManagedObjectContext.
928c0eb
Contributor

mattt commented Jun 19, 2013

This looks like a great patch, @alvinhu. Thanks so much, and apologies for taking so long to get around to taking a look at it.

If I'm not mistaken, wouldn't this logic be useful for the other fetch request result types as well? Would it make sense to put this outside the switch statement, and have all of that translated into the backing context?

Contributor

mattt commented Nov 18, 2014

As of e8c6472, AFIncrementalStore is no longer being actively maintained. Thank you for your contributions; my sincere apologies for any inconvenience this may have caused.

mattt closed this Nov 18, 2014

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment