Swift variable is nil half the time -


i'd update filterx in accordance accelerometer updates, variable nil half of time when build. @ first thought scope issue self, realized not work other half time.

my second guess was kind of race condition. didn't make sense either since code running synchronously, , should correct since parent function run every 0.1 second.

any appreciated.

class locationtrackingservice: cllocationmanagerdelegate {      static let sharedinstance = locationtrackingservice()     private override init() {}      var locationmanager = cllocationmanager()     var motionmanager = cmmotionmanager()      var filterx : kalmanfilter<double>?      func starttracking() {         locationmanager.requestalwaysauthorization()          if cllocationmanager.locationservicesenabled() {             locationmanager.delegate = self             locationmanager.startupdatinglocation()             motionmanager.startaccelerometerupdates()              let location = locationmanager.location!             let xycoord = latlontoxy(coordinate: (location.coordinate))              self.filterx = kalmanfilter(stateestimateprior: xycoord.x, errorcovarianceprior: pow(location.horizontalaccuracy, 2))         }          if motionmanager.isaccelerometeravailable {               print(self.filterx) // <-- prints object no problem               motionmanager.accelerometerupdateinterval = 0.1             motionmanager.startaccelerometerupdates(to: operationqueue.main) { data, _ in                   print(self.filterx) // <------- prints nil half time (i.e. half of builds)                   if let acceleration = data?.acceleration {                     if self.filterx != nil {                          // stuff                     }                 }             }         }     } 

where's locationmanager(_:didchangeauthorization:) delegate method? need authorization before proceeding startupdatinglocation().


Comments

Popular posts from this blog

android - InAppBilling registering BroadcastReceiver in AndroidManifest -

python Tkinter Capturing keyboard events save as one single string -

sql server - Why does Linq-to-SQL add unnecessary COUNT()? -