MonoTouch 确定性处理单元 [英] MonoTouch deterministic disposing cells

查看:18
本文介绍了MonoTouch 确定性处理单元的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

In our current app we need to perform some cleanup in the Dispose() of our custom UITableViewCells.

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

Unfortunately even though we call TableView.Dispose() our cells won't immediately get disposed. Instead, later on, the finalizer will call Dispose(false). Since we are called by the finalizer we shouldn't perform our cleanup here: objects might be in a unpredictable state and the time when it occurs can't be determined.

来自微软文档:

当 GC 检测到一个对象符合收集条件时,会调用终结器.这种情况发生在不再需要资源后的某个不确定时间段.

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.

还有:

如果该方法是从终结器调用的(disposing 为 false),则不应访问其他对象.原因是对象以不可预测的顺序完成,因此它们或它们的任何依赖项可能已经完成.

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() 方法?

Is there a solution to have the Dispose() method of our custom cells be called when the tableview is disposed?

推荐答案

如果你真的需要在对表格本身调用 Dispose 时对每个单独的单元格调用 Dispose,你需要自己处理:

If you really need to call Dispose on each individual cell when Dispose is called on the table itself, you need to handle this yourself:

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

这篇关于MonoTouch 确定性处理单元的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆