简体   繁体   English

当UITableView是UIView的子视图时(数据最初加载),未调用NSFetchedResultsControllerDelegate方法

[英]NSFetchedResultsControllerDelegate methods not called when UITableView is a subview of UIView (data initially loads)

I have a UIView that contains a UITableView . 我有一个UIView包含UITableView These are managed by a UIViewController that inherits the delegate methods UITableViewDelegate and NSFetchedResultsControllerDelegate . 这些由继承自委托方法UITableViewDelegateNSFetchedResultsControllerDelegateUIViewController管理。 I am able to populate the table just fine using the NSFetchedResultsController , however the delegate methods (specifically controllerWillChangeContent ) are not called when changes are made to managed objects in the fetchObjects . 我可以使用NSFetchedResultsController很好地填充表,但是在对fetchObjects托管对象进行更改时,不会调用委托方法(特别是controllerWillChangeContent )。

I have checked independently that changes are made to the objects, and those changes I have reflected in the cells, however you must manually reload the cells (scrolling up) to see the changes occur. 我已经独立检查了对对象所做的更改,这些更改已反映在单元格中,但是您必须手动重新加载单元格(向上滚动)才能看到更改。 The behavior I am looking for is that when the changes are made to the object (via save), that the NSFetchedResultsControllerDelegate methods fire so I can make the updates to the table view. 我正在寻找的行为是,当对对象进行更改(通过保存)时,将触发NSFetchedResultsControllerDelegate方法,以便可以对表视图进行更新。

Just to be clear, the data is populated from the database when the view loads, so I know the code pulls that just fine. 需要明确的是,在加载视图时,数据是从数据库中填充的,所以我知道代码可以正常运行。

Header interface 头接口

@interface HSAwardsViewController : UIViewController <UITableViewDelegate, UITableViewDataSource, NSFetchedResultsControllerDelegate>

@property (nonatomic, retain) IBOutlet UITableView *tableView;

@property (nonatomic, retain) NSPersistentStoreCoordinator *persistentStoreCoordinator;
@property (nonatomic, retain) NSFetchedResultsController *fetchedResultsController;
@property (nonatomic, retain) NSManagedObjectContext *managedObjectContext;

@end

init method - Setting up managedObjectContext (uses Magical Record) 初始化方法-设置ManagedObjectContext(使用Magical Record)

- (id)initWithCoder:(NSCoder *)aDecoder
{
    self = [super initWithCoder:aDecoder];
    if (self) {
        HSInternalDB *dbInstance = [HSInternalDB getInternalDB];
        self.persistentStoreCoordinator = [dbInstance getPersistentStoreCoordinator];
        self.managedObjectContext = [NSManagedObjectContext MR_contextWithStoreCoordinator:self.persistentStoreCoordinator];

        self.tableView.delegate = self;
        self.tableView.dataSource = self;

        NSError *error;
        if (![[self fetchedResultsController] performFetch:&error]) {
            // Update to handle the error appropriately.
            NSLog(@"Unresolved error %@, %@", error, [error userInfo]);
            exit(-1);  // Fail
        }
    }
    return self;
}

Setting up the NSFetchedResultsController and assigning the delegate 设置NSFetchedResultsController并分配委托

- (NSFetchedResultsController *)fetchedResultsController {

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

    NSFetchRequest *fetchRequest = [NSFetchRequest fetchRequestWithEntityName:@"Award"];

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

    [fetchRequest setSortDescriptors:[NSArray arrayWithObject:sort]];
    [fetchRequest setFetchBatchSize:20];

    NSFetchedResultsController *theFetchedResultsController = [[NSFetchedResultsController alloc] initWithFetchRequest:fetchRequest managedObjectContext:self.managedObjectContext sectionNameKeyPath:nil cacheName:nil];

    NSError *error = nil;
    [theFetchedResultsController performFetch:&error];
    theFetchedResultsController.delegate = self;
    self.fetchedResultsController = theFetchedResultsController;

    // Listen for this notification so that the managedObjectContext runs on the main thread
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(contextChanged:) name:NSManagedObjectContextDidSaveNotification object:nil];

    return _fetchedResultsController;
}



- (void)contextChanged:(NSNotification*)notification
{
    NSManagedObjectContext *context = [notification object];
    if ([context isEqual:self.managedObjectContext]) {
        return;
    }

    if (![NSThread isMainThread]) {
        [self performSelectorOnMainThread:@selector(contextChanged:) withObject:notification waitUntilDone:YES];
        return;
    }

    [[self managedObjectContext] mergeChangesFromContextDidSaveNotification:notification];
}

As far as I can see, all the connections are are made in code or in the storyboard. 据我所知,所有连接都是通过代码或情节提要进行的。 I know a change is recorded in my database because I am listening for the NSManagedObjectContextDidSaveNotification . 我知道数据库中记录了更改,因为我正在监听NSManagedObjectContextDidSaveNotification

This has me stumped, so I appreciate any suggestions or bread crumbs. 这让我感到难过,因此,我感谢任何建议或面包屑。 I'm happy to answer any questions or post more code if needed! 我很乐意回答任何问题或在需要时发布更多代码!

contextChanged:应该在MainThread上调用,因为它涉及UI更新。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 NSFetchedResultsControllerDelegate委托方法未调用 - NSFetchedResultsControllerDelegate delegates methods are not called 未调用cellForRowAtIndexPath,但为UITableView调用的numberofRowsInSection已添加为UIView的子视图 - cellForRowAtIndexPath not called but numberofRowsInSection called for UITableView added as subview to UIView 最初将UIView添加为子视图很慢 - Adding a `UIView` as a subview is initially slow 未从UIView调用的UITableView委托方法 - UITableView delegate methods not called from UIView UIView内部的UITableView-仅在uitableview外部滚动单元时才加载数据 - UITableView inside UIView - data loads only when scrolling cells outside uitableview 带有UITableView作为子视图的UIView - UIView with UITableView as subview 当UITableview是Custom UIView的子视图时,处理UITableView委托 - Handling UITableView delegates when the UITableview is a subview of a Custom UIView 与实体创建关系时,为什么我的NSFetchedResultsControllerDelegate方法会被随机调用? - Why is my NSFetchedResultsControllerDelegate methods get called randomly when creating a relationship with my entity? 将UITableview添加为UIView的子视图时遇到问题 - Trouble in adding a UITableview as subview of UIView UITableView最初不加载数据 - UITableView Not Loading Data Initially
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM