Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

AndroidExecutionScope doesn't work #38

Closed
jamchamb opened this issue Sep 9, 2014 · 15 comments
Closed

AndroidExecutionScope doesn't work #38

jamchamb opened this issue Sep 9, 2014 · 15 comments
Assignees
Labels

Comments

@jamchamb
Copy link

jamchamb commented Sep 9, 2014

I'm trying to get certain callbacks to run in the background by using AndroidDeferredManager and Android****Callback functions that return AndroidExecutionScope.BACKGROUND in getExecutionScope(), but no matter what the callbacks always seem to execute on the main UI thread.

This is the test I'm using to see which thread the callback is executing on:

  if (Looper.myLooper() == Looper.getMainLooper()) {
    Log.e(TAG, "bg thread on main"); 
  } else {
     Log.d(TAG, "bg thread on bg");
  }

Even using DefaultDeferredManager, the callbacks run in the main UI thread. The only thing that does work is the DeferredAsyncTask passed to the AndroidDeferredManager, but I would like to be able to use the callbacks.

If this is not a bug, please provide more documentation/examples on how to use these callbacks on Android.

@jamchamb jamchamb changed the title Android: callbacks not running in background AndroidExecutionScope doesn't work Oct 24, 2014
@saturnism
Copy link
Member

hiya - what version are you using?

thanks!

@jamchamb
Copy link
Author

jdeferred-android-aar v1.2.3

@saturnism
Copy link
Member

@jamchamb could I trouble you to provide a full example? thanks!

@saturnism
Copy link
Member

mm, i can't seem to reproduce this. any additional info would be great. thanks!

@saturnism saturnism self-assigned this Jan 12, 2015
@jamchamb
Copy link
Author

To clarify, I'm trying to initiate the promise from a UI (main) thread and have my callback functions run in a background thread. For example, I would have something like this in an Activity class:

AndroidDeferredManager dm = new AndroidDeferredManager();
dm.when(somePromise, AndroidExecutionScope.BACKGROUND).done(new DoneCallback<Something>() {
    @Override
    public void onDone(Something result) {
        // Stuff I want to be handled in a background thread
        if (Looper.myLooper() == Looper.getMainLooper()) Log.e(TAG, "On main thread");     
    }
});

I can write up a full working example later.

@saturnism
Copy link
Member

I see. Let me give that a try as well. I've used AndroidDoneCallback and that seemed to work just fine as well..

@saturnism
Copy link
Member

Oh, btw, how did you construct the somePromise?

@sandrocsimas
Copy link

The same here, callback is always executed in MAIN THREAD.
I'm using AndroidDeferredObject with AndroidExecutionScope.BACKGROUND.

@sandrocsimas
Copy link

sandrocsimas commented May 13, 2016

@saturnism, i'm testing this way:

public class DefaultPromise<D> extends AndroidDeferredObject<D, Exception, Progress> {

  public DefaultPromise() {
    super(new DeferredObject<D, Exception, Progress>(), AndroidExecutionScope.BACKGROUND);
  }
}

I create a class that handle responses from my backend server called RequestPromise

public class RequestPromise<D> extends DefaultPromise<D> {

  private RequestBuilder builder;

  public RequestPromise(RequestBuilder<?> builder) {
    this.builder = builder;
    execute();
  }

  private void execute() {
    final Response.Listener<D> listener = new Response.Listener<D>() {
      @Override
      public void onResponse(D response) {
        resolve(response);
      }
    };
    final Response.ErrorListener errorListener = new Response.ErrorListener() {
      @Override
      public void onErrorResponse(VolleyError error) {        
        reject(new Exception());
      }
    };
    builder.withListener(listener);
    builder.withErrorListener(errorListener);
    builder.execute();
  }
}

Activity:

// Configure request
RequestBuilder builder = new RequestBuilder();
// Create promise
RequestPromise promise = new RequestPromise(builder);
promise.then(new DoneCallback<Object>() {
  @Override
  public void onDone(Object obj) {
    // Here the code is running on UI thread instead of a background thread
    if (Looper.myLooper() == Looper.getMainLooper()) {
      Log.e(TAG, "On main thread");
    }
  }
}).fail(new FailCallback<Exception>() {
  @Override
  public void onFail(Exception e) {

  }
});

@sandrocsimas
Copy link

sandrocsimas commented May 13, 2016

Looking the code on AndroidDeferredObject:

protected void triggerDone(DoneCallback<D> callback, D resolved) {
    if (determineAndroidExecutionScope(callback) == AndroidExecutionScope.UI) {
        executeInUiThread(MESSAGE_POST_DONE, callback, State.RESOLVED, resolved, null, null);
    } else {
        super.triggerDone(callback, resolved);
    }
}

The "else" code should not ensure that the code will run on background?

@roastduck
Copy link

I found that in DefaultDeferredManager, only Callables and Runnables are submitted to the ExecutorService to be executed in other threads. So I guess normal Promises can only be run from main thread? Am I right? Then how should I get Promises run asynchronously?

@saturnism
Copy link
Member

A Promise doesn't run - it's just a way to pass the state. I.e., dm.when(p) is simply waiting for the promise state to change. Something else would've executed (preferably in a different thread), uses a DeferredObject to pass signal, and returns the Promise to the caller.

@roastduck
Copy link

How should I run that Promise?

@saturnism
Copy link
Member

A promise isn't something you can run. Think of it as a Future. I.e., you can't run a Future. Something else runs and returns a promise to the caller. When that task finishes, it signals the promise.

For an example of this, please see https://github.com/jdeferred/jdeferred#runnable-and-callable and the "using your own Deferred object". Thanks!

@saturnism
Copy link
Member

Closing for now. Thanks!

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

No branches or pull requests

4 participants