Forum Xamarin.iOS

How to use WeakReference?

mrtnkrstnmrtnkrstn ZABeta ✭✭

The Advanced Memory Management session at Evolve really opened my eyes. I have tried to use WeakReference<T> in my code:

public WeakReference <MyParentClass> parent;

but get an error "The non-generic type `System.WeakReference' cannot be used with the type arguments".

What am I doing wrong?

Have to add, this code does not generate the error:

public WeakReference parent;

but then I can't access any of the MyParentClass methods/properties.

Can anyone help me? Struggling to find any documentation on WeakReference.

Best Answers

Answers

  • mrtnkrstnmrtnkrstn ZABeta ✭✭
    edited July 2013

    @AlexandrePalma, thank you, that is the second time in as many days that you have helped me :) Can you perhaps point me to any WeakReference documentation?

    Also, any idea why WeakReference<T> generates an error?

  • mrtnkrstnmrtnkrstn ZABeta ✭✭

    @AlexandrePalma thanks, did not realize it was part of the C# library, thought it was specific to Xamarin :|

  • AlexPalmaAlexPalma USMember ✭✭

    No problem, as I said this prb is not relative to xamarin, you will have the same issues if you are doing a windows forms wpf or anyother application written in .net or Java, the diference is that in the mobile world you have less resources and the OS is not that forgiven as Windows is.

  • PedroSilva.4631PedroSilva.4631 USUniversity

    @AlexPalma Could you create an example of that WeakReference use please? I im having issues implementing it, it would be very nice to have a working example

  • AlexPalma72AlexPalma72 PTMember

    @PedroSilva.4631 ,
    ok I made some changes to it since I posted here, you can see latest at https://github.com/AlexPalma72/MyMvvm/blob/master/MyMvvm/WeakEventManager.cs

    so the way you do it is imagine you are inside a method of a View, Page, Controller, etc...
    so you will do the following.
    this.WeakSubscriptions.Add(this.WeakSubscribe(h => this.OkButton.TouchUpInside += h,
    h => this..OkButton.TouchUpInside -= h,
    (s, e) => {s.ViewModel.MethodToRunOnOKClick()}));

    then on the dispose of the View you iterate over the WeakSubsriptions List and dispose each one, if you don't do that then the action handler will stay in memory until the GC runs and cleans it.

    Another thing to have in mind is that inside the handler anonymous method you shouldn't refer to this or anything ouside of that method since that will create a strong reference defeating the purpose of this. instead use the s, where s is the object that you did the weak subscribe.

  • gustaveracostagustaveracosta GBMember ✭✭
    public class MyFooDelegate : FooDelegate {
        WeakReference<MyViewController> controller;
        public MyFooDelegate (MyViewController ctrl) => controller = new WeakReference<MyViewController> (ctrl);
        public void CallDoSomething ()
        {
            MyViewController ctrl;
            if (controller.TryGetTarget (out ctrl)) {
                ctrl.DoSomething ();
            }
        }
    }
    
Sign In or Register to comment.