FetchedResultsController添加到项目后的问

FetchedResultsController添加到项目后的问

本文介绍了将NSFetchedResultsController添加到项目后的问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在没有 NSFetchedResultsController 的情况下设置了CoreData,并保存了所有内容.切换到 NSFetchedResultsController 后,尝试保存图像时出现一个奇怪的错误.

I set up CoreData without NSFetchedResultsController and had everything saving fine. After switching to NSFetchedResultsController, I am getting a strange error when trying to save the image.

这是我用来保存图像的代码:

Here is the code I am using to save the image:

- (void)saveImage {

    NSManagedObjectContext *context = [self managedObjectContext];

    TimeTravelFeed *timeTravelFeed = [NSEntityDescription insertNewObjectForEntityForName:@"TimeTravelFeed" inManagedObjectContext:context];

    NSData *imageData = UIImageJPEGRepresentation(self.thumbImage, 0.8f);

    [timeTravelFeed setValue:imageData forKey:@"imageData"];

    NSError *error = nil;

    if (![self.managedObjectContext save:&error]) {
        NSLog(@"Unresolved error %@, %@", error, [error userInfo]);
        abort();
    }

    //[tableView reloadData];
}

这是错误消息:

-[_PFExternalReferenceData compare:]: unrecognized selector sent to instance 0x1669fb40
2013-12-08 10:09:49.442 Time Travel[830:60b] CoreData: error: Serious application error.  Exception was caught during Core Data change processing.  This is usually a bug within an observer of NSManagedObjectContextObjectsDidChangeNotification.
-[_PFExternalReferenceData compare:]: unrecognized selector sent to instance 0x1669fb40 with userInfo (null)
2013-12-08 10:09:49.443 Time Travel[830:60b] *** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[_PFExternalReferenceData compare:]: unrecognized selector sent to instance 0x1669fb40'

这是NSFetchedResultsController的代码:

Here is the code for NSFetchedResultsController:

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    DetailViewController *detailsViewController = [[DetailViewController alloc] init];
    [self.navigationController pushViewController:detailsViewController animated:YES];

}

- (void)controllerWillChangeContent:(NSFetchedResultsController *)controller {
    [self.tableView beginUpdates];
}


- (void)controller:(NSFetchedResultsController *)controller didChangeSection:(id <NSFetchedResultsSectionInfo>)sectionInfo
           atIndex:(NSUInteger)sectionIndex forChangeType:(NSFetchedResultsChangeType)type {

    switch(type) {
        case NSFetchedResultsChangeInsert:
            [self.tableView insertSections:[NSIndexSet indexSetWithIndex:sectionIndex]
                          withRowAnimation:UITableViewRowAnimationFade];
            break;

        case NSFetchedResultsChangeDelete:
            [self.tableView deleteSections:[NSIndexSet indexSetWithIndex:sectionIndex]
                          withRowAnimation:UITableViewRowAnimationFade];
            break;
    }
}


- (void)controller:(NSFetchedResultsController *)controller didChangeObject:(id)anObject
       atIndexPath:(NSIndexPath *)indexPath forChangeType:(NSFetchedResultsChangeType)type
      newIndexPath:(NSIndexPath *)newIndexPath {

    UITableView *tableView = self.tableView;

    switch(type) {

        case NSFetchedResultsChangeInsert:
            [tableView insertRowsAtIndexPaths:[NSArray arrayWithObject:newIndexPath]
                             withRowAnimation:UITableViewRowAnimationFade];
            break;

        case NSFetchedResultsChangeDelete:
            [tableView deleteRowsAtIndexPaths:[NSArray arrayWithObject:indexPath]
                             withRowAnimation:UITableViewRowAnimationFade];
            break;

        case NSFetchedResultsChangeUpdate:
            [self configureCell:[tableView cellForRowAtIndexPath:indexPath]
                    atIndexPath:indexPath];
            break;

        case NSFetchedResultsChangeMove:
            [tableView deleteRowsAtIndexPaths:[NSArray arrayWithObject:indexPath]
                             withRowAnimation:UITableViewRowAnimationFade];
            [tableView insertRowsAtIndexPaths:[NSArray arrayWithObject:newIndexPath]
                             withRowAnimation:UITableViewRowAnimationFade];
            break;
    }
}


- (void)controllerDidChangeContent:(NSFetchedResultsController *)controller {
    [self.tableView endUpdates];
}

- (NSFetchedResultsController *)fetchedResultsController {

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

    NSFetchRequest *fetchRequest = [[NSFetchRequest alloc] init];
    NSEntityDescription *entity = [NSEntityDescription
                                   entityForName:@"TimeTravelFeed" inManagedObjectContext:managedObjectContext];
    [fetchRequest setEntity:entity];

    NSSortDescriptor *sort = [[NSSortDescriptor alloc]
                              initWithKey:@"imageData" ascending:NO];
    [fetchRequest setSortDescriptors:[NSArray arrayWithObject:sort]];

    [fetchRequest setFetchBatchSize:20];

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

    return _fetchedResultsController;

}

推荐答案

问题似乎在这里:

NSSortDescriptor *sort = [[NSSortDescriptor alloc]
                          initWithKey:@"imageData" ascending:NO];

您无法对二进制数据"属性进行排序.取得的结果控制器需要排序描述符,因此您应该使用其他属性,例如字符串,数字或日期.例如

You cannot sort on a "Binary Data" attribute. A fetched results controllerneeds a sort descriptor, so you should use a different attribute, e.g. a string, number or date. For example

NSSortDescriptor *sort = [[NSSortDescriptor alloc]
                          initWithKey:@"creationDate" ascending:NO];

其中"creationDate"是一个属性(类型为"Date"),并在创建对象时进行设置:

where "creationDate" is an attribute (type "Date") and set when the object is created:

[timeTravelFeed setValue:imageData forKey:@"imageData"];
[timeTravelFeed setValue:[NSDate date] forKey:@"creationDate"];

这篇关于将NSFetchedResultsController添加到项目后的问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-28 03:24