Iphone 由于核心数据错误,TableView崩溃/冻结
当用户单击导航栏中的+按钮时,会出现带有文本提示的UIAlert。然后,用户在提示符中输入一个字符串,它将生成一个新的UITableViewCell,名称为字符串 由于某种原因,当我进入这个viewController的屏幕时,应用程序正在崩溃 它认为它与ViewDidLoad中的以下行相关:Iphone 由于核心数据错误,TableView崩溃/冻结,iphone,objective-c,core-data,Iphone,Objective C,Core Data,当用户单击导航栏中的+按钮时,会出现带有文本提示的UIAlert。然后,用户在提示符中输入一个字符串,它将生成一个新的UITableViewCell,名称为字符串 由于某种原因,当我进入这个viewController的屏幕时,应用程序正在崩溃 它认为它与ViewDidLoad中的以下行相关:NSEntityDescription*entity=[NSEntityDescription entityForName:@“例程”在managedObjectContext:managedObjectC
NSEntityDescription*entity=[NSEntityDescription entityForName:@“例程”在managedObjectContext:managedObjectContext中]代码>
控制台显示以下信息:由于未捕获的异常“NSInternalInconsistencyException”而终止应用程序,原因:“+entityForName:找不到实体名称“例程”的NSManagedObjectModel
我想我需要用“名字”来代替常规,但这也不行
以下是我的核心数据模型:
这是我的密码:
#import "RoutineTableViewController.h"
#import "AlertPrompt.h"
#import "Routine.h"
@implementation RoutineTableViewController
@synthesize tableView;
@synthesize eventsArray;
@synthesize managedObjectContext;
- (void)dealloc
{
[managedObjectContext release];
[eventsArray release];
[super dealloc];
}
- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
}
#pragma mark - View lifecycle
- (void)viewDidLoad
{
UIBarButtonItem * addButton = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemAdd target:self action:@selector(showPrompt)];
[self.navigationItem setLeftBarButtonItem:addButton];
[addButton release];
UIBarButtonItem *editButton = [[UIBarButtonItem alloc]initWithTitle:@"Edit" style:UIBarButtonItemStyleBordered target:self action:@selector(toggleEdit)];
self.navigationItem.rightBarButtonItem = editButton;
[editButton release];
NSFetchRequest *request = [[NSFetchRequest alloc] init];
NSEntityDescription *entity = [NSEntityDescription entityForName:@"Routine" inManagedObjectContext:managedObjectContext];
[request setEntity:entity];
NSError *error = nil;
NSMutableArray *mutableFetchResults = [[managedObjectContext executeFetchRequest:request error:&error] mutableCopy];
if (mutableFetchResults == nil) {
// Handle the error.
}
[self setEventsArray:mutableFetchResults];
[mutableFetchResults release];
[request release];
[super viewDidLoad];
}
-(void)toggleEdit
{
[self.tableView setEditing: !self.tableView.editing animated:YES];
if (self.tableView.editing)
[self.navigationItem.rightBarButtonItem setTitle:@"Done"];
else
[self.navigationItem.rightBarButtonItem setTitle:@"Edit"];
}
-(void)showPrompt
{
AlertPrompt *prompt = [AlertPrompt alloc];
prompt = [prompt initWithTitle:@"Add Workout Day" message:@"\n \n Please enter title for workout day" delegate:self cancelButtonTitle:@"Cancel" okButtonTitle:@"Add"];
[prompt show];
[prompt release];
}
- (void)alertView:(UIAlertView *)alertView willDismissWithButtonIndex:(NSInteger)buttonIndex
{
if (buttonIndex != [alertView cancelButtonIndex])
{
NSString *entered = [(AlertPrompt *)alertView enteredText];
if(eventsArray && entered)
{
[eventsArray addObject:entered];
[tableView reloadData];
}
}
}
-(void)addEvent
{
Routine *routine = (Routine *)[NSEntityDescription insertNewObjectForEntityForName:@"Routine" inManagedObjectContext:managedObjectContext];
NSError *error = nil;
if (![managedObjectContext save:&error]) {
// Handle the error.
}
[eventsArray insertObject:routine atIndex:0];
NSIndexPath *indexPath = [NSIndexPath indexPathForRow:0 inSection:0];
[self.tableView insertRowsAtIndexPaths:[NSArray arrayWithObject:indexPath]
withRowAnimation:UITableViewRowAnimationFade];
[self.tableView scrollToRowAtIndexPath:[NSIndexPath indexPathForRow:0 inSection:0] atScrollPosition:UITableViewScrollPositionTop animated:YES];
}
- (void)viewDidUnload
{
self.eventsArray = nil;
[super viewDidUnload];
}
#pragma mark - Table view data source
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
return 1;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
return [eventsArray count];
}
- (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];
cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellEditingStyleDelete reuseIdentifier:CellIdentifier] autorelease];
}
cell.textLabel.text = [self.eventsArray objectAtIndex:indexPath.row];
return cell;
}
// Override to support conditional editing of the table view.
- (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath
{
// Return NO if you do not want the specified item to be editable.
return YES;
}
-(void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath
{
if (editingStyle == UITableViewCellEditingStyleDelete) {
// Delete the managed object at the given index path.
NSManagedObject *eventToDelete = [eventsArray objectAtIndex:indexPath.row];
[managedObjectContext deleteObject:eventToDelete];
// Update the array and table view.
[eventsArray removeObjectAtIndex:indexPath.row];
[tableView deleteRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:YES];
// Commit the change.
NSError *error = nil;
if (![managedObjectContext save:&error]) {
// Handle the error.
}
}
}
@end
我没有看到ManagedObjectContext在哪里被声明并连接到现有的数据模型中。在中,您在哪里声明从persistentStoreCoordinator访问它的“getter”。尝试检查连接并在viewDidLoad上插入。并检查此处的文档步骤:
这里有一个例子,它与你的perstistantStoreCoordinator挂钩
- (NSManagedObjectContext *) managedObjectContext {
if (managedObjectContext != nil) {
return managedObjectContext;
}
NSPersistentStoreCoordinator *coordinator = [self persistentStoreCoordinator];
if (coordinator != nil) {
managedObjectContext = [[NSManagedObjectContext alloc] init];
[managedObjectContext setPersistentStoreCoordinator: coordinator];
}
return managedObjectContext;
}
或者从您正在使用的教程中,您将在应用程序代理的didfishLaunching方法中看到它:
NSManagedObjectContext *context = [self managedObjectContext];
if (!context) {
// Handle the error.
}
// Pass the managed object context to the view controller.
rootViewController.managedObjectContext = context;
编辑
检查代码后,您需要做两件事:
1) 编辑AppDelegate以加载“Curl”模型,而不是“Temp”模型。这是xdatamodel的名称
2) 您需要引用应用程序代理的上下文,而不是在本地创建上下文。即
CurlAppDelegate *curlAppDelegate = [[UIApplication sharedApplication] delegate];
NSManagedObjectContext *context = [curlAppDelegate managedObjectContext];
NSFetchRequest *request = [[NSFetchRequest alloc] init];
NSEntityDescription *entity = [NSEntityDescription entityForName:@"Routine" inManagedObjectContext:context];
[request setEntity:entity];
NSError *error = nil;
NSMutableArray *mutableFetchResults = [[context executeFetchRequest:request error:&error] mutableCopy];
if (mutableFetchResults == nil) {
// Handle the error.
}
“NSInternalInconsistencyException”错误与更改基础数据模型导致核心数据无法执行以下操作有关:用于存储数据的实际SQLite或plist文件现在与数据模型的新结构不兼容
要清除它,你可以用通常的方法从模拟器(或设备,如果你正在测试的话)中删除应用程序——按住应用程序图标直到它摆动,然后点击/点击X——或者从Mac上的应用程序工作目录中删除文件本身
~/Library/Application Support/iPhone模拟器/YOUR-IOS-BASE-SDK-HERE/Applications/YOUR-36-BYTE-APP-ID-HERE/Documents
(如果不是文档,则以您使用的文件夹为准。)
之后,您可以运行应用程序,该特定错误将消失,因为核心数据将能够重新创建文件。您如何初始化managedObjectContext
?你确定它不是零吗?我正在关注这里发布的教程(),他们似乎没有使用你拥有的那些方法。还是我遗漏了呢?我添加了你发布的代码,看起来一切都应该正常,但仍然不起作用。在教程中(非常好),你应该在应用程序委托中设置上下文。是你干的吗?并确保xdatamodel已保存?你有一个链接到你的项目压缩的地方,我可以调试它?这是解释设置的页面:谢谢,多米尼克。我将回顾一下那个教程,看看我是否遗漏了什么。这里是我的项目压缩的链接:。感谢多明尼克,他修复了崩溃,现在视图可以正常工作了。但由于某些原因,它没有保存数据并在下一次启动应用程序时获取数据。我将再次检查代码,我可能忘记实现获取请求。