开发者

NSExpressionDescription for empty result sets

开发者 https://www.devze.com 2023-04-01 18:40 出处:网络
Using Core Data, I have a fetch request to fetch the minimum of a certain attribute using an expression. If I set a predicate on the request that results in no results being matched, I get EXC_BAD_ACC

Using Core Data, I have a fetch request to fetch the minimum of a certain attribute using an expression. If I set a predicate on the request that results in no results being matched, I get EXC_BAD_ACCESS. This makes some sense, seeing as you can't add nil to an NSArray for the results, but what's the best way around this?

I can just use a sort order and a fetch limit of 1, but it seems a bit of the oversigh开发者_如何学Ct in the NSExpressionDescription API that there's no way to return a default result or return an empty array if no objects were matched before evaluating the expression.

Or am I misdiagnosing the EXC_BAD_ACCESS entirely, and NSExpressionDescriptions, used in setPropertiesToFetch on a fetch request should already behave sensibly for this situation?


Ran into a similar situation today. Set your NSFetchRequest resultType to NSDictionaryResultType.

- (NSDate *)maxDateForEvent:(Event *)event withContext:(NSManagedContext *)context
{

    NSExpression *modifiedDateExpression = [NSExpression expressionForKeyPath:@"modifiedDate"];
    NSExpression *maxExpression = [NSExpression expressionForFunction:@"max:"
                                                              arguments:[NSArray arrayWithObject:modifiedDateExpression]];

    NSExpressionDescription *expressionDescription = [[NSExpressionDescription alloc] init];
    [expressionDescription setName:@"maxDate"];
    [expressionDescription setExpression:maxExpression];
    [expressionDescription setExpressionResultType:NSDateAttributeType];

    NSFetchRequest *fetch = [[NSFetchRequest alloc] init];
    fetch.entity = [NSEntityDescription entityForName:@"Event" inManagedObjectContext:context];
    fetch.propertiesToFetch = [NSArray arrayWithObject:expressionDescription];
    fetch.resultType = NSDictionaryResultType;
    fetch.predicate = [NSPredicate predicateWithFormat:@"event == %@", event];
    NSError *error;
    NSArray *array = [context executeFetchRequest:fetch error:&error];
    if (!array || ![array count]) {
        return [NSDate distantPast];
    }

    NSDate *date = [[array lastObject] objectForKey:@"maxDate"];
    if (!date) {
        return [NSDate distantPast];
    }

    return date;
}


The workaround in the second paragraph is what I've had to use, so this appears to just be an API oversight.

0

精彩评论

暂无评论...
验证码 换一张
取 消