Over17 / UnityAndroidPermissions

Unity Android Runtime Permissions for Android Marshmallow

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Callbacks not on Main Thread issue

rotter opened this issue · comments

I have integrated your plugin and it's working great - thanks!

But I have one problem/suggestion: make the callbacks run on main thread.

I was having a lot of problems until I found out the callbacks weren't on the main thread.

I had to turn on a flag on the callback and then check for the flag on the next Update. No big deal, but it would be better if the callback was already on the main thread, or at least make it clear for the developer that the callbacks are not on the main thread.

Best

Obs: I have just noticed that you do mention that the callbacks are not on (Unity's) main thread... but who reads documents? :)

Just kidding, but anyhow if you could somehow make it run on Unity's main thread it would be much better

commented

I think you can do it by yourself.

public IEnumerator MyPermissionRequestCoroutine(string permission, Action grantedPermission, Action deniedPermission ){
                bool granted = false;
                bool gotCallback = false;
                AndroidPermissionsManager.RequestPermission(new string[] { permission }, 
new AndroidPermissionCallback(
                    grantedPermission =>
                    {
                        gotCallback = true;
                        granted = true;
                    },
                    deniedPermission =>
                    {
                        gotCallback = true;
                        granted = false;
                    }
                    ));

                while (gotCallback == false)
                    yield return null;
                if (granted)
                {
                    grantedPermission();
                }
                else
                {
                    deniedPermission();
                }
}
//and use
StartCoroutine(MyPermissionRequestCoroutine( ... ));

(written from head)

Thanks for the suggestion. I had already made a solution similar to this, using flags.

But anyway it would be best for the user of the plugin if the callback was already on the main thread, so he didnt need to do anything (or lose time with crashes not noticing the callback isn't on the main thread).

Context synchronization should be used for this indeed.
But it's not as trivial as to use flags and async / coroutines.