c# - MonoTouch 确定性处理单元

标签 c# ios xamarin.ios xamarin

在我们当前的应用程序中,我们需要在自定义 UITableViewCells 的 Dispose() 中执行一些清理操作。

不幸的是,即使我们调用 TableView.Dispose(),我们的单元格也不会立即被处理。相反,稍后,终结器将调用 Dispose(false)。 由于我们是由终结器调用的,所以我们不应该在这里执行清理:对象可能处于不可预测的状态,并且无法确定它发生的时间。

来自微软文档:

The finalizer is called when the GC detects that an object is eligible for collection. This happens at some undetermined period of time after the resource is not needed anymore.

还有:

If the method is invoked from the finalizer (disposing is false), other objects should not be accessed. The reason is that objects are finalized in an unpredictable order and so they, or any of their dependencies, might already have been finalized.

是否有解决方案可以在释放 tableview 时调用自定义单元格的 Dispose() 方法?

最佳答案

如果在表本身上调用 Dispose 时确实需要对每个单元格调用 Dispose,则需要自己处理:

  1. 子类 UITableView。
  2. 将您使用的所有单元格存储在此类的单元格列表中。
  3. 重写 View 上的 Dispose,并对列表中的每个单元格调用 Dispose。

关于c# - MonoTouch 确定性处理单元,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24019899/

相关文章:

c# - 使用 Word 字典检查某个单词是否存在

ios - 在 IOS 中使用 NSPredicate 按数组的一个元素过滤数组的 NSArray

iphone - 这两个 NSString 方法之间的区别

c# - 调用 FinishedPlaying 事件时如何处理 AVAudioPlayer?

c# - 为什么要调用 Dispose()?不会发生内存泄漏?

C# 在标签之间抓取 HTML

c# - 我可以在 Xamarin.iOS 项目中使用 T4 文本生成吗?

c# - Xamarin iOS ObserveOrientationDidChange 横向或纵向

c# - 带有 ASP-NET 和 Angular 的 Visual Studio 在浏览器中显示 TimeoutException

ios - 如何打开从一个 UIWebView 到另一个 UIWebView 的链接?