Skip to content

Instantly share code, notes, and snippets.

@steipete
Last active December 14, 2015 09:18
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save steipete/5063602 to your computer and use it in GitHub Desktop.
Save steipete/5063602 to your computer and use it in GitHub Desktop.
Xamarin Native Code porting, thoughts.
  1. Better error handling. It took me 3 hours to figure out that the crash in the constructor happened because of out NSError. It did not directly crash on calling that method, but sometimes afterwards. The broken call in question was TryLoadAnnotationsFromFileWithError inside PSPDFFileAnnotationProvider's constructor - but it did not crash there. The class PSPDFFileAnnotationProvider has been overridden and that constructor was called as well, and THEN everything crashed with an opaque error. The 'fix' is also super messy (https://github.com/Krumelur/MonoTouch-PSPDFKit-bindings/commit/a2b225c4fd6ba7b7a0bdcd966bd39b1cc3f3e724#L1R1158). NSError** is a very common pattern in Cocoa to relay errors, there should be a builtin-way to address that. (sth. like bool TryLoadAnnotationsFromFileWithError (out [NullAllowed] NSError error);)

  2. Exceptions should show the native stack trace. It's not that hard to symbolicate this on the fly. Currently once you get sth like this... you're basically f*cked. http://cl.ly/image/2a1q0R083b1n (and no, showing Details doesn't show the actual stack trace). I also did some work for Appcelerator to port my plugin there, and they generate an Xcode project during their build. This can then be opened to debug the native part, which is extremely helpful. Especially since you can replace 3rd party static libraries and use Xcode submodules instead, drastically speeding up the current painful workflow. (see http://forums.xamarin.com/discussion/1795/debugging-native-bindings-workflow-question#latest)

  3. The default deployment target should probably be 4.3, and those options shouldn't be there anymore: http://cl.ly/image/3U1t223d341D

  4. Error handling again. In this case we forgot changing NSSet to NSOrderedSet when updating the bindings, and then getting this (compared to other crashes) actually pretty helpful exception. The "NSObject" type is actually an NSOrderedSet, but that one is not happed yet.http://cl.ly/image/2L1T3e2S1i14. Bugzilla report: https://bugzilla.xamarin.com/show_bug.cgi?id=10694 (I understand that things like NSHashTable or NSPointerArray are still missing from the Xamarin foundation, but NSOrderedSet is pretty basic and came with iOS5)

  5. Xamarin seems to have runtime issues when an optional protocol is being bound and then a subclass that implements that protocol is being created in managed code. When I uncomment this: https://github.com/Krumelur/MonoTouch-PSPDFKit-bindings/commit/7078f2cb467f3d252a6c5f4d9dcf68417d6fbecf Xamarin changes the runtime information of that class so that respondsToSelector returns YES on 'annotationViewClassForAnnotation:' and then the code subsequently crash because it's not implemented. There should be a bugzilla entry as well - fixing this would actually be my priority #1, since it's a very non-obvious issue. (Or maybe the bindings are wrong, but I re-read the documentation and everything looks correct) Update: Here's the bug report, from 2012: https://bugzilla.xamarin.com/show_bug.cgi?id=8769

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment