How to avoid constant null checks of getView() in Mortar presenters?

235 Views Asked by At

I wonder what approach you are using to avoid repeated null checks for getView() in Mortar presenters?

1

There are 1 best solutions below

0
On

90% of the time it's not necessary because a method is happening in response to something initiated from view-land. In the cases where it is necessary, something that happens in response to some kind of asynchronous event…we just cope with it.

public void omgTheServerSaid(Some thing) {
  MyView view = getView();
  if (view == null) return;
  view.showIt(thing.it);
}

This is the kind of thing that makes me want to write in Kotlin from now on.

getView()?.showIt(thing.it);