2009-12-18 33 views
15

Tôi cần tìm giá trị lớn nhất của thuộc tính của thực thể dữ liệu cốt lõi.Tính giá trị lớn nhất cho thuộc tính dữ liệu cốt lõi - Lỗi NSCFNumber

Tôi vẫn vững chắc trong đường cong học tập Cocoa, và đây là một ứng dụng thử nghiệm đơn giản mà tôi đang sử dụng để tìm hiểu.

Ứng dụng nhập tài sản từ tệp văn bản và hiển thị bảng trên màn hình. Việc nhập khẩu được thực hiện trong một chủ đề nền riêng biệt.

tôi tìm thấy mã này trên mạng, mà tôi đã cố gắng để làm việc:

- (double)getMaxID 
{ 
    NSLog(@"in getMaxID"); // debug 

    // Use a new moc with the original persistentStoreCoordinator to ensure thread safety 
    NSManagedObjectContext *moc = [[NSManagedObjectContext alloc] init]; 
    [moc setPersistentStoreCoordinator:[[self delegate] persistentStoreCoordinator]]; 

    // Create fetch 
    NSFetchRequest *fetch = [[NSFetchRequest alloc] init]; 
    [fetch setEntity:[NSEntityDescription entityForName:@"Fortune" inManagedObjectContext:moc]]; 
    [fetch setResultType:NSDictionaryResultType]; 

    // Expression for Max ID 
    NSExpression *keyPathExpression = [NSExpression expressionForKeyPath:@"id"]; 
    NSExpression *minExpression = [NSExpression expressionForFunction:@"max:" arguments: [NSArray arrayWithObject:keyPathExpression]]; 
    NSExpressionDescription *expressionDescription = [[NSExpressionDescription alloc] init]; 
    [expressionDescription setName:@"maxID"]; 
    [expressionDescription setExpression:minExpression]; 
    [expressionDescription setExpressionResultType:NSDoubleAttributeType]; 
    [fetch setPropertiesToFetch:[NSArray arrayWithObject:expressionDescription]]; 

    // Execute the fetch. 
    double theID = 0; 
    NSError *error = nil; 
    NSArray *objects = nil; 
    objects = [moc executeFetchRequest:fetch error:&error]; // crashes here 

    if (objects && ([objects count] > 0)) 
    { 
     NSLog(@"query successful"); // debug 
     theID = [((NSNumber *)[[objects objectAtIndex:0] valueForKey:@"maxID"]) doubleValue]; 
    } 
    else 
    { 
     NSLog(@"Setting default value for theID"); // debug 
     theID = 0; 
    } 

    return(theID); 
} 

thực thể của tôi được gọi là "Fortune" và thuộc tính được gọi là "id" (cú đúp).

Khi mã chạy nó treo khi yêu cầu tìm nạp được thực thi. Bảng điều khiển hiển thị điều này:

 
2009-12-18 00:53:42.777 FortunesHelperMVC[4027:1703] -[NSCFNumber count]: unrecognized selector sent to instance 0x1004d7b10 
2009-12-18 00:53:42.778 FortunesHelperMVC[4027:1703] An uncaught exception was raised 
2009-12-18 00:53:42.778 FortunesHelperMVC[4027:1703] -[NSCFNumber count]: unrecognized selector sent to instance 0x1004d7b10 
2009-12-18 00:53:42.779 FortunesHelperMVC[4027:1703] *** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[NSCFNumber count]: unrecognized selector sent to instance 0x1004d7b10' 
*** Call stack at first throw: 
(
0 CoreFoundation 0x00007fff83ed9444 __exceptionPreprocess + 180 
1 libobjc.A.dylib 0x00007fff85fbb0f3 objc_exception_throw + 45 
2 CoreFoundation 0x00007fff83f321c0 +[NSObject(NSObject) doesNotRecognizeSelector:] + 0 
3 CoreFoundation 0x00007fff83eac08f ___forwarding___ + 751 
4 CoreFoundation 0x00007fff83ea81d8 _CF_forwarding_prep_0 + 232 
5 Foundation 0x00007fff88a5609e +[_NSPredicateUtilities max:] + 46 
6 Foundation 0x00007fff8893ce72 -[NSFunctionExpression expressionValueWithObject:context:] + 530 
7 CoreData 0x00007fff8613b5b1 -[NSMappedObjectStore executeFetchRequest:withContext:] + 2081 
8 CoreData 0x00007fff8613ad10 -[NSMappedObjectStore executeRequest:withContext:] + 80 
9 CoreData 0x00007fff86108900 -[NSPersistentStoreCoordinator(_NSInternalMethods) executeRequest:withContext:] + 688 
10 CoreData 0x00007fff8610621b -[NSManagedObjectContext executeFetchRequest:error:] + 267 
11 FortunesHelperMVC 0x0000000100001d9d -[ImportOperation getMaxID] + 572 
12 FortunesHelperMVC 0x0000000100001f95 -[ImportOperation main] + 330 
13 Foundation 0x00007fff888f406d -[__NSOperationInternal start] + 681 
14 Foundation 0x00007fff888f3d23 ____startOperations_block_invoke_2 + 99 
15 libSystem.B.dylib 0x00007fff86a98ce8 _dispatch_call_block_and_release + 15 
16 libSystem.B.dylib 0x00007fff86a77279 _dispatch_worker_thread2 + 231 
17 libSystem.B.dylib 0x00007fff86a76bb8 _pthread_wqthread + 353 
18 libSystem.B.dylib 0x00007fff86a76a55 start_wqthread + 13 
) 
terminate called after throwing an instance of 'NSException' 

Mọi ý tưởng tại sao điều này không hoạt động? Tôi đang bối rối sau rất nhiều Googling.

Cảm ơn

Darren.

Trả lời

2

Vấn đề là rằng dòng này là sai:

[fetch setResultType:NSDictionaryResultType]; 

Các loại quả cần phải có được NSManagedObjectResultType

Trân

Darren.

+1

Nhưng điều đó không chỉ trả về tất cả các thực thể thuộc loại đó (bỏ qua NSExpressionDescription bạn thiết lập? Nói cách khác, điều đó không gây ra sự cố, nhưng bạn sẽ không nhận được bất động sản bạn đang tìm kiếm, phải không? – Dov

15

tôi phải giải quyết một vấn đề tương tự và tiếp cận nó hơi khác bởi:

  • truy vấn đơn vị
  • đặt hàng bởi thuộc tính bạn muốn
  • giới hạn sự lấy kết quả tới 1

Mã cho điều này là dưới đây. Tôi đang truy vấn một thực thể có tên là 'entityName' và lấy giá trị lớn nhất cho thuộc tính 'sequenceId'.

NSFetchRequest *request = [[NSFetchRequest alloc] init]; 
NSEntityDescription *res = [NSEntityDescription entityForName:@"entityName" inManagedObjectContext:managedObjectContext]; 
[request setEntity:res]; 

NSSortDescriptor *sortDescriptor = [[NSSortDescriptor alloc] initWithKey:@"sequenceId" ascending:NO]; 
NSArray *sortDescriptors = [[NSArray alloc] initWithObjects:sortDescriptor, nil]; 
[request setSortDescriptors:sortDescriptors]; 
[sortDescriptors release]; 
[sortDescriptor release]; 

[request setFetchLimit:1]; 

NSError *error = nil; 
NSArray *results = [managedObjectContext executeFetchRequest:request error:&error]; 
[request release]; 
if (results == nil) { 
    NSLog(@"error fetching the results: %@",error); 
} 

NSInteger maximumValue = 0; 
if (results.count == 1) { 
    Result *result = (Result*)[results objectAtIndex:0]; 
    maximumValue = [result.sequenceId integerValue]; 
} 
+0

Khi tôi cố gắng làm những gì bạn nói, tôi nhận được kết quả như vậy.Tôi đoán, nó cho giới hạn tối đa của loại Int32. "account_id" = 139784529; – Harish

+2

Ít nhất trên iOS 6 và iOS 7, thời gian sắp xếp tăng tuyến tính với số lượng đối tượng trong cơ sở dữ liệu Ý tưởng tồi – Christoph

10

Mã này từ Core Data Programming Guide sẽ hoạt động.

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

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

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

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

// Create an expression description using the maxExpression 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:@"maxDate"]; 
[expressionDescription setExpression:maxExpression]; 
[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 = nil; 
NSArray *objects = [managedObjectContext executeFetchRequest:request error:&error]; 
if (objects == nil) { 
    // Handle the error. 
} 
else { 
    if ([objects count] > 0) { 
     NSLog(@"Maximum date: %@", [[objects objectAtIndex:0] valueForKey:@"maxDate"]); 
    } 
} 
+0

Tôi cũng sẽ thêm liên kết này vào một bài viết tuyệt vời: http://useyourloaf.com/blog/2012/01/19 /core-data-queries-using-expressions.html –

Các vấn đề liên quan