xamarin / ios-samples

Xamarin.iOS sample apps
http://docs.microsoft.com/xamarin/ios/
Other
1.59k stars 1.91k forks source link

iOS HttpClient crash #372

Closed luczha closed 5 years ago

luczha commented 5 years ago

This is my code

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

And get 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

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.

rolfbjarne commented 5 years ago

This is a bug in Xamarin.iOS (https://github.com/xamarin/xamarin-macios/issues/5388), which has already been fixed in the latest stable release.