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.










share|improve this question
























  • 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 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












  • 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

















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.










share|improve this question
























  • 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 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












  • 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















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.










share|improve this question















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






share|improve this question















share|improve this question













share|improve this question




share|improve this question








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: 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












  • 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










  • @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










  • 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


















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



















active

oldest

votes











Your Answer






StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");

StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});

function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});


}
});














 

draft saved


draft discarded


















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






























active

oldest

votes













active

oldest

votes









active

oldest

votes






active

oldest

votes
















 

draft saved


draft discarded



















































 


draft saved


draft discarded














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





















































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







Popular posts from this blog

Schultheiß

Liste der Kulturdenkmale in Wilsdruff

Android Play Services Check