如何解决 NSInternalInconsistencyException',原因:'+entityForName: 失败报告

发布于 2024-10-15 00:04:27 字数 5538 浏览 3 评论 0原文

我的代码如下所示:

NSManagedObjectContext *context = [self.fetchedResultsController managedObjectContext];
        NSEntityDescription *entity = [[self.fetchedResultsController fetchRequest] entity];
        NSManagedObject *newManagedObject = [NSEntityDescription insertNewObjectForEntityForName:[entity name] inManagedObjectContext:context];

        // If appropriate, configure the new managed object.
        [newManagedObject setValue:[NSDate date] forKey:@"date"];

           // Save the context.
            NSError *error = nil;
            if (![context save:&error]) { 
                NSLog(@"Unresolved error %@, %@", error, [error userInfo]);
                abort();
        }

.h 头文件中,我有这个:

@interface TretiViewController : UIViewController <NSFetchedResultsControllerDelegate> {

 NSFetchedResultsController *fetchedResultsController_;
    NSManagedObjectContext *managedObjectContext_;

 // private NSString *accountID;
}


@property (nonatomic, retain) NSManagedObjectContext *managedObjectContext;
@property (nonatomic, retain) NSFetchedResultsController *fetchedResultsController;

它应该使用核心数据保存数据,但它出现错误:

NavTest[1372:207] *** Terminating app due to uncaught exception 'NSInternalInconsistencyException', reason: '+entityForName: could not locate an NSManagedObjectModel for entity name 'Obrat''
*** Call stack at first throw:
(
 0   CoreFoundation                      0x00f92be9 __exceptionPreprocess + 185
 1   libobjc.A.dylib                     0x010e75c2 objc_exception_throw + 47
 2   CoreData                            0x00cbf45b +[NSEntityDescription entityForName:inManagedObjectContext:] + 187
 3   NavTest                             0x0000c210 -[TretiViewController fetchedResultsController] + 187
 4   NavTest                             0x00007a76 -[TretiViewController ukazObrat0] + 38
 5   UIKit                               0x002c4a6e -[UIApplication sendAction:to:from:forEvent:] + 119
 6   UIKit                               0x003531b5 -[UIControl sendAction:to:forEvent:] + 67
 7   UIKit                               0x00355647 -[UIControl(Internal) _sendActionsForEvents:withEvent:] + 527
 8   UIKit                               0x00354438 -[UIControl touchesBegan:withEvent:] + 277
 9   UIKit                               0x0054f987 _UIGestureRecognizerSortAndSendDelayedTouches + 3609
 10  UIKit                               0x005500fc _UIGestureRecognizerUpdateObserver + 927
 11  CoreFoundation                      0x00f73fbb __CFRUNLOOP_IS_CALLING_OUT_TO_AN_OBSERVER_CALLBACK_FUNCTION__ + 27
 12  CoreFoundation                      0x00f090e7 __CFRunLoopDoObservers + 295
 13  CoreFoundation                      0x00ed1bd7 __CFRunLoopRun + 1575
 14  CoreFoundation                      0x00ed1240 CFRunLoopRunSpecific + 208
 15  CoreFoundation                      0x00ed1161 CFRunLoopRunInMode + 97
 16  GraphicsServices                    0x018c7268 GSEventRunModal + 217
 17  GraphicsServices                    0x018c732d GSEventRun + 115
 18  UIKit                               0x002d342e UIApplicationMain + 1160
 19  NavTest                             0x00002594 main + 102
 20  NavTest                             0x00002525 start + 53
)

NavTest.xcdatamodel 中,我有两个实体Obrta 包含 attributesEvet 这是默认的。

- (NSFetchedResultsController *)fetchedResultsController {

    if (fetchedResultsController_ != nil) {
        return fetchedResultsController_;
    }


   //  Set up the fetched results controller.

  // Create the fetch request for the entity.
  NSFetchRequest *fetchRequest = [[NSFetchRequest alloc] init];
  // Edit the entity name as appropriate.
  NSEntityDescription *entity = [NSEntityDescription entityForName:@"Obrat" inManagedObjectContext:self.managedObjectContext];
  [fetchRequest setEntity:entity];

  // Set the batch size to a suitable number.
  [fetchRequest setFetchBatchSize:20];

  // Edit the sort key as appropriate.
  NSSortDescriptor *sortDescriptor = [[NSSortDescriptor alloc] initWithKey:@"date" ascending:NO];
  NSArray *sortDescriptors = [[NSArray alloc] initWithObjects:sortDescriptor, nil];

  [fetchRequest setSortDescriptors:sortDescriptors];

  // Edit the section name key path and cache name if appropriate.
  // nil for section name key path means "no sections".
  NSFetchedResultsController *aFetchedResultsController = [[NSFetchedResultsController alloc] initWithFetchRequest:fetchRequest managedObjectContext:self.managedObjectContext sectionNameKeyPath:nil cacheName:@"Root"];
  aFetchedResultsController.delegate = self;
  self.fetchedResultsController = aFetchedResultsController;

  [aFetchedResultsController release];
  [fetchRequest release];
  [sortDescriptor release];
  [sortDescriptors release];

  NSError *error = nil;
  if (![fetchedResultsController_ performFetch:&error]) {

 // Replace this implementation with code to handle the error appropriately.

 // abort() causes the application to generate a crash log and terminate. You should not use this function in a shipping application, although it may be useful during development. If it is not possible to recover from the error, display an alert panel that instructs the user to quit the application by pressing the Home button.

  NSLog(@"Unresolved error %@, %@", error, [error userInfo]);
  abort();
  }

  return fetchedResultsController_;
  }    

我缺少什么?

My code looks like that:

NSManagedObjectContext *context = [self.fetchedResultsController managedObjectContext];
        NSEntityDescription *entity = [[self.fetchedResultsController fetchRequest] entity];
        NSManagedObject *newManagedObject = [NSEntityDescription insertNewObjectForEntityForName:[entity name] inManagedObjectContext:context];

        // If appropriate, configure the new managed object.
        [newManagedObject setValue:[NSDate date] forKey:@"date"];

           // Save the context.
            NSError *error = nil;
            if (![context save:&error]) { 
                NSLog(@"Unresolved error %@, %@", error, [error userInfo]);
                abort();
        }

in .h header file I have this:

@interface TretiViewController : UIViewController <NSFetchedResultsControllerDelegate> {

 NSFetchedResultsController *fetchedResultsController_;
    NSManagedObjectContext *managedObjectContext_;

 // private NSString *accountID;
}


@property (nonatomic, retain) NSManagedObjectContext *managedObjectContext;
@property (nonatomic, retain) NSFetchedResultsController *fetchedResultsController;

It should save data using core data but it falls on error:

NavTest[1372:207] *** Terminating app due to uncaught exception 'NSInternalInconsistencyException', reason: '+entityForName: could not locate an NSManagedObjectModel for entity name 'Obrat''
*** Call stack at first throw:
(
 0   CoreFoundation                      0x00f92be9 __exceptionPreprocess + 185
 1   libobjc.A.dylib                     0x010e75c2 objc_exception_throw + 47
 2   CoreData                            0x00cbf45b +[NSEntityDescription entityForName:inManagedObjectContext:] + 187
 3   NavTest                             0x0000c210 -[TretiViewController fetchedResultsController] + 187
 4   NavTest                             0x00007a76 -[TretiViewController ukazObrat0] + 38
 5   UIKit                               0x002c4a6e -[UIApplication sendAction:to:from:forEvent:] + 119
 6   UIKit                               0x003531b5 -[UIControl sendAction:to:forEvent:] + 67
 7   UIKit                               0x00355647 -[UIControl(Internal) _sendActionsForEvents:withEvent:] + 527
 8   UIKit                               0x00354438 -[UIControl touchesBegan:withEvent:] + 277
 9   UIKit                               0x0054f987 _UIGestureRecognizerSortAndSendDelayedTouches + 3609
 10  UIKit                               0x005500fc _UIGestureRecognizerUpdateObserver + 927
 11  CoreFoundation                      0x00f73fbb __CFRUNLOOP_IS_CALLING_OUT_TO_AN_OBSERVER_CALLBACK_FUNCTION__ + 27
 12  CoreFoundation                      0x00f090e7 __CFRunLoopDoObservers + 295
 13  CoreFoundation                      0x00ed1bd7 __CFRunLoopRun + 1575
 14  CoreFoundation                      0x00ed1240 CFRunLoopRunSpecific + 208
 15  CoreFoundation                      0x00ed1161 CFRunLoopRunInMode + 97
 16  GraphicsServices                    0x018c7268 GSEventRunModal + 217
 17  GraphicsServices                    0x018c732d GSEventRun + 115
 18  UIKit                               0x002d342e UIApplicationMain + 1160
 19  NavTest                             0x00002594 main + 102
 20  NavTest                             0x00002525 start + 53
)

in NavTest.xcdatamodel I have two entytis Obrta whith attributes and Evet which is default.

- (NSFetchedResultsController *)fetchedResultsController {

    if (fetchedResultsController_ != nil) {
        return fetchedResultsController_;
    }


   //  Set up the fetched results controller.

  // Create the fetch request for the entity.
  NSFetchRequest *fetchRequest = [[NSFetchRequest alloc] init];
  // Edit the entity name as appropriate.
  NSEntityDescription *entity = [NSEntityDescription entityForName:@"Obrat" inManagedObjectContext:self.managedObjectContext];
  [fetchRequest setEntity:entity];

  // Set the batch size to a suitable number.
  [fetchRequest setFetchBatchSize:20];

  // Edit the sort key as appropriate.
  NSSortDescriptor *sortDescriptor = [[NSSortDescriptor alloc] initWithKey:@"date" ascending:NO];
  NSArray *sortDescriptors = [[NSArray alloc] initWithObjects:sortDescriptor, nil];

  [fetchRequest setSortDescriptors:sortDescriptors];

  // Edit the section name key path and cache name if appropriate.
  // nil for section name key path means "no sections".
  NSFetchedResultsController *aFetchedResultsController = [[NSFetchedResultsController alloc] initWithFetchRequest:fetchRequest managedObjectContext:self.managedObjectContext sectionNameKeyPath:nil cacheName:@"Root"];
  aFetchedResultsController.delegate = self;
  self.fetchedResultsController = aFetchedResultsController;

  [aFetchedResultsController release];
  [fetchRequest release];
  [sortDescriptor release];
  [sortDescriptors release];

  NSError *error = nil;
  if (![fetchedResultsController_ performFetch:&error]) {

 // Replace this implementation with code to handle the error appropriately.

 // abort() causes the application to generate a crash log and terminate. You should not use this function in a shipping application, although it may be useful during development. If it is not possible to recover from the error, display an alert panel that instructs the user to quit the application by pressing the Home button.

  NSLog(@"Unresolved error %@, %@", error, [error userInfo]);
  abort();
  }

  return fetchedResultsController_;
  }    

What am I missing?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

罪#恶を代价 2024-10-22 00:04:27

fetchedResultsController 中的代码更改

NSEntityDescription *entity = [NSEntityDescription entityForName:@"Obrat" inManagedObjectContext:self.managedObjectContext];

NSEntityDescription *entity = [NSEntityDescription entityForName:@"Obrta" inManagedObjectContext:self.managedObjectContext];

使用 Xcode 创建的 Entity name 必须与代码中的 NSEntityDescription 保持一致

Change the code in fetchedResultsController :

NSEntityDescription *entity = [NSEntityDescription entityForName:@"Obrat" inManagedObjectContext:self.managedObjectContext];

to

NSEntityDescription *entity = [NSEntityDescription entityForName:@"Obrta" inManagedObjectContext:self.managedObjectContext];

Entity named you create with Xcode must keep same with the NSEntityDescription in your code

蓝色星空 2024-10-22 00:04:27

您说您有一个名为 “Obrta” 的实体,但代码正在创建一个 “Obrat” 类型的对象。修复该拼写错误应该可以解决您的问题。

You say you have an entity named "Obrta", but the code is creating an object of type "Obrat". Fixing that typo should fix your issue.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文