cowbell / cordova-plugin-geofence

Geofencing plugin for cordova
Apache License 2.0
264 stars 318 forks source link

EXC_BAD_ACCESS on addOrUpdate #238

Closed KindTech closed 6 years ago

KindTech commented 6 years ago

IOS 10.3 ionic 2 xcode 8.3.3 Location set to always Have done conversion as described in https://github.com/cowbell/cordova-plugin-geofence/issues/231

When adding a geofence, I get a warning showing in XCode:

A location manager (0x10116cf40) was created on a dispatch queue executing on a thread other than the main thread. It is the developer's responsibility to ensure that there is a run loop running on the thread on which the location manager object is allocated. In particular, creating location managers in arbitrary dispatch queues (not attached to the main queue) is not supported and will result in callbacks not being received.

Immediately after that I get a EXC_BAD_ACCESS (code:1) at GeoNotificationManager.init()

class GeoNotificationManager : NSObject, CLLocationManagerDelegate {
    let locationManager = CLLocationManager() <--Error occurs here

Stepping up the trace leads to addOrUpdate.

Relevant code from adding geofence:

let fence:iGeofence = {
    id: this.utilityService.createGuid(),
    latitude: 39.58redacted,
    longitude:-105.37redacted,
    radius: 1000,
    transitionType: this.geofence.TransitionType.ENTER,
    notification: {
      id: this.getNextNotificationId(),
      title: "redacted",
      text: "redacted",
      icon: null,
      openAppOnClick: true,
      data:{
        reftype:"redacted",
        refid:"redacted",
        refcity:"redacted",
        refname:"redacted"
      }
    },
  };
  this.addOrUpdate(fence);
KindTech commented 6 years ago

Update: Implementing the changes in https://github.com/cowbell/cordova-plugin-geofence/pull/224 fixed the issue.

KindTech commented 6 years ago

Ok apparently it didn't fix the issue.

KindTech commented 6 years ago

Is anybody maintaining this plugin?

KindTech commented 6 years ago

So to fix this: assuming you've done #231 and #224 in GeofencePlugin.swift replace

let locationManager = CLLocationManager()

with

 let locationManager = { () -> CLLocationManager in
        // CLLocationManager must be created on main thread otherwise
        // it will generate an error at init time.
        if Thread.isMainThread {
            return CLLocationManager()
        } else {
            return DispatchQueue.main.sync {
                return CLLocationManager()
            }
        }
     }()