I'm stuck with subclassing a CLCircularRegion
I have a subclass because I need some additional properties.
class MyCircularRegion: CLCircularRegion {
var alreadyEnteredProperty = false
var geoFenceExitTime: Double = 0.0
var geofenceEnterTime: Double = 0.0
var hasVisitStarted = false
override init(center: CLLocationCoordinate2D, radius: CLLocationDistance, identifier: String) {
super.init(center: center, radius: radius, identifier: identifier)
}
init(center: CLLocationCoordinate2D, radius: CLLocationDistance, identifier: String, alreadyEntered: Bool) {
super.init(center: center, radius: radius, identifier: identifier)
self.alreadyEnteredProperty = alreadyEntered
}
required init?(coder aDecoder: NSCoder) {
fatalError("Its all gone wrong")
}
}
all pretty standard.
I init my subclass like so
func updatePlaceTrigger(places: Set<Place>){
let monitoredRegions = Set<CLRegion>(locationManager.monitoredRegions.filter { region in region is MyCircularRegion })
let newPlaces = places.filter{place in place.inventoryId != nil}.map { (place : Place) -> CLRegion in
if let id = place.inventoryId, let location = place.postion, let category = place.metaData?.category{
let location = CLLocationCoordinate2D(latitude: location.latitude, longitude: location.longitude)
let region = MyCircularRegion(center: location, radius: category.radius, identifier: id, alreadyEntered: false)
region.notifyOnEntry = true
region.notifyOnExit = true
regionTriggerMapper[region.identifier] = place
return region
}else{
return CLBeaconRegion(proximityUUID: UUID(), identifier: "")
}
}
// Get items to remove and remove them from monitoring
let needToRemove = monitoredRegions.subtracting(newPlaces)
for region in needToRemove {
stopMonitoring(region)
regionTriggerMapper.removeValue(forKey: region.identifier)
}
// Start to monitor all regions
for region in newPlaces {
startMonitoring(region)
}
}
This line is causing me problems
let monitoredRegions = Set<CLRegion>(locationManager.monitoredRegions.filter { region in region is MyCircularRegion })
it always returns 0
but if I set it to this
let monitoredRegions = Set<CLRegion>(locationManager.monitoredRegions.filter { region in region is CLCircularRegion })
monitoerRegions has some elements, but then if I do
let regions = monitoredRegions as? Set<MyCircularRegion>
regions is now nil
Any help would be awesome
Thanks