Сбой приложения Core data при выполнении запроса на выборку

#iphone

#iPhone

Вопрос:

Я извлекаю результат из объекта core data

 - (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:@"Tutorial" inManagedObjectContext: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:@"id" ascending:YES];
    NSArray *sortDescriptors = [[NSArray alloc] initWithObjects:sortDescriptor, nil];


    //sorting 
    [fetchRequest setSortDescriptors:sortDescriptors];

    NSFetchedResultsController *aFetchedResultsController = [[NSFetchedResultsController alloc] initWithFetchRequest:fetchRequest managedObjectContext:managedObjectContext sectionNameKeyPath:nil cacheName:@"Root"];
    aFetchedResultsController.delegate = self;
    self.fetchedResultsController = aFetchedResultsController;

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

    return fetchedResultsController;
}
  

Мое приложение выходит из строя при выполнении выборки

 - (void)viewDidLoad
{
    [super viewDidLoad];

    //title


    self.navigationItem.leftBarButtonItem = self.editButtonItem;
    //add song button



    UIBarButtonItem *addSongbutton = [[UIBarButtonItem alloc] initWithTitle:@"Add Songs" style:UIBarButtonItemStyleBordered target:self action:@selector(addSongs)];
    self.navigationItem.rightBarButtonItem = addSongbutton;

    [addSongbutton release];

    if (managedObjectContext == nil) {
        managedObjectContext = [(SongsWithLyricsAppDelegate *)[[UIApplication sharedApplication]delegate]managedObjectContext];
    }

    self.title = @"Song List";

    //Error message
    NSError *error =nil;
    if (![[self fetchedResultController]performFetch:amp;error]) {
        NSLog(@"Unresolved error %@, %@", error, [error userInfo]);

        abort();

    }




}
  

это вывод Nslog
Неразрешенная ошибка (null), (null)
пожалуйста, помогите

Комментарии:

1. ну, сбой из-за прерывания () :-).

2. хорошо .. но .. это не загружает записи, почему так?

Ответ №1:

grrr… всегда нажимайте return в комментарии… Я хотел добавить, что вывод ошибки (null) (null) выглядит так, как будто ошибки нет, но тогда выборка не должна возвращать false .

Вы уверены, что fetchedResultsController не равен нулю, когда вы пытаетесь вызвать performFetch на нем?

Комментарии:

1. хммм… теперь я вижу, что вы вызываете [self fetchedResultController …], но определенная вами функция получения называется fetchedResultsController (с «s») — поэтому функция получения никогда не вызывается. Вы должны назвать свою глобальную переменную NSFetchedResultsController так же, как и функцию получения, с помощью «s». Смотрите также developer.apple.com/library/mac/#documentation/Cocoa/Conceptual /…

Ответ №2:

Может быть, в вашей реализации setFetchedResultsController есть ошибка?