c# - 如何打破 View Controller 和数据源之间的引用循环

标签 c# ios memory-management xamarin xamarin.ios

考虑这个简单的例子:

public partial class TableViewController : UITableViewController
{
    public TableViewController (IntPtr handle) : base (handle)
    {
    }

    protected override void Dispose (bool disposing)
    {
        Console.WriteLine (String.Format ("{0} controller disposed - {1}", this.GetType (), this.GetHashCode ()));

        base.Dispose (disposing);
    }

    public override void ViewDidLoad ()
    {
        //TableView.Source = new TableSource(this);
        TableView.Source = new TableSource();
    }
}

public class TableSource : UITableViewSource {

    private TableViewController controller;
    string CellIdentifier = "TableCell";

    public TableSource ()
    {

    }

    public TableSource (TableViewController controller)
    {
        this.controller = controller;
    }

    public override nint RowsInSection (UITableView tableview, nint section)
    {
        return 1;
    }

    public override UITableViewCell GetCell (UITableView tableView, NSIndexPath indexPath)
    {
        UITableViewCell cell = tableView.DequeueReusableCell (CellIdentifier);

        //if there are no cells to reuse, create a new one
        if (cell == null){
            cell = new UITableViewCell (UITableViewCellStyle.Default, CellIdentifier);
        }

        cell.TextLabel.Text = "test";

        return cell;
    }
}

我注意到 View Controller (TableViewController)从未被释放。 TableView Controller 有对数据源的引用,但数据源也有对 TableView Controller 的引用。

使用 TableView.Source = new TableSource(); View Controller 会被释放,使用 TableView.Source = new TableSource(this); 则不会。

应该如何打破这个引用循环以便释放所有内容?

编辑:

现在我尝试了WeakReference:

当 View Controller 从导航堆栈中弹出时,通过使用 WeakReference 调用 Dispose 方法。

ViewDidLoad中:

TableView.Source = new TableSource(new WeakReference<TableViewController> (this));

在数据源中:

private WeakReference<TableViewController> controller;

public TableSource (WeakReference<TableViewController> controller)
{
    this.controller = controller;
}

我将其构建到我的真实项目中,但是我如何访问我的 Controller ?我收到消息了

Type 'System.WeakReference' does not contain a definition for 'xxx' and no extension method 'xxx' of type 'System.WeakReference' could be found. Are you missing an assembly reference?

最佳答案

据我所知,您与 Xamarin 合作?您尝试过弱引用吗? https://msdn.microsoft.com/en-us/library/system.weakreference(v=vs.110).aspx

附注:

private WeakReference weakController; 

设置:

this.weakController = new WeakReference(controller); 

获取:

if (weakController.isAlive)
{
  TableViewController controller = weakController.Target as TableViewController;
}

关于c# - 如何打破 View Controller 和数据源之间的引用循环,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32140743/

相关文章:

iphone - iOS CF 数据结构与 NS 数据结构

ios - iOS 中的内存管理

c++ - 在任何情况下 new 都会返回 NULL 吗?

c# - 将来自两个互斥数据集的值合并到一个单独的数据集中

c# - DateTime.Ticks、DateTime.Equals 和时区

android - 模拟器测试与基于云的测试

iphone - 如何在使用 AVAudioPlayer 的应用程序的多任务栏中启用音频控件?

c# - WPF Banana-Like 按钮的曲线排列

c# - 防止 C# 中 HtmlButton 的回发

c++ - 当它是 noop 时是否需要调用一个非平凡的析构函数?