2012-01-22 196 views
0

我的應用程序有多個視圖控制器。在我的VehicleListController中,我將數據保存到核心數據。在FavouritesController中,我從核心數據中獲取數據以在表格視圖中顯示它。核心數據錯誤iPhone

我收到此錯誤,同時檢查favouritesController表來查看核心數據。

'NSInternalInconsistencyException', reason: '+entityForName: could not locate an NSManagedObjectModel for entity name 'Favouritesdata'' 

實體名稱是Favouritesdata。

在應用程序委託方法didFinishLaunchingWithOptions是

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions{ 

self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]]; 
// Override point for customization after application launch. 

NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults]; 
NSString *user = [defaults objectForKey:@"Username"]; 
NSString *passwd = [defaults objectForKey:@"Password"]; 

if ((user != nil) && (passwd != nil)) { 

    NSLog(@"Data found"); 

    self.progressView = [[Progressbar alloc] initWithNibName:@"Progressbar" bundle:nil]; 
    self.window.rootViewController = self.progressView; 
    [self.window makeKeyAndVisible]; 

} 
else { 

    NSLog(@"No data saved"); 

    self.viewController = [[VektorViewController alloc] initWithNibName:@"VektorViewController" bundle:nil]; 
    self.window.rootViewController = self.viewController; 
    [self.window makeKeyAndVisible]; 
} 

return YES;} 

在favouritesController.m獲取數據

-(void)getData { 

NSEntityDescription *entity = [NSEntityDescription entityForName:@"Favouritesdata" inManagedObjectContext:context]; 
NSFetchRequest *request = [[NSFetchRequest alloc] init]; 
[request setFetchBatchSize:20]; 
[request setEntity:entity]; 
NSSortDescriptor *sort = [[NSSortDescriptor alloc] initWithKey:@"licensePlate" ascending:YES]; 
NSArray *newArray = [NSArray arrayWithObject:sort]; 
[request setSortDescriptors:newArray]; 
NSError *error; 
NSMutableArray *results = [[context executeFetchRequest:request error:&error] mutableCopy]; 
[self setLicensePlateArray:results]; 
[self.favouritesTable reloadData]; 
} 

而在的cellForRowAtIndexPath在favouritesController

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { 

static NSString *CellIdentifier = @"Cell"; 

UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier]; 

if (cell == nil) { 

    cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease]; 

    UILongPressGestureRecognizer *pressRecongnizer = [[UILongPressGestureRecognizer alloc] initWithTarget:self action:@selector(tableCellPressed:)]; 
    pressRecongnizer.minimumPressDuration = 0.5f; 
    [cell addGestureRecognizer:pressRecongnizer]; 
    [pressRecongnizer release]; 
} 

Favouritesdata *favdata = [licensePlateArray objectAtIndex:indexPath.row]; 

NSLog(@"favdata: %@", favData); 

if ([tableView isEqual:self.searchDisplayController.searchResultsTableView]){ 
    cell.textLabel.text = 
    [self.filteredListItems objectAtIndex:indexPath.row]; 
} 
else{ 
    cell.textLabel.text = [favdata licenseplate]; 
    // [self.licensePlateArray objectAtIndex:indexPath.row]; 
} 

return cell;} 

在應用程序核心數據存取delegate.m:

- (NSManagedObjectContext *) managedObjectContext { 
if (managedObjectContext != nil) { 
    return managedObjectContext; 
} 
NSPersistentStoreCoordinator *coordinator = [self persistentStoreCoordinator]; 
if (coordinator != nil) { 
    managedObjectContext = [[NSManagedObjectContext alloc] init]; 
    [managedObjectContext setPersistentStoreCoordinator: coordinator]; 
} 

return managedObjectContext;} 

- (NSManagedObjectModel *)managedObjectModel { 
if (managedObjectModel != nil) { 
    return managedObjectModel; 
} 
managedObjectModel = [[NSManagedObjectModel mergedModelFromBundles:nil] retain]; 

return managedObjectModel;} 

- (NSPersistentStoreCoordinator *)persistentStoreCoordinator { 
if (persistentStoreCoordinator != nil) { 
    return persistentStoreCoordinator; 
} 
NSURL *storeUrl = [NSURL fileURLWithPath: [[self applicationDocumentsDirectory] 
              stringByAppendingPathComponent: @"LoginTest.sqlite"]]; 
NSError *error = nil; 
persistentStoreCoordinator = [[NSPersistentStoreCoordinator alloc] 
           initWithManagedObjectModel:[self managedObjectModel]]; 
if(![persistentStoreCoordinator addPersistentStoreWithType:NSSQLiteStoreType 
              configuration:nil URL:storeUrl options:nil error:&error]) { 
    /*Error for store creation should be handled in here*/ 
} 

return persistentStoreCoordinator;} 

- (NSString *)applicationDocumentsDirectory { 
return [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) lastObject];} 

我已將核心數據添加到現有項目。現在我不知道爲什麼我得到這個錯誤,而我認爲我已經將所有必需的方法和框架複製到了我的項目中。

有誰能告訴我嗎?

回答

0

錯誤消息很明顯:周圍沒有NSManagedObjectModel的實例。

我只在一個項目中使用了Core Data,並且我不知道這是否是一種普遍讚譽的方式來執行此操作,但是我在應用程序委託中設置了各種核心數據相關對象,然後傳遞受管對象上下文圍繞每個需要它的控制器。

但無論如何,核心數據起初有點複雜。嘗試並閱讀關於它的一些東西。

如果它可以幫助你,這是我做到的。

- (NSManagedObjectModel*) managedObjectModel 
{ 
    if(!_managedObjectModel) 
    { 
     NSString* modelPath = [[NSBundle mainBundle] pathForResource:@"Model" 
                   ofType:@"momd"]; 
     NSURL* modelURL = [NSURL fileURLWithPath:modelPath]; 
     _managedObjectModel = [[NSManagedObjectModel alloc] initWithContentsOfURL:modelURL]; 
    } 

    return _managedObjectModel; 
} 

- (NSPersistentStoreCoordinator*) persistentStoreCoordinator 
{ 
    if(!_persistentStoreCoordinator) 
    { 
     NSError* error = nil; 
     NSURL* storeURL = [NSURL fileURLWithPath:self.dataStorePath]; 
     NSDictionary* options = [NSDictionary dictionaryWithObjectsAndKeys: 
           [NSNumber numberWithBool:YES], NSMigratePersistentStoresAutomaticallyOption, 
           [NSNumber numberWithBool:YES], NSInferMappingModelAutomaticallyOption, nil]; 

     _persistentStoreCoordinator = [[NSPersistentStoreCoordinator alloc] 
             initWithManagedObjectModel:self.managedObjectModel]; 

     if(![_persistentStoreCoordinator addPersistentStoreWithType:NSSQLiteStoreType 
                 configuration:nil 
                   URL:storeURL 
                  options:options 
                   error:&error]) 
     { 
      #warning handle core data error 
      NSLog(@"Error adding persistent store %@, %@", error, error.userInfo); 
      abort(); 
     } 
    } 

    return _persistentStoreCoordinator; 
} 

- (NSManagedObjectContext*) managedObjectContext 
{ 
    if(!_managedObjectContext) 
    { 
     NSPersistentStoreCoordinator* coordinator = self.persistentStoreCoordinator; 

     if(coordinator) 
     { 
      _managedObjectContext = [[NSManagedObjectContext alloc] init]; 
      [_managedObjectContext setPersistentStoreCoordinator:coordinator]; 
     } 
    } 

    return _managedObjectContext; 
} 
0

看起來CoreData並沒有被初始化爲在您嘗試訪問該實體之前加載了您的模型。在獲取實體的代碼以及加載CoreData模型的代碼中查看斷點,並查看哪個代碼是首先調用的(或者是否有任何加載數據模型的錯誤)。

0

我認爲你的上下文可能是零(我可以在我的應用程序中產生與零上下文相同的錯誤)。它在你的getData方法中:

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

你可以通過代碼初始化上下文嗎?

+0

我編輯了我的問題,並添加了核心訪問者數據,請檢查它 –

0

兩件事情:

  • 確保在名爲「Favouritesdata」(非常區分大小寫)的核心數據模型有一個實體。
  • 與您的問題並不真正相關,但在NSUserDefaults中存儲密碼並不是一個好主意。用戶名確定,但不是密碼。這可以被具有正確知識的人破解,如果可以幫助你,你不想在手機上存儲這種東西。
+0

我編輯了我的問題並添加了核心訪問者數據,請檢查它 –

0

參考這個問題上堆棧溢出

Core-Data iPhone: could not locate an NSManagedObjectModel

我已經在我的應用程序採用相同的代碼維瓦斯建議:

將下面的代碼的RootViewController的的viewDidLoad消除了內錯誤:

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

現在我是n沒有得到那個錯誤:)