+
Skip to content

Docs don't mention that on iOS geolocation needs to be done on a thread with a run loop #184

Open
@heldercorreia

Description

@heldercorreia

I'm new to iOS and expected that calling this inside a suspend function launched in Dispatchers.IO:

val geolocator = Geolocator.mobile()
val geolocatorResult = geolocator.current(priority = Priority.HighAccuracy)

would simply work on iOS just like it does on Android.
It turns out that on iOS it needs to be done like this:

var geolocatorResult
withContext(Dispatchers.Default) {    // [***]
        val geolocator = Geolocator.mobile()
        geolocatorResult = geolocator.current(priority = Priority.HighAccuracy)
}

Otherwise, the app will never return.

There is no mention of these facts in the docs, which can result in great time wasting for people in the same situation like myself.

Source: https://developer.apple.com/documentation/corelocation/cllocationmanager

[***] Using Dispatchers.Main here instead will trigger the warning:
[CoreLocation] This method can cause UI unresponsiveness if invoked on the main thread. Instead, consider waiting for the -locationManagerDidChangeAuthorization: callback and checking authorizationStatus first.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions

      点击 这是indexloc提供的php浏览器服务,不要输入任何密码和下载