2010-05-10 28 views
7

Tôi đang cố tìm ngày cũ nhất trong một thuộc tính cụ thể trong Dữ liệu chính. Tôi đã tìm thấy an example in the Core Data Programming Guide rằng purports thực hiện chính xác điều đó nhưng vẫn gặp phải lỗi đã chọn không được công nhận khi tôi chạy nó.Dữ liệu cốt lõi: cố gắng tìm ngày tối thiểu cho một thuộc tính trong một thực thể

Mã của tôi (với những thay đổi chỉ tối thiểu từ Apple Ví dụ):

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]; 

Và các lỗi:

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

Đó là kỳ lạ cho rằng 1) NSCalendarDate bị phản đối và 2) Tôi m chắc chắn không gọi số.

Mọi trợ giúp sẽ được đánh giá cao nhất!

+0

Có phải "startedAt" được định nghĩa là NSDate không? và nó có được lập chỉ mục hay không. Tôi đã sử dụng gần như mã chính xác cho bạn và nó hoạt động rất tốt. Tôi nghĩ rằng nó là tốt hơn so với sử dụng một loại –

Trả lời

12

Tại sao không chỉ thêm bộ mô tả sắp xếp để sắp xếp theo số tăng thêm startedDate và sau đó chỉ yêu cầu tìm nạp trả về 1 đối tượng?

+0

Vâng, rõ ràng là bởi vì tôi đã không suy nghĩ ... :-) Điều đó hoạt động. Cảm ơn! – AndrewO

+0

Đó là ... rất thông minh. Vỗ tay. – duci9y

+0

Tôi nghĩ rằng việc sử dụng biểu thức trên một sắp xếp sẽ hiệu quả hơn. –

0

Đây là mã của tôi hoạt động. Tôi không thể thấy bất kỳ sự khác biệt đáng kể nào đối với mã của riêng bạn và có lẽ nó nằm trong định nghĩa của mô hình dữ liệu cốt lõi. Hãy chắc chắn rằng ngày của bạn là NSDate và nó được lập chỉ mục.

- (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; 
} 
Các vấn đề liên quan