본문 바로가기

Develop/MAUI 가이드

[Xamarin] 성능 개선 - OneTime Binding

반응형

OneTime Binding
OneTime Binding

성능 개선 - OneTime Binding

Compiled Binding 보다 성능을 더 끌어올릴 수 있는 바인딩 방식으로 OneTime Binding 이 있습니다. 이름에서 알 수 있듯이, 최초에 BindingContext가 바뀔 때만 값이 바인딩되고 INotifyPropertyChanged 이벤트를 구독하지 않는 방식입니다. 따라서 이벤트를 구독하고 처리하는 과정이 없어지므로 성능이 개선되는 원리입니다. OneTime Binding 방식이 도움이 되는 때는 UI가 처음 생성될 때만 바인딩하고 이후로는 값이 변경되지 않을 때입니다.OneTime Binding이 유용할 수 있는 사례를 아래에 소개드리도록 하겠습니다.

Command를 바인딩할 때

Command 같은 경우 메서드를 바인딩하는데, 런타임에 메서드를 교체할 일은 거의 발생하지 않습니다. 따라서 아래와 같이 OneTime Binding을 적용하면 성능을 개선할 수 있습니다.

public ICommand Create => new TinyCommand(async() => {
  await CreatePerson();
});
<Button Text="Create" Command="{Binding Create, Mode=OneTime}" />

DataTemplate을 사용할 때

경우에 따라 다르긴 하지만 DataTemplate 으로 ViewModel이 추가될 때마다 View를 생성하는 구조에서 데이터를 표시하기만 하는 경우 OneTime Binding을 적용하면 성능을 개선할 수 있습니다.

<CollectionView ItemSource="{Binding Items}">
  <CollectionView.ItemTemplate>
    <DataTemplate x:DataType="models:Item">
      <StackLayout>
        <Label Text="{Binding A, Mode=OneTime}" />
        <Label Text="{Binding B, Mode=OneTime}" />
    </DataTemplate>
    </CollectionView.ItemTemplate>
</CollectionView>

static readonly 또는 const을 바인딩할 때

static readonlyconst는 값이 변경되지 않으므로 OneTime Binding 을 사용합시다.

반응형