Bug 26416 - MKAnnotationWrapper, CCHMapClusterController Objective-C Binding
Summary: MKAnnotationWrapper, CCHMapClusterController Objective-C Binding
Status: RESOLVED ANSWERED
Alias: None
Product: iOS
Classification: Xamarin
Component: General ()
Version: XI 8.6.0
Hardware: Macintosh Mac OS
: --- normal
Target Milestone: Untriaged
Assignee: Bugzilla
URL:
Depends on:
Blocks:
 
Reported: 2015-01-26 08:22 UTC by Nicolas VERINAUD
Modified: 2015-03-05 08:05 UTC (History)
2 users (show)

Tags:
Is this bug a regression?: ---
Last known good build:

Notice (2018-05-24): bugzilla.xamarin.com is now in read-only mode.

Please join us on Visual Studio Developer Community and in the Xamarin and Mono organizations on GitHub to continue tracking issues. Bugzilla will remain available for reference in read-only mode. We will continue to work on open Bugzilla bugs, copy them to the new locations as needed for follow-up, and add the new items under Related Links.

Our sincere thanks to everyone who has contributed on this bug tracker over the years. Thanks also for your understanding as we make these adjustments and improvements for the future.


Please create a new report on Developer Community or GitHub with your current version information, steps to reproduce, and relevant error messages or log files if you are hitting an issue that looks similar to this resolved bug and you do not yet see a matching new report.

Related Links:
Status:
RESOLVED ANSWERED

Description Nicolas VERINAUD 2015-01-26 08:22:07 UTC
I am maintaining the CCHMapClusterController mono port.

I did the Unified API migration and now encounter an issue with all annotations on map views.

When panning the map view or changing the region programatically all map view's annotations are replaced by instances of MKAnnotationWrapper.

The library does not override the framework behavior using methods swizzling or categories.

You can download the code of the mono port here : https://github.com/nverinaud/CCHMapClusterController-Mono.
Open the "CCHMapClusterController-Mono/MapClusterSample/MapClusterSample.sln" solution and run the project on simulator or device.
You'll see logs of wrapped annotations.

I can't figure out how this happen. If I do not use the library, just reference it somewhere in the code for it to load it breaks all map views in the app.

Thank you for your help !
Comment 1 Rolf Bjarne Kvinge [MSFT] 2015-01-26 13:20:38 UTC
It is by design, but the answer is a bit complicated.

When Objective-C calls GetViewForAnnotation, the only thing Xamarin.iOS knows is that the pointer we get is to an object that conforms to MKAnnotation protocol, we do not know anything else about the object. In particular we can't check the actual type of the object at runtime, because Objective-C is known to lie (you could for instance get back that the object is of a type that does not implement the MKAnnotation protocol). This also means that we do not know which C# type to use - we know we need something that implements the IMKAnnotation interface, but we need an actual class that can be instantiated, not an interface. So internally we create Wrapper classes that only implements the I* interfaces, and use those when we get pointers to protocols.

Admittedly we could check if there already is an existing C# object with the desired pointer and which implements the desired interface, but right now we don't (this would also make the behavior less consistent; sometimes you'd get a wrapper object, sometimes not).

The workaround is to lookup the object yourself:

using ObjCRuntime;
public override MKAnnotationView GetViewForAnnotation(MKMapView mapView,
IMKAnnotation annotation)
{
    ACustomAnnotation customAnnotation = Runtime.GetNSObject (annotation.Handle) as ACustomAnnotation;
}

I hope this makes sense.
Comment 2 Nicolas VERINAUD 2015-01-27 02:30:14 UTC
Thanks for your help, your snippet work.

What is odd to me is that I only need this trick when the CCHMapClusterController DLL is loaded.

If I create an empty project with a map view and add an MKPointAnnotation to it, Xamarin.iOS does not wrap the annotation.

Are you sure there is no issue in Xamarin.iOS ?
Comment 3 Rolf Bjarne Kvinge [MSFT] 2015-03-05 08:05:17 UTC
The difference with CCHMapClusterAnnotation is that when GetViewForAnnotation is called by iOS there is no managed instance for the CCHMapClusterAnnotation instance in question. If you add an MKPointAnnotation to a MKMapView, the managed MKPointAnnotation instance already exist, and that's what's passed to the managed GetViewForAnnotation method.