This repository has been archived by the owner on Jul 13, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1.3k
New async #78
Milestone
Comments
The signature of public fun <T> T.asyncResult(task: AnkoAsyncContext<T>.() -> T): Future<T> doesn't seem to make any sense to me. Why should the type of the I think the signature should be public fun <T, R> T.asyncResult(task: AnkoAsyncContext<T>.() -> R): Future<R> |
Closed
Proposed fix: public fun <T, R> T.asyncResult(task: AnkoAsyncContext<T>.() -> R): Future<R> {
val context = AnkoAsyncContext(WeakReference(this))
return BackgroundExecutor.submit { context.task() }
}
public fun <T, R> T.asyncResult(executorService: ExecutorService, task: AnkoAsyncContext<T>.() -> R): Future<R> {
val context = AnkoAsyncContext(WeakReference(this))
return executorService.submit<R> { context.task() }
} |
Looks fine! |
So the only thing that should be done is a warning in IDE:
|
|
Sign up for free
to subscribe to this conversation on GitHub.
Already have an account?
Sign in.
Current
async
requires too much responsibility from the developer.1. The main problem is that user should not call anything from the
Activity
but should be able to access all resolved widgets (and other data stored in activity properties). For example:The problem can be solved by casting
this
to the particularActivity
type. It is complicated, unsafe and doesn't work for everything except activities:2. The signature for
uiThread
is:So passing
Context
as a receiver makes things complex cause you havethis@SomeActivity
andthis
from theuiThread
receiver, these twothis
have different types though they are bothContext
. Even if you don't want to call anything outside the lambda directly, you can do it unintentionally.3. If user calls
async
in someFragment
class, he/she probably wants to do something with thatFragment
instance, and the underlyingContext
, thatuiThread
receives (as a receiver! 👎 ) is completely useless in this case.Solution:
Use cases:
Affected behaviour:
Context.uiThread()
still looks strange. The name is very short, and the function is likely to be called outsideasync
DSL. Furthermore, the name clashes with theuiThread
defined forAnkoAsyncContext
. Probably it should be renamed toContext.runOnMainApplicationThread()
or something.Possible shortcomings:
async
now is defined forAny?
.Additional info:
Anko IDEA plugin must show the warning when the lambda passed to
async
captures an underlyingActivity
/Fragment
/whatever.The text was updated successfully, but these errors were encountered: