2010-05-10 54 views
7

我想在覈心數據中的特定屬性中找到最早的日期。我發現an example in the Core Data Programming Guide這意味着做到了這一點,但是當我運行它時,仍然收到無法識別的選定錯誤。核心數據:試圖找到一個實體屬性的最短日期

我的代碼(從蘋果實例只有很少的改動):

NSFetchRequest *request = [[NSFetchRequest alloc] init]; 
NSEntityDescription *entity = [NSEntityDescription entityForName:@"Session" inManagedObjectContext: ctx]; 
[request setEntity:entity]; 

// Specify that the request should return dictionaries. 
[request setResultType:NSDictionaryResultType]; 

// Create an expression for the key path. 
NSExpression *keyPathExpression = [NSExpression expressionForKeyPath:@"startedAt"]; 

// Create an expression to represent the minimum value at the key path 'creationDate' 
NSExpression *minExpression = [NSExpression expressionForFunction:@"min:" arguments:[NSArray arrayWithObject:keyPathExpression]]; 

// Create an expression description using the minExpression and returning a date. 
NSExpressionDescription *expressionDescription = [[NSExpressionDescription alloc] init]; 

// The name is the key that will be used in the dictionary for the return value. 
[expressionDescription setName:@"minDate"]; 
[expressionDescription setExpression:minExpression]; 
[expressionDescription setExpressionResultType:NSDateAttributeType]; 

// Set the request's properties to fetch just the property represented by the expressions. 
[request setPropertiesToFetch:[NSArray arrayWithObject:expressionDescription]]; 

// Execute the fetch. 
NSError *error; 
NSArray *objects = [ctx executeFetchRequest:request error:&error]; 

和錯誤:

-[NSCalendarDate count]: unrecognized selector sent to instance ... 

這是奇怪的,因1)NSCalendarDate已被棄用,2)我」我絕對不會打電話給伯爵。

任何幫助將不勝感激!

+0

「startingAt」定義爲NSDate?並且它被索引。我已經使用了幾乎精確的代碼給你,它很好用。我認爲這比使用 – 2015-10-15 08:01:12

回答

12

爲什麼不只是添加一個排序描述符按startedDate升序排序,然後只有獲取請求返回1個對象?

+0

更好,顯然是因爲我沒有想到...... :-)這很有效。謝謝! – AndrewO 2010-05-11 02:56:55

+0

這是......非常聰明。掌聲。 – duci9y 2013-10-18 14:59:15

+0

我認爲使用表達式進行排序會更有效率。 – 2015-10-15 07:56:30

0

這是我的代碼,它的工作。我看不出你自己的代碼有什麼重大差別,也許它在覈心數據模型的定義中。確保你的日期是NSDate並且它被編入索引。

- (NSDate *)lastSync:(PHAssetMediaType)mediaType { 
    NSEntityDescription *entity = [NSEntityDescription entityForName:kMediaItemEntity inManagedObjectContext:self.managedObjectContext]; 

    NSFetchRequest *fetchRequest = [[NSFetchRequest alloc] init]; 
    fetchRequest.entity = entity; 
    fetchRequest.resultType = NSDictionaryResultType; 

    NSMutableArray *predicates = [NSMutableArray array]; 
    [predicates addObject:[NSPredicate predicateWithFormat:@"%K=%d", kMediaType,mediaType]]; 
    [predicates addObject:[NSPredicate predicateWithFormat:@"%K=%d", kMediaProviderType,self.mediaProviderType]]; 
    NSPredicate *predicate = [NSCompoundPredicate andPredicateWithSubpredicates: predicates]; 
    fetchRequest.predicate = predicate; 

    // Create an expression for the key path. 

    NSExpression *keyPathExpression = [NSExpression expressionForKeyPath:kSyncTime]; 
    // Create an expression to represent the function you want to apply 

    NSExpression *maxExpression = [NSExpression expressionForFunction:@"max:" 
                  arguments:@[keyPathExpression]]; 

    // Create an expression description using the maxExpression and returning a date. 
    NSExpressionDescription *expressionDescription = [[NSExpressionDescription alloc] init]; 
    [expressionDescription setName:@"maxDate"]; 
    [expressionDescription setExpression:maxExpression]; 
    [expressionDescription setExpressionResultType:NSDateAttributeType]; 

    // Set the request's properties to fetch just the property represented by the expressions. 
    fetchRequest.propertiesToFetch = @[expressionDescription] ; // @[kSyncTime]; 

    NSError *fetchError = nil; 
    id requestedValue = nil; 

    // fetch stored media 
    NSArray *results = [self.managedObjectContext executeFetchRequest:fetchRequest error:&fetchError]; 
    if (fetchError || results == nil || results.count == 0) { 
     return [NSDate dateWithTimeIntervalSince1970:0]; 
    } 
    requestedValue = [[results objectAtIndex:0] valueForKey:@"maxDate"]; 
    if (![requestedValue isKindOfClass:[NSDate class]]) { 
     return [NSDate dateWithTimeIntervalSince1970:0]; 
    } 
    DDLogDebug(@"sync date %@",requestedValue); 
    return (NSDate *)requestedValue; 
} 
相關問題