This error is cryptic and can have any number of causes, but the most common are:
- Performing animations when the view isn’t on screen (use viewDidAppear, not viewWillAppear)
- Failing to call super when overriding a method
- Calling something in the UI from a thread other than the main UI one
- Calling UI methods that dramatically change the view (such as presenting / hiding main views) too quickly
- Calling UI methods before an alert has been dismissed
Basically the error is related to the UI. The message appearing in the output may not reflect the code that actually triggered it – so to debug you need to isolate the code that’s causing it. Your first port of call should be callbacks from alerts, or other popups. For me it was the UIImagePicker, I fixed it like so:
protected void FinishedPickingMedia(object sender, UIImagePickerMediaPickedEventArgs e) { var picker = sender as UIImagePickerController; if (picker == null) return; picker.DismissModalViewControllerAnimated(true); if (picker == _picker) _picker = null; // Work with the image here // Bug fix for fences issue BeginInvokeOnMainThread(() => { // CODE THAT INTERACTS WITH THE UI HERE }); }
The pertinent code is the BeginInvokeOnMainThread block, this is fixing the wait_fences issue. It looks like the callback method isn’t being executed on the UI thread. I’m not sure if this is an iOS SDK thing, or a quirk added by MonoTouch. It could also be that the picker is still animating away when I start tweaking the UI, causing the issue. For example messing about with the UI in the Clicked event of a UIAlertView can sometimes cause minor issues, moving the code to the Dismissed event fixes the issues completely.