Is it possible to store subclasses of CLRegion with startMonitoringForRegion: and retrieve a correct version/copy of it later with monitoredRegions

StackOverflow https://stackoverflow.com/questions/22408954

Question

This week I tried to subclass CLRegion class by adding new properties and methods on it and then monitor it.

@interface MyClass : CLRegion

I did this because I found it way cleaner than storing separately the CLRegion, and the added properties separately.

I implemented the NSSecureCoding and NSCopying protocol in this subclass to make it work. Everything went fine for monitoring this subclass with startMonitoringForRegion: or stopMonitoringForRegion:.

But when retrieving existing regions with (NSArray *)monitoredRegions: my problem occurs:

If I identify the region as a subclassed Region by the identifier, and then cast it to the subclassed class (MyClass *) to call specific methods, this doesn't work and I get a "unknown selector for CLCircularRegion" exception.

I highly suspect that this doesn't work because there is no such thing as virtual methods in Objective-C and so the son's init//initWithCoder//encodeWithCoder//.. is not called when the objects are builded by monitoredRegions.

As for know, i'm forced to store the additionals properties in NSUserDefaults and retrieve it when I identify the region, but I find it a little bit dirty...

So is there a way to do it like this ? Or should I keep the NSUserDefault solution ?

Was it helpful?

Solution

no you will get 'stock' CLRegions


quote: "The objects in this set may not necessarily be the same objects you specified at registration time. Only the region data itself is maintained by the system. Therefore, the only way to uniquely identify a registered region is using its identifier property."

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top