Skip to content
This repository has been archived by the owner on Feb 26, 2023. It is now read-only.

Lifespan validation for @UiThread #1874

Open
couchcrew-thomas opened this issue Oct 11, 2016 · 5 comments
Open

Lifespan validation for @UiThread #1874

couchcrew-thomas opened this issue Oct 11, 2016 · 5 comments

Comments

@couchcrew-thomas
Copy link

This is not a bug but a discussion of a feature request.

TL;DR extend AA to execute methods only in certain life-cycle states

In my code I have to repeatedly check if a method annotated with @UiThread is not being called after the activity / fragment has been destroyed. Most of the time this occurs when a long running background operation returns to display its result.

@UiThread
protected void foo() {
  if (!isResumed()) {
     return;
  }
  // [...]
}

To prevent to write this check manually every time my idea was to add a parameter lifespan to the @UiThread annotation (Or whatever because I'm bad at naming things). Its values could be the following:

enum LIFESPAN {
    CREATED, //valid between onCreate and onDestroy
    STARTED, //valid between onStart and onStop
    RESUMED //valid between onResume and onPause
};

An annotated method would look this way:

@UiThread(lifespan = RESUMED)
protected void foo() {
  // [...] executed when activity / fragment is in resumed state
}

The generated class would have to override the onCreate... lifecycle methods to save the current state and query this state inside the generated methods that use the lifespan parameter.

The downside of this idea is that the parameter could only be used in activities or fragments and as a result the @UiThread annotation would not have the same features in all @E... anymore.
An alternate approach would be to create a new annotation similar to @IgnoreWhen that checks for the lifecycle states in @EActivity and @EFragment.

What is your opinion on this matter?

@Thohid
Copy link

Thohid commented Oct 13, 2016

@UiThread

@Thohid
Copy link

Thohid commented Oct 13, 2016

@UiThread
protected void foo() {
if (!isResumed()) {
return;
}
// [...]
}

@WonderCsabo
Copy link
Member

@couchcrew-thomas i would use a different annotation. Maybe we can extend @IgnoreWhen. @dodgex wdyt?

@dodgex
Copy link
Member

dodgex commented Nov 23, 2016

I think @IgnoreWhen is a good idea. this allows to be used on other methods than @UiThread too. e.g. networking callbacks

@WonderCsabo
Copy link
Member

@couchcrew-thomas contributions are always welcome!

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

No branches or pull requests

4 participants