0

This is my code

         HttpClient client = new HttpClient();
         client.DefaultRequestHeaders.Accept.Add(new 
         MediaTypeWithQualityHeaderValue("application/json"));
         var jsonstring = await client.GetStringAsync("https://asdasdadasd" + 
              url);

Occur this error:

2019-03-29 11:18:43.901 Eliant.App.iOS[3859:3548233] [AppCenterCrashes] ERROR: +[MSWrapperLogger MSWrapperLog:tag:level:]/7 Unhandled Exception: UIKit.UIKitThreadAccessException: UIKit Consistency error: you are calling a UIKit method that can only be invoked from the UI thread. at UIKit.UIApplication.EnsureUIThread () [0x00020] in

Library/Frameworks/Xamarin.iOS.framework/Versions/12.6.0.16/src/Xamarin.iOS/UIK

it/UIApplication.cs:89 at UIKit.UIGestureRecognizer.RemoveTarget (Foundation.NSObject target, System.IntPtr action) [0x00000] in /Library/Frameworks/Xamarin.iOS.framework/Versions/12.6.0.16/src/Xamarin.iOS/UIKit/UIGestureRecognizer.g.cs:342 at UIKit.UIGestureRecognizer.OnDispose () [0x00016] in /Library/Frameworks/Xamarin.iOS.framework/Versions/12.6.0.16/src/Xamarin.iOS/UIKit/UIGestureRecognizer.cs:41 at UIKit.UIGestureRecognizer.Dispose (System.Boolean disposing) [0x00000] in /Library/Frameworks/Xamarin.iOS.framework/Versions/12.6.0.16/src/Xamarin.iOS/UIKit/UIGestureRecognizer.g.cs:959 at Foundation.NSObject.Finalize () [0x00000] in /Library/Frameworks/Xamarin.iOS.framework/Versions/12.6.0.16/src/Xamarin.iOS/Foundation/NSObject2.cs:143

Any ideas?

Commenting out await client.GetStringAsync() makes the app work fine. I've tried running it using Device invoke on the main thread (even though I don't think there is any reason I should need to do that?) I've tried .result instead of await, and I've tried getasync().content.readstringasync and always have the same issue.

Nkosi
  • 235,767
  • 35
  • 427
  • 472
ish1104
  • 401
  • 4
  • 19
  • Provide a [mcve] that include where the above code is being called from. You have not provided enough details for us to be able to identify the actual cause of the problem. – Nkosi Apr 02 '19 at 05:45
  • Just those three lines of code, with nothing else, was being run on a clicked event of a button. The same error also happens on if placed in selectedindexchanged of a picker. It was running on the main thread, and I even tried invokeonmainthread. I ended up 'fixing' it by downgrading to Visual Studio 2017. If I go back to Visual Studio 2019, the same thing happens, so I assume this is some sort of bug in Xamarin.IOS version that ships with 2019. – ish1104 Apr 02 '19 at 05:46
  • Same as error as https://stackoverflow.com/questions/55423904/uikit-uikitthreadaccessexception-uikit-consistency-error-you-are-calling-a-uik#comment97572082_55423904 File an issue on Xamarin.iOS github's repo – SushiHangover Apr 02 '19 at 05:49
  • yes,issue is same but how to solve this – ish1104 Apr 02 '19 at 06:30
  • which HTTP client implementation are you using? CHeck it in ios project settings in iOS build section – Yauhen Sampir Apr 02 '19 at 06:45
  • This also happens if i swap out HttpClient for WebClient. – ish1104 Apr 02 '19 at 09:08

1 Answers1

1

Looks like you need to make this call on the UI thread (from the error contents).

Have you tried ensuring that this code is running on the UI thread? See this link for details on how you should be able to do this.

Essentially you should be doing something like:

InvokeOnMainThread ( () => {
    // code which needs to be run on UI thread
});
James Lavery
  • 920
  • 4
  • 25