Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/ios/94.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ios Objective C-获取结果控制器在插入后获取较少的对象_Ios_Objective C_Uitableview_Nsfetchedresultscontroller_Reloaddata - Fatal编程技术网

Ios Objective C-获取结果控制器在插入后获取较少的对象

Ios Objective C-获取结果控制器在插入后获取较少的对象,ios,objective-c,uitableview,nsfetchedresultscontroller,reloaddata,Ios,Objective C,Uitableview,Nsfetchedresultscontroller,Reloaddata,我在iOS应用程序中使用NSFetchResultController有一个聊天面板。取数过程如下: - (NSFetchedResultsController *)fetchedResultsController { if (_fetchedResultsController != nil) { return _fetchedResultsController; } DataSource *dataSource = [DataSource getIns

我在iOS应用程序中使用NSFetchResultController有一个聊天面板。取数过程如下:

- (NSFetchedResultsController *)fetchedResultsController
{
    if (_fetchedResultsController != nil) {
        return _fetchedResultsController;
    }

    DataSource *dataSource = [DataSource getInstance];
    [NSFetchedResultsController deleteCacheWithName:@"MessageList"];
    NSFetchRequest *fetchRequest = [[NSFetchRequest alloc] init];

    NSEntityDescription *entity = [NSEntityDescription entityForName:_CHAT_MESSAGE_ENTITY_ inManagedObjectContext:dataSource.managedObjectContext];
    [fetchRequest setEntity:entity];
    [fetchRequest setFetchBatchSize:20];

    //Predicate
    DataClass *dataClass = [DataClass getInstance];
    NSPredicate *predicate;
    if ([self.chatType intValue] == SINGULAR_CHAT_TYPE) {

        Contact *receiver = [self.members objectAtIndex:0];

        predicate = [NSPredicate predicateWithFormat:@"(senderUid LIKE %@ AND receiverUid LIKE %@) OR (senderUid LIKE %@ AND receiverUid LIKE %@)", [dataClass getKeychainValueWithKey:_USER_ID_IDENTIFIER_], receiver.contactUid, receiver.contactUid, [dataClass getKeychainValueWithKey:_USER_ID_IDENTIFIER_]];
    }
    else { // group chat

    }

    [fetchRequest setPredicate:predicate];

    NSSortDescriptor *sortDescriptor = [[NSSortDescriptor alloc] initWithKey:@"messageDate" ascending:YES];
    NSArray *sortDescriptors = @[sortDescriptor];

    [fetchRequest setSortDescriptors:sortDescriptors];

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

    NSError *error = nil;
    if (![self.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.
        NSLog(@"%@:Unresolved error in fetchedResultsController performFetch %@List, %@", _CHAT_MESSAGE_ENTITY_,error, [error userInfo]);
        //abort();
    }

    return _fetchedResultsController;
}

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

- (void)controller:(NSFetchedResultsController *)controller didChangeSection:(id <NSFetchedResultsSectionInfo>)sectionInfo
           atIndex:(NSUInteger)sectionIndex forChangeType:(NSFetchedResultsChangeType)type
{
    switch(type) {
        case NSFetchedResultsChangeInsert:
            [self.chatTableView  insertSections:[NSIndexSet indexSetWithIndex:sectionIndex] withRowAnimation:UITableViewRowAnimationFade];
            break;

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

        default:
            return;
    }
}

- (void)controller:(NSFetchedResultsController *)controller didChangeObject:(id)anObject
       atIndexPath:(NSIndexPath *)indexPath forChangeType:(NSFetchedResultsChangeType)type
      newIndexPath:(NSIndexPath *)newIndexPath
{
    UITableView *tableView = self.chatTableView;

    switch(type) {
        case NSFetchedResultsChangeInsert:
            [tableView insertRowsAtIndexPaths:@[newIndexPath] withRowAnimation:UITableViewRowAnimationFade];
            break;

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

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

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

- (void)controllerDidChangeContent:(NSFetchedResultsController *)controller
{
    [self.chatTableView reloadData];
    [self.chatTableView  endUpdates];
}
保存后,我会告诉我的聊天视图控制器:

- (void)refreshPage {

    [self.fetchedResultsController performFetch:nil];
    [self.chatTableView reloadData];
    [self takeTableContentsUp];
}
我是否缺少有关获取结果控制器的信息?为什么会发生此问题?我如何解决它

谢谢大家!

编辑:如果我删除了保存消息中的“聊天”实体,只需保存消息即可,聊天面板中的everyting is ok。因此,问题是多个实体相互作用。
我应该如何编辑和保存2个实体到核心数据,并毫无问题地获取数据?

可能不相关,但不要在BeginUpdate/endUpdates块中重新加载数据-这是不必要的,因为相关更新应该通过didChangeObject中的insertRowsAtIndexPaths、deleteRowsAtIndexPaths等进行。你的意思是,我应该删除“结束更新“我不应该吗?我是这么想的,但不确定。谢谢:)@pbasdfNo,从
controllerDidChangeContent
中删除
reloadData
。。。。但是保持
beginUpdates
endUpdates
有趣。我看到很多人用这个。好的,我把它取下来了。多谢各位@PBASDF可能不相关,但不要在BeginUpdate/endUpdates块中重新加载数据-这是不必要的,因为相关更新应该通过didChangeObject中的insertRowsAtIndexPaths、deleteRowsAtIndexPaths等进行。你的意思是,我应该删除“end updates”,不是吗?我是这么想的,但不确定。谢谢:)@pbasdfNo,从
controllerDidChangeContent
中删除
reloadData
。。。。但是保持
beginUpdates
endUpdates
有趣。我看到很多人用这个。好的,我把它取下来了。多谢各位@pbasdf
- (void)refreshPage {

    [self.fetchedResultsController performFetch:nil];
    [self.chatTableView reloadData];
    [self takeTableContentsUp];
}