简体   繁体   English

当我从单独的viewController中删除CoreData实体时,为什么要调用UITableView方法?

[英]Why are UITableView methods being called when I delete CoreData Entities from a separate viewController?

I have an iOS app that is built on a tabBarViewController . 我有一个基于tabBarViewController构建的iOS应用。 The App utilizes Restkit to pull data from a Restful web service which is persisted in CoreData . 该应用程序利用Restkit从持久性Web服务中提取数据,该Web服务CoreDataCoreData In the event a user wants to logout of the app, I have a logout button that triggers a method to delete all objects in the CoreData entities. 如果用户要注销该应用程序,我有一个注销按钮,该按钮触发一种方法来删除CoreData实体中的所有对象。 The main/home tab in the app is a UICollectionView . 应用程序中的主/主页选项卡是UICollectionView The second tab has a UITableView , and the last tab has a static table with two cells. 第二个选项卡具有UITableView ,最后一个选项卡具有包含两个单元格的静态表。 One of the cells in the last tab allows the user to log out of the app. 最后一个标签中的一个单元格允许用户注销该应用程序。

By chance I noticed that when I logout, the numberOfSections and numberOfRows methods of the previous viewController I had open are getting triggered when I delete the entity that pertains to them. 偶然地,我注意到当我注销时,当我删除与它们有关的实体时,我之前打开的viewController的numberOfSectionsnumberOfRows方法会被触发。 For example my collectionView is populated with data persisted in the Gist entity. 例如,我的collectionView填充了Gist实体中Gist数据。 If the user was viewing the collectionView and navigated to the settings tab and chose to logout, my deleteAllEntitiesForName method will then be called. 如果用户正在查看collectionView并导航到设置选项卡并选择注销,则将调用我的deleteAllEntitiesForName方法。 When the method is told to delete the objects in the Gist entity, for some reason the numberOfSections and numberOfRows methods are called in the viewController for the collectionView! 当告知该方法删除Gist实体中的对象时,出于某种原因,在collectionController的viewController中调用了numberOfSectionsnumberOfRows方法! The same will happen in the UITableViewController if that is where the user navigated from. 如果这是用户从中导航的,则UITableViewController中也会发生相同的情况。

Why are methods from a totally separate view controller being hit when I am just clearing out data? 当我清除数据时,为什么会击中完全独立的视图控制器中的方法?

I am using NSLog to be notified that these functions are getting triggered. 我正在使用NSLog来通知这些功能正在触发。

Even stranger, if I login and logout consecutively the methods will be called once for each time I have logged out. 甚至更陌生,如果我连续登录和注销,则每次注销后该方法将被调用一次。 Here is the proof from my console.... 这是我的控制台上的证明。

First Logout after a build and run in the simulator: 构建后首次退出并在模拟器中运行:

2014-01-16  delete Gist
2014-01-16  Check numberOfSectionsInCollectionView
2014-01-16  Check numberofItemsInSection
2014-01-16  delete ActivityData
2014-01-16  delete FollowActivityData
2014-01-16  delete InsertNodes

Login and log back out while still running the app in the simulator: 登录并注销,同时仍在模拟器中运行该应用程序:

2014-01-16  delete Gist
2014-01-16  Check numberOfSectionsInCollectionView
2014-01-16  Check numberofItemsInSection
2014-01-16  Check numberOfSectionsInCollectionView
2014-01-16  Check numberofItemsInSection
2014-01-16  delete ActivityData
2014-01-16  delete FollowActivityData
2014-01-16  delete InsertNodes

Here is the method I am using to clear the data persisted in the entities. 这是我用来清除实体中保留的数据的方法。

- (void) deleteAllEntitiesForName:(NSString*)entityName {
    NSEntityDescription *entityDescription = [NSEntityDescription entityForName: entityName inManagedObjectContext:managedObjectContext];
    NSFetchRequest *request = [[NSFetchRequest alloc] init];
    [request setEntity:entityDescription];
    NSError *error = nil;
    NSArray *array = [managedObjectContext executeFetchRequest:request error:&error];
    if (array != nil) {
        for(NSManagedObject *managedObject in array) {
            [managedObjectContext deleteObject:managedObject];
        }
        error = nil;
        [managedObjectContext save:&error];
    }

}

I can't say that I notice any problems with the way the app runs. 我不能说我注意到应用程序运行方式上的任何问题。 I've confirmed that all the data is cleared from the core data entities. 我已经确认从核心数据实体中清除了所有数据。 But I am concerned that I may be doing something wrong here. 但是我担心我在这里可能做错了什么。 The especially concerning part is that the methods are triggered once for every time the user has logged out. 特别值得关注的部分是,每次用户注销时,方法都会触发一次。

Does anyone have any idea what is going on here? 有人知道这里发生了什么吗? Should I be concerned? 我应该担心吗? How do I handle this? 我该如何处理? Thanks for the input! 感谢您的输入!

I believe I got the deleteAllEntitiesForName method from here Delete/Reset all entries in Core Data? 我相信我从这里得到了deleteAllEntitiesForName方法删除/重置核心数据中的所有条目?

=========== UPDATE ============ =========== 更新 ============

The collectionView and tableView are populated using a FetchedResultsController. 使用FetchedResultsController填充collectionView和tableView。 Here is my implementation along with commentary of some of some of my findings. 这是我的实现以及对一些发现的评论。

- (void)controller:(NSFetchedResultsController *)controller didChangeObject:(id)anObject
   atIndexPath:(NSIndexPath *)indexPath forChangeType:(NSFetchedResultsChangeType)type
  newIndexPath:(NSIndexPath *)newIndexPath
{
    NSLog(@"CHECK didChangeObject");
    //THIS METHOD IS TRIGGERED FIRST AFTER THE DELETE HAS STARTED.
    //IT IS TRIGGERED ONCE FOR EVERY CELL IN THE COLLECTION OR ONCE FOR EVERY ROW IN THE TABLE
}

- (void)controllerDidChangeContent:(NSFetchedResultsController *)controller
{
    NSLog(@"CHECK didChangeContent");
    //THIS METHOD IS TRIGGERED NEXT.  IT IS TRIGGERED ONCE AFTER ALL THE OBJECT DATA WAS DELETED IN THE METHOD ABOVE.
}

- (NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView{
    NSLog(@"Check numberOfSectionsInCollectionView");
    return [[self.fetchedResultsController sections] count];
    //THIS METHOD IS THEN TRIGGERED FOLLOWED BY THE FETCHED RESULTS CONTROLLER
}

- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section {
    NSLog(@"Check numberofItemsInSection");
    id <NSFetchedResultsSectionInfo> sectionInfo = [self.fetchedResultsController sections][section];
    return [sectionInfo numberOfObjects];
    //FOLLOWING THE FRC THIS METHOD IS CALLED AND ONCE AGAIN THE FRC IS TRIGGERED
}

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
    static NSString *CellIdentifier = @"Cell";
    //THIS IS WHERE GUTS OF THE CELLS ARE POPULATED.  I LEFT IT OUT FOR BREVITY.
    //THIS METHOD IS NEVER TRIGGERED WHEN CLEARING OUT THE CORE DATA.
}

You might (guessing, since you don't say) be using a FetchedResultsController as the data source for your tables. 您可能(猜测,因为您没有说)正在使用FetchedResultsController作为表的数据源。

If so, be aware that FRC objects are "active" in the sense that they proactively respond to changes in their data (the Core Data contents they are linked to). 如果是这样,请注意FRC对象是“活动的”,即它们会主动响应其数据(链接到的核心数据内容)的更改。 If you change data out from under them, they notice and respond. 如果您从它们下面更改数据,它们将引起注意并做出响应。 And if you have them linked as data sources for UITableViews, they're going to tell the table views about it. 而且,如果您将它们链接为UITableViews的数据源,则它们将告诉表视图有关它。

If you'd care to post some details about how your data is linked from core data to your table views, we could better help you understand why Core Data changes are generating messages to the TableView. 如果您想发布一些有关如何将数据从核心数据链接到表视图的详细信息,我们可以更好地帮助您了解为何核心数据更改会导致向TableView生成消息。

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM