Android method annotation to prevent call from GUI thread

The project I'm working on now has a lot of caching that is done on the main thread, which makes the laggy application. My plan is to make asynchronous variants of these, but still keeping synchronous calls to simplify the chain when merged into asyncTasks. The problem is that I want to somehow intuitively prohibit the use of caching functions in the GUI thread. Any ideas? Is it possible? Is it possible to mark a method with an annotation that prevents it from being called in the GUI thread?

+4
source share
3 answers

http://androidannotations.org/ provides a library that uses annotations to solve this problem. They have annotations, such as @UiThreadand @Background, which significantly replace the need for use runOnUiThread()and, AsyncTaskaccordingly.

If you're interested, here paper discusses a similar system for dealing with blocking UI thread operations from non-UI threads.

+4
source

As far as I know, there is no such possibility.

Perhaps you can try using the "State machine" using Android.Handlers

Checkout

Google Android Communication with UI Stream Using Handlers

--- > !

" android " ! , J.

, .

0

- , . AsyncTask, , , .., .

AsyncTask, , .. , , promises. JQ - Java7/Android, Java JavaScript Promises/A+. Promise - , , Future .

( JQ) , foo, bar baz:

try {
  int x = foo();
  int y = bar(x);
  return baz(y);
} catch (Exception e) {
  return -1;
} finally {
  // Always called
}

return foo().then(x -> {
  return bar(x);
}).then(y -> {
  return baz(y);
}).fail(e -> {
  return Value.wrap(-1);
}).fin({
  // Always called
});

( , , then(new OnFulfilledCallback() {...}) .., Android Studio , - Retrolambda, lambda Java8 Java7.)

foo, bar baz Promise . JQ promises :

private Promise<Integer> foo() {
    return JQ.defer(() -> {
        int result = 42;

        // do some heavy processing...
        return result;
    });
}

Note that it JQalso provides useful utilities for synchronizing multiple concurrent operations, adding timeouts to lengthy operations, etc. etc.

Disclaimer: somewhat shameless promotion as I am the author of JQ .;)

0
source

All Articles