简体   繁体   English

为什么每次更改单元格对应的对象的值时都会调用我的`configureCell:`方法? (但我不保存它。)

[英]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 = ...改变articleposition属性)

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.

相关问题 每当对应于该单元格的NSFetchedResultsController中的对象发生更改时,如何使单元格的外观更新? - How do I make what my cell looks like update each time there is a change to the object in NSFetchedResultsController that corresponds to the cell? 如何从自定义单元格保存UILabel,以便每次我打开应用程序时都显示最新更改? - How to save UILabel from custom cell so that every time I open the app it displays the latest change? configureCell方法在自定义单元中不起作用 - configureCell method does not work in a Custom cell 我的 TableView 数据源函数没有被调用,我不知道为什么 - My TableView datasource functions aren't being called and I don't know why 每次我获取它时,Core Data都会为同一个NSManagedObject返回一个不同的对象实例。为什么是这样? - Core Data returns a different object instance for the same NSManagedObject each time I fetch it. Why is this? 如果我的应用程序退出后,我将某些内容保存到Core Data,则每次启动后它都会崩溃。 为什么? - If after my app quits I save something to Core Data, it crashes on the following launch every time. Why? 我不知道为什么在swift3中没有调用viewForHeaderInSection - I don't know why viewForHeaderInSection is not called in swift3 为什么每次更改视图并返回视图时,数组都会自我覆盖-swift 3 - why does my array overwrite itself every time i change view and go back to it - swift 3 我不明白为什么我的对象没有收到通知 - I don't understand why my object doesn't receive notifications 将对象/结构传递给 UITableViewCell,或在 configureCell:atIndexPath:? 中配置单元格 - Pass an object/structure to a UITableViewCell, or configure the cell in configureCell:atIndexPath:?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM