获取核心数据时出现NSEntityDescription错误

时间:2011-08-05 04:51:08

标签: iphone objective-c xcode core-data nsentitydescription

这是我获取核心数据的功能

- (void)fetchRecords {   

    // Define our table/entity to use  
    NSEntityDescription *entity = [NSEntityDescription entityForName:@"Fugitive" inManagedObjectContext:managedObjectContext];   

    // Setup the fetch request  
    NSFetchRequest *request = [[NSFetchRequest alloc] init];  
    [request setEntity:entity];   

    // Define how we will sort the records  
    NSSortDescriptor *sortDescriptor = [[NSSortDescriptor alloc] initWithKey:@"name" ascending:NO];  
    NSArray *sortDescriptors = [NSArray arrayWithObject:sortDescriptor];  
    [request setSortDescriptors:sortDescriptors];  
    [sortDescriptor release];   

    // Fetch the records and handle an error  
    NSError *error;  
    NSMutableArray *mutableFetchResults = [[managedObjectContext executeFetchRequest:request error:&error] mutableCopy];   

    if (!mutableFetchResults) {  
        NSLog(@"Error in Fetching Result");
        // Handle the error.  
        // This is a serious error and should advise the user to restart the application  
    }

    // Save our fetched data to an array
    [self setFugitiveArray: mutableFetchResults];
    [mutableFetchResults release];
    [request release];
}

我在运行程序'程序接收信号时遇到以下错误:“SIGABRT”'

iBountyHunter[1013:207] *** Terminating app due to uncaught exception 'NSInternalInconsistencyException', reason: '+entityForName: could not locate an NSManagedObjectModel for entity name 'Fugitive''
*** Call stack at first throw:
(
    0   CoreFoundation                      0x011005a9 __exceptionPreprocess + 185
    1   libobjc.A.dylib                     0x01254313 objc_exception_throw + 44
    2   CoreData                            0x00e2aebb +[NSEntityDescription entityForName:inManagedObjectContext:] + 187
    3   iBountyHunter                       0x000030e6 -[FugitiveListViewController fetchRecords] + 86
    4   iBountyHunter                       0x00003411 -[FugitiveListViewController viewDidLoad] + 289
    5   UIKit                               0x00375f26 -[UINib instantiateWithOwner:options:] + 1556
    6   UIKit                               0x00377ab7 -[NSBundle(UINSBundleAdditions) loadNibNamed:owner:options:] + 168
    7   UIKit                               0x0017d17a -[UIApplication _loadMainNibFile] + 172
    8   UIKit                               0x0017dcf4 -[UIApplication _runWithURL:payload:launchOrientation:statusBarStyle:statusBarHidden:] + 291
    9   UIKit                               0x00188617 -[UIApplication handleEvent:withNewEvent:] + 1533
    10  UIKit                               0x00180abf -[UIApplication sendEvent:] + 71
    11  UIKit                               0x00185f2e _UIApplicationHandleEvent + 7576
    12  GraphicsServices                    0x01c48992 PurpleEventCallback + 1550
    13  CoreFoundation                      0x010e1944 __CFRUNLOOP_IS_CALLING_OUT_TO_A_SOURCE1_PERFORM_FUNCTION__ + 52
    14  CoreFoundation                      0x01041cf7 __CFRunLoopDoSource1 + 215
    15  CoreFoundation                      0x0103ef83 __CFRunLoopRun + 979
    16  CoreFoundation                      0x0103e840 CFRunLoopRunSpecific + 208
    17  CoreFoundation                      0x0103e761 CFRunLoopRunInMode + 97
    18  UIKit                               0x0017d7d2 -[UIApplication _run] + 623
    19  UIKit                               0x00189c93 UIApplicationMain + 1160
    20  iBountyHunter                       0x00002519 main + 121
    21  iBountyHunter                       0x00002495 start + 53
)
terminate called after throwing an instance of 'NSException'

1 个答案:

答案 0 :(得分:0)

如果您已在app delegate中声明,那么您应该检查它:

if (managedObjectContext == nil) 
{ 
        managedObjectContext = [(YourAppDelegate *)[[UIApplication sharedApplication] delegate] managedObjectContext]; 
        NSLog(@" %@",  managedObjectContext);
}