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

AndroidDeferredObject running callbacks in background thread #33

Closed
korkag opened this issue Jun 24, 2014 · 7 comments
Closed

AndroidDeferredObject running callbacks in background thread #33

korkag opened this issue Jun 24, 2014 · 7 comments
Labels

Comments

@korkag
Copy link

korkag commented Jun 24, 2014

In Android I want to be able to run callbacks in background thread when resolve called from UI thread and AndroidExecutionScope.BACKGROUND passed in constructor. For example if i run this code in UI thread:

DeferredObject deferred = new DeferredObject();
AndroidDeferredObject androidDeffered = new
AndroidDeferredObject(deferred,AndroidExecutionScope.BACKGROUND);

androidDeffered.then(new DoneCallback(){
// currently run in UI thread, suppose to be running in background thread
});

androidDeffered.resolve("Done");

I don't want to use AndroidDefferedManager, since i want to return to my application Promise without done method and trigger it via resolve function.

@saturnism
Copy link
Member

@korkag let me look into that.. but if you always want to do things in the background, you should be able to simply use the DefaultDeferredManager.

@korkag
Copy link
Author

korkag commented Jun 25, 2014

No, sometimes i want to be able to run it in background, sometimes in UI thread (android).
There's AndroidDefferedManager which using when(...) with AndroidAsyncTask. I don't want to use "when", instead I want to trigger the Promise via resolve function.

Workaround, i extended from AndroidDeferredObject and did override trigger functions like:

@OverRide
protected void triggerDone(final DoneCallback callback,final D resolved) {
if (determineAndroidExecutionScope(callback) == AndroidExecutionScope.UI || Looper.myLooper() != Looper.getMainLooper()) {
super.triggerDone(callback, resolved);
} else {
new Thread(new Runnable() {
@OverRide
public void run() {
callback.onDone(resolved);
}
}).start();
}
};

Thanks

@saturnism
Copy link
Member

gotcha - i think that's a bit more tricky...

@korkag
Copy link
Author

korkag commented Aug 11, 2014

Any progress? Do you want me to help? Lets discuss about implementation.
I have another question/feature request - to add cancel functionality/state to the promise. When you cancel promise, no done/fail callbacks are executed. Is it possible to add?

@saturnism
Copy link
Member

absolutely would be happy to discuss and help is definitely welcomed.

cancellation is a bit tricky - there was an open issue on this, since the thread will need to handle cancellation properly. when not handled properly, it will cause issues.

@korkag
Copy link
Author

korkag commented Aug 12, 2014

Are you using this library in your projects?

On Mon, Aug 11, 2014 at 6:08 PM, Ray Tsang notifications@github.com wrote:

absolutely would be happy to discuss and help is definitely welcomed.

cancellation is a bit tricky - there was an open issue on this, since the
thread will need to handle cancellation properly. when not handled
properly, it will cause issues.


Reply to this email directly or view it on GitHub
#33 (comment).

@saturnism
Copy link
Member

of course =) but it is opensource after all, any contribution for needs/fixes are welcomed.

i don't think that spawning new threads w/o an executor service is a general enough use case. I'm happy that you were able to extend it to fit your need.

it is a bit tricky.

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

2 participants