Reputation: 421
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.
Upvotes: 0
Views: 365
Reputation: 949
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
});
Upvotes: 1