Forum Xamarin Xamarin.iOS

Why does the value of an NSIndexPath change

DanielBurtonDanielBurton USMember
edited January 2017 in Xamarin.iOS

I have seen this problem multiple times so I created a simple app to demonstrate. Here it is:
using System;
using Foundation;
using UIKit;

namespace nsindexpathbug.iOS
{
public partial class ViewController : UIViewController
{
public ViewController(IntPtr handle) : base(handle)
{
}

    public override void ViewWillAppear(bool animated)
    {
        base.ViewWillAppear(animated);

        var indexPath1 = NSIndexPath.FromRowSection(1, 1);
        var indexPath = NSIndexPath.FromRowSection(1, 1);

        BeforeLabel.Text = $"section = {indexPath.Section} row = {indexPath.Row}";
        indexPath1.Dispose();
        AfterLabel.Text = $"section = {indexPath.Section} row = {indexPath.Row}";
    }

    public override void DidReceiveMemoryWarning()
    {
        base.DidReceiveMemoryWarning();
        // Release any cached data, images, etc that aren't in use.     
    }
}

}

and here is the results:

Best Answer

Answers

  • Daniel.1965Daniel.1965 USMember

    When I run this code to try and illustrate the same issue

    var one = NSIndexPath.FromRowSection(1, 2);
    Console.WriteLine(one.GetHashCode());
    var two = NSIndexPath.FromRowSection(1, 2);
    Console.WriteLine(two.GetHashCode());
    one.Dispose();
    Console.WriteLine(two.GetHashCode());
    

    I get this output

    2097686
    2097686
    0
    

    Which seems to indicate that NSIndexPath.FromRowSection is returning the same object when called with the same parameters.
    I also verified this by using Object.ReferenceEquals(one, two) which returns true.
    I'm not sure if Xamarin was intending this behavior, but it certainly does screw up anyone trying to follow best practices of IDisposable

Sign In or Register to comment.