This project has moved and is read-only. For the latest updates, please go here.
8

Closed

WhenSourceChanges calls callback multiple times on iOS

description

I have an iOS project on Xamarin and use bindings to storyboard labels and other controls.

For example like this:
        // binding #1
        _bindings.Add(this.SetBinding(() => this.ViewModel.MyProperty, BindingMode.OneWay).WhenSourceChanges(this.MyCallback));
        // binding #2
        _bindings.Add(this.SetBinding(() => this.ViewModel.MyProperty, () => this.label1.Text));
        // binding #3
        _bindings.Add(this.SetBinding(() => this.ViewModel.MyProperty2, () => this.label2.Text));
        // binding #4
        _bindings.Add(this.SetBinding(() => this.ViewModel.MyProperty3, () => this.label3.Text));_
ViewModel implements INotifyPropertyChanged interface. Changing of MyProperty raises PropertyChanged event.

The problem is that this.MyCallback method called 4 times. Exactly the same number of times as many I have bindings. If I leave the only one binding with WhenSourceChanges the method MyCallback will be called one time. If I add another one, it will be called 5 times. One time for each added binding.

I've created the quite simple sample iOS project that reproduces this issue and attached it to this post. You can set breakpoint into callback method and make sure that it called multiply times.

Could you please investigate what the problem and fix it in next release?

Please, let me know if you have any questions. Thank you!

file attachments

Closed Feb 15, 2015 at 6:14 PM by lbugnion

comments

lbugnion wrote Feb 6, 2015 at 9:44 AM

Woah. Checking it out, I'll let you know.

lbugnion wrote Feb 6, 2015 at 2:07 PM

OK I got it. There was an issue in the PropertyChangedEventManager where the same event handler could be attached to the same sender multiple times. This is both inefficient (since the event will be handled N times) and plain wrong (since one single PropertyChanged event will cause all the listeners to react to the event anyway).

Thanks for pointing this to me. Not sure why no one noticed that before. Actually the issue was not related to iOS nor to the WhenSourceChanges method, but it would occur for all bindings. You made it more obvious., Thanks for sending the simple repro, that makes my life so much easier.

I uploaded the new DLLs here:
http://1drv.ms/1ylP1TS

Can you give it a try? I will update the Nuget package too (that will be V5.1).

Thanks!
Laurent

rgiggs wrote Feb 10, 2015 at 8:33 AM

Thank you very much for fast response and fix! I'll try to check it today.