如何打破视图控制器和数据源之间的参考周期 [英] How to break reference cycle between view controller and data source

查看:126
本文介绍了如何打破视图控制器和数据源之间的参考周期的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

考虑以下简单示例:

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;
    }
}

我注意到视图控制器(TableViewController)从未发布.表视图控制器具有对数据源的引用,但是数据源也具有对表视图控制器的引用.

I've noticed that the view controller (TableViewController) is never released. The table view controller has a reference to the data source, but the data source also has a reference to the table view controller.

使用TableView.Source = new TableSource();释放视图控制器,使用TableView.Source = new TableSource(this);释放视图控制器.

With TableView.Source = new TableSource(); the view controller gets released, with TableView.Source = new TableSource(this); it's not.

该参考周期应如何打破,以使所有内容都得到释放?

How should this reference cycle be broken so that everything get released?

现在我尝试了WeakReference:

当视图控制器从导航堆栈中弹出时,通过使用WeakReference调用Dispose方法.

Through using a WeakReference the Dispose method is called, when the view controller is popped off the navigation stack.

ViewDidLoad中:

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

在数据源中:

private WeakReference<TableViewController> controller;

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

我将其内置到我的真实项目中,但是如何访问我的 controller ?我收到消息了

I built this into my real project, but how can I access my controller? I get the message

类型'System.WeakReference'不包含'xxx'的定义,并且找不到类型'System.WeakReference'的扩展方法'xxx'.您是否缺少装配参考?

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了吗?您是否尝试过WeakReference? https://msdn.microsoft.com/zh-cn/library/system.weakreference(v = vs.110).aspx

You work with Xamarin, as I see? Have you tried WeakReference? https://msdn.microsoft.com/en-us/library/system.weakreference(v=vs.110).aspx

PS:

private WeakReference weakController; 

设置:

this.weakController = new WeakReference(controller); 

获取:

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

这篇关于如何打破视图控制器和数据源之间的参考周期的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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