[英]Why is my `configureCell:` method called every time I change the value of the object the cell corresponds to? (But I DON'T save it.)
I'm making an app where when you select a cell, you're segued to a new view for reading. 我正在创建一个应用程序,当你选择一个单元格时,你会被一个新的视图用于阅读。 The cell you tapped on corresponds to an object in the Core Data store (through
NSFetchedResultsController
) and that object is set as the value for the article
property of the view that is being segued to. 您点击的单元格对应于Core Data存储中的对象(通过
NSFetchedResultsController
),该对象被设置为正被搜索的视图的article
属性的值。
Once there, I move the position of the user in the article as they read it. 到那里之后,我会在阅读文章时移动用户在文章中的位置。 (So I alter the
position
attribute of article
via article.position = ...
) (所以我通过
article.position = ...
改变article
的position
属性)
However, this occurs very frequently as they read, often hundreds of times, and each time NSFetchedResultsController
is detecting the change as an update, then calling configureCell:
, which then runs through a bunch of configuration for that cell. 但是,这种情况在读取时经常发生,通常是数百次,每次
NSFetchedResultsController
都将更改检测为更新,然后调用configureCell:
NSFetchedResultsController
然后运行该单元的一堆配置。 As this configureCell:
method is called so often (and I only want it to be called when they go back to the table view, as that's the only time the update is needed) it's causing a decent performance loss. 由于这个
configureCell:
方法经常被调用(我只希望它们在返回到表视图时被调用,因为这是唯一需要更新的时间),这导致了相当大的性能损失。
I don't get why it's calling it though. 我不明白为什么它会调用它。 I'm not saving the data with
NSManagedObjectContext
into the Core Data store, so why does it care? 我没有使用
NSManagedObjectContext
将数据保存到Core Data存储中,为什么它关心? I only call that when viewWillDisappear
is called, indicating that they're leaving the view, likely to go back to the table view which is where I want it! 我只在调用
viewWillDisappear
时调用它,表示它们正在离开视图,可能会返回到我想要的表视图!
Basically, how do I get it to only call configureCell:
when it needs to/when I ask it to write the data to Core Data? 基本上,我如何让它只调用
configureCell:
当它需要/当我要求它将数据写入Core Data时? It's calling it hundreds of times as is. 它称之为数百次。
It doesn't matter if you're not persisting the changes to Core Data objects to disk; 如果您没有将Core Data对象的更改持久保存到磁盘,则无关紧要; a change to a Core Data managed object IS a change, and so NSFetchedResultsController acknowledges it.
对Core Data托管对象的更改是一个更改,因此NSFetchedResultsController会对其进行确认。
You could avoid updating the tableview that's not visible by setting NSFetchedResultsController's delegate to nil
in viewWillDisappear:
, and setting it back to self
in viewDidAppear:
. 通过在
viewWillDisappear:
中将viewWillDisappear:
的委托设置为nil
,并在viewDidAppear:
中将其设置回self
,可以避免更新不可见的tableview viewDidAppear:
. Also, add a performFetch:
after setting the delegate. 此外,在设置委托后添加
performFetch:
.
The "hidden" NSFetchedResultsController will still be receiving all the changes made to the article object in the view that's in the foreground, but will ignore them since it doesn't have a delegate. “隐藏的”NSFetchedResultsController仍将接收在前台视图中对文章对象所做的所有更改,但由于它没有委托,因此将忽略它们。
When going back to the articles list view, it will have a delegate again, and it will be able to react to all changes. 返回文章列表视图时,它将再次拥有一个委托,并且它将能够对所有更改做出反应。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.