Сбой / зависание TableView из-за ошибки в основных данных - PullRequest
0 голосов
/ 28 марта 2011

Когда пользователь нажимает кнопку + на панели навигации, появляется UIAlert с текстовым приглашением. Затем пользователь вводит строку в приглашение, и это должно привести к созданию нового UITableViewCell с именем в качестве строки.

По какой-то причине происходит сбой приложения, когда я выхожу на экран для этого viewController.

Это связано со следующей строкой в ​​ViewDidLoad: NSEntityDescription *entity = [NSEntityDescription entityForName:@"Routine" inManagedObjectContext:managedObjectContext];.

Консоль говорит следующее: Terminating app due to uncaught exception 'NSInternalInconsistencyException', reason: '+entityForName: could not locate an NSManagedObjectModel for entity name 'Routine''

Я думаю, что мне нужно использовать «имя» вместо рутины, но это тоже не работает.

Вот моя основная модель данных: enter image description here

Вот мой код:

#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

Ответы [ 2 ]

1 голос
/ 28 марта 2011

Я не вижу, где ваш ManagedObjectContext объявлен и подключен к вашей существующей модели данных. Например, где вы объявляете свой «получатель» для доступа к нему из persistentStoreCoordinator . Попробуйте проверить ваше соединение и вставить на viewDidLoad. И проверьте шаги документации здесь:

http://developer.apple.com/library/mac/#documentation/Cocoa/Conceptual/CoreData/Articles/cdUsingMOM.html#//apple_ref/doc/uid/TP40005190-SW1

Вот пример того, как он подключается к вашему perstistantStoreCoordinator

- (NSManagedObjectContext *) managedObjectContext {

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

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

}

Или из учебника, который вы используете, вы увидите его в методе didFinishLaunching делегата приложения :

NSManagedObjectContext *context = [self managedObjectContext];
if (!context) {
    // Handle the error.
}
// Pass the managed object context to the view controller.
rootViewController.managedObjectContext = context;

EDIT

После просмотра вашего кода вам нужно сделать две вещи:

1) отредактируйте ваш AppDelegate для загрузки модели "Curl", а не модели "Temp". Это имя вашей xdatamodel.

2) Вам нужно сослаться на контекст вашего делегата приложения, а НЕ создавать его локально. * 1026 Т.е. *

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.
    }
0 голосов
/ 29 марта 2011

Ошибка «NSInternalInconsistencyException» связана с тем, что базовая модель данных была изменена настолько, что Core Data не может выполнить автоматическую облегченную миграцию данных : фактический файл SQLite или plist, который вы используете для Хранить ваши данные теперь несовместимо с новой структурой модели данных.

Чтобы очистить его, вы можете просто удалить приложение из симулятора (или устройства, если оно там, где вы тестируете) обычным способом - нажимать и удерживать значок приложения до тех пор, пока оно не покачивается, а затем нажать / нажать X - или удалив сам файл из рабочего каталога приложения на вашем Mac.

~/Library/Application Support/iPhone Simulator/YOUR-IOS-BASE-SDK-HERE/Applications/YOUR-36-BYTE-APP-ID-HERE/Documents

(Или, если не Документы, какую папку вы использовали.)

После этого вы можете запустить приложение, чтобы эта конкретная ошибка исчезла, потому что Core Data сможет снова создать файл с нуля.

Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...