GoogleMap suspending initializer with coroutine
up vote
0
down vote
favorite
I have created delegate to initialize GoogleMap
instance which is using coroutine. Map fragment is dispalyed properly until I invoke any method on map reference, then map hangs and stop responding.
This is my delegate:
class MapInitializer(private val setUp: GoogleMap.() -> Unit) : ReadOnlyProperty<AppCompatActivity, GoogleMap> {
var instance: GoogleMap? = null
override fun getValue(thisRef: AppCompatActivity, property: KProperty<*>): GoogleMap {
return instance ?: thisRef.loadMap().apply(setUp).also { instance = it }
}
private fun AppCompatActivity.loadMap(): GoogleMap = runBlocking {
async { registerCallback() }.await()
}
private suspend fun AppCompatActivity.registerCallback(): GoogleMap = suspendCoroutine { continuation ->
(supportFragmentManager.findFragmentById(R.id.map) as SupportMapFragment).getMapAsync {
continuation.resume(
it
)
}
}
}
I suppose that map thread is suspending but I have no idea why. Help please.
android kotlin maps coroutine
|
show 2 more comments
up vote
0
down vote
favorite
I have created delegate to initialize GoogleMap
instance which is using coroutine. Map fragment is dispalyed properly until I invoke any method on map reference, then map hangs and stop responding.
This is my delegate:
class MapInitializer(private val setUp: GoogleMap.() -> Unit) : ReadOnlyProperty<AppCompatActivity, GoogleMap> {
var instance: GoogleMap? = null
override fun getValue(thisRef: AppCompatActivity, property: KProperty<*>): GoogleMap {
return instance ?: thisRef.loadMap().apply(setUp).also { instance = it }
}
private fun AppCompatActivity.loadMap(): GoogleMap = runBlocking {
async { registerCallback() }.await()
}
private suspend fun AppCompatActivity.registerCallback(): GoogleMap = suspendCoroutine { continuation ->
(supportFragmentManager.findFragmentById(R.id.map) as SupportMapFragment).getMapAsync {
continuation.resume(
it
)
}
}
}
I suppose that map thread is suspending but I have no idea why. Help please.
android kotlin maps coroutine
runBlocking
blocks the thread.
– Zoe
Nov 9 at 16:11
@Zoe What else I can use instead as bridge between suspended and nonsuspend function?
– Karol Kulbaka
Nov 9 at 16:13
@Zoe More important: whyrunBlocking
block thread when I useMap
reference?
– Karol Kulbaka
Nov 9 at 16:20
runBlocking
combined withawait
will prevent execution until the coroutine is complete. You call loadMap in getValue every single time, without caching the result. So you load it every time you retrieve it, blocking it every time you do so.
– Zoe
Nov 9 at 16:24
I have edited code in question to load map only on first invoke, but it didn't solve problem.
– Karol Kulbaka
Nov 9 at 16:50
|
show 2 more comments
up vote
0
down vote
favorite
up vote
0
down vote
favorite
I have created delegate to initialize GoogleMap
instance which is using coroutine. Map fragment is dispalyed properly until I invoke any method on map reference, then map hangs and stop responding.
This is my delegate:
class MapInitializer(private val setUp: GoogleMap.() -> Unit) : ReadOnlyProperty<AppCompatActivity, GoogleMap> {
var instance: GoogleMap? = null
override fun getValue(thisRef: AppCompatActivity, property: KProperty<*>): GoogleMap {
return instance ?: thisRef.loadMap().apply(setUp).also { instance = it }
}
private fun AppCompatActivity.loadMap(): GoogleMap = runBlocking {
async { registerCallback() }.await()
}
private suspend fun AppCompatActivity.registerCallback(): GoogleMap = suspendCoroutine { continuation ->
(supportFragmentManager.findFragmentById(R.id.map) as SupportMapFragment).getMapAsync {
continuation.resume(
it
)
}
}
}
I suppose that map thread is suspending but I have no idea why. Help please.
android kotlin maps coroutine
I have created delegate to initialize GoogleMap
instance which is using coroutine. Map fragment is dispalyed properly until I invoke any method on map reference, then map hangs and stop responding.
This is my delegate:
class MapInitializer(private val setUp: GoogleMap.() -> Unit) : ReadOnlyProperty<AppCompatActivity, GoogleMap> {
var instance: GoogleMap? = null
override fun getValue(thisRef: AppCompatActivity, property: KProperty<*>): GoogleMap {
return instance ?: thisRef.loadMap().apply(setUp).also { instance = it }
}
private fun AppCompatActivity.loadMap(): GoogleMap = runBlocking {
async { registerCallback() }.await()
}
private suspend fun AppCompatActivity.registerCallback(): GoogleMap = suspendCoroutine { continuation ->
(supportFragmentManager.findFragmentById(R.id.map) as SupportMapFragment).getMapAsync {
continuation.resume(
it
)
}
}
}
I suppose that map thread is suspending but I have no idea why. Help please.
android kotlin maps coroutine
android kotlin maps coroutine
edited Nov 9 at 16:48
asked Nov 9 at 16:09
Karol Kulbaka
357111
357111
runBlocking
blocks the thread.
– Zoe
Nov 9 at 16:11
@Zoe What else I can use instead as bridge between suspended and nonsuspend function?
– Karol Kulbaka
Nov 9 at 16:13
@Zoe More important: whyrunBlocking
block thread when I useMap
reference?
– Karol Kulbaka
Nov 9 at 16:20
runBlocking
combined withawait
will prevent execution until the coroutine is complete. You call loadMap in getValue every single time, without caching the result. So you load it every time you retrieve it, blocking it every time you do so.
– Zoe
Nov 9 at 16:24
I have edited code in question to load map only on first invoke, but it didn't solve problem.
– Karol Kulbaka
Nov 9 at 16:50
|
show 2 more comments
runBlocking
blocks the thread.
– Zoe
Nov 9 at 16:11
@Zoe What else I can use instead as bridge between suspended and nonsuspend function?
– Karol Kulbaka
Nov 9 at 16:13
@Zoe More important: whyrunBlocking
block thread when I useMap
reference?
– Karol Kulbaka
Nov 9 at 16:20
runBlocking
combined withawait
will prevent execution until the coroutine is complete. You call loadMap in getValue every single time, without caching the result. So you load it every time you retrieve it, blocking it every time you do so.
– Zoe
Nov 9 at 16:24
I have edited code in question to load map only on first invoke, but it didn't solve problem.
– Karol Kulbaka
Nov 9 at 16:50
runBlocking
blocks the thread.– Zoe
Nov 9 at 16:11
runBlocking
blocks the thread.– Zoe
Nov 9 at 16:11
@Zoe What else I can use instead as bridge between suspended and nonsuspend function?
– Karol Kulbaka
Nov 9 at 16:13
@Zoe What else I can use instead as bridge between suspended and nonsuspend function?
– Karol Kulbaka
Nov 9 at 16:13
@Zoe More important: why
runBlocking
block thread when I use Map
reference?– Karol Kulbaka
Nov 9 at 16:20
@Zoe More important: why
runBlocking
block thread when I use Map
reference?– Karol Kulbaka
Nov 9 at 16:20
runBlocking
combined with await
will prevent execution until the coroutine is complete. You call loadMap in getValue every single time, without caching the result. So you load it every time you retrieve it, blocking it every time you do so.– Zoe
Nov 9 at 16:24
runBlocking
combined with await
will prevent execution until the coroutine is complete. You call loadMap in getValue every single time, without caching the result. So you load it every time you retrieve it, blocking it every time you do so.– Zoe
Nov 9 at 16:24
I have edited code in question to load map only on first invoke, but it didn't solve problem.
– Karol Kulbaka
Nov 9 at 16:50
I have edited code in question to load map only on first invoke, but it didn't solve problem.
– Karol Kulbaka
Nov 9 at 16:50
|
show 2 more comments
active
oldest
votes
active
oldest
votes
active
oldest
votes
active
oldest
votes
active
oldest
votes
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53229354%2fgooglemap-suspending-initializer-with-coroutine%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
runBlocking
blocks the thread.– Zoe
Nov 9 at 16:11
@Zoe What else I can use instead as bridge between suspended and nonsuspend function?
– Karol Kulbaka
Nov 9 at 16:13
@Zoe More important: why
runBlocking
block thread when I useMap
reference?– Karol Kulbaka
Nov 9 at 16:20
runBlocking
combined withawait
will prevent execution until the coroutine is complete. You call loadMap in getValue every single time, without caching the result. So you load it every time you retrieve it, blocking it every time you do so.– Zoe
Nov 9 at 16:24
I have edited code in question to load map only on first invoke, but it didn't solve problem.
– Karol Kulbaka
Nov 9 at 16:50