0
- (void)fetchResult 
{ 
    NSFetchRequest *request = [[NSFetchRequest alloc] init]; 
    NSSortDescriptor *sortDescriptor = [NSSortDescriptor sortDescriptorWithKey:@"Project" ascending:YES]; 

    [request setEntity:self.entityDescription]; 
    [request setSortDescriptors:[NSArray arrayWithObject:sortDescriptor]]; 
    self.fetchResultController = [[NSFetchedResultsController alloc] initWithFetchRequest:request managedObjectContext:self.managedObjectContext sectionNameKeyPath:nil cacheName:nil]; 

    NSError *fetchError = nil; 
    BOOL success = [self.fetchResultController performFetch:&fetchError]; 
    if (success) { 
     NSLog(@"fetched!"); 
    } 
    else { 
    NSLog(@"fetch fail!"); 
    } 
} 

的entityDescription和設置在viewDidLoad方法,因爲它是如下圖所示的背景:核心數據崩潰:的keyPath項目實體未發現

AppDelegate *appDelegate = [[UIApplication sharedApplication] delegate]; 
self.managedObjectContext = appDelegate.managedObjectContext; 
self.entityDescription = [NSEntityDescription entityForName:@"Project" inManagedObjectContext:self.managedObjectContext]; 

崩潰信息: *終止應用程序由於未捕獲的異常「NSInvalidArgumentException」,原因是:


可能有人送「的keyPath項目實體沒有發現」手?;)

回答

5

的關鍵在於排序描述符

[NSSortDescriptor sortDescriptorWithKey:@"Project" ascending:YES]; 

使用必須是你的核心數據實體的屬性,而不是實體的名稱本身。 因此,您應該使用 項目實體的某些屬性(如@"name")替換排序描述符中的@"Project"

+0

非常感謝你的回答;)你做出正確的猜測! – JackieLam 2013-05-02 10:35:19

0

設置緩存的名字,如:

self.fetchResultController = [[NSFetchedResultsController alloc] initWithFetchRequest:request managedObjectContext:self.managedObjectContext sectionNameKeyPath:nil cacheName:@"temp"]; 
0
NSFetchRequest *fetchRequest = [[NSFetchRequest alloc] init]; 

[fetchRequest setPredicate:predicate]; 

NSEntityDescription *entity = [NSEntityDescription entityForName:@"Project"   inManagedObjectContext:context]; 

NSSortDescriptor *sortDescriptor = [NSSortDescriptor sortDescriptorWithKey:@"Column_Name" ascending:YES]; 

[request setSortDescriptors:[NSArray arrayWithObject:sortDescriptor]]; 

[fetchRequest setEntity:entity]; 

NSError *error = nil; 
NSArray *arrTemp = [context executeFetchRequest:fetchRequest error:&error]; 

if([arrTemp count]>0){ 
    NSLog(@"%@",arrTemp); 
}