Sebastian Keller
09/13/2019, 2:12 PMIncorrectDereferenceException
? I want to use the iOS CMAltimeter class, but it's callback gets called from a non-main-thread when altimeter?.stopRelativeAltitudeUpdates()
is called. I tried a surrounding try
and catch
, but no luck. I can not freeze the callback, because that would freeze update()
too. I really don't know what else to do right now.
altimeter = CMAltimeter()
altimeter?.startRelativeAltitudeUpdatesToQueue(NSOperationQueue.mainQueue) { data: CMAltitudeData?, error: NSError? ->
update(data.pressure.doubleValue)
}
Can the relaxed mode help? If so, how to enable it?Kris Wong
09/13/2019, 3:01 PMkpgalligan
09/13/2019, 3:53 PMSebastian Keller
09/13/2019, 4:15 PMupdate(data.pressure.doubleValue)
Sebastian Keller
09/13/2019, 4:16 PMkpgalligan
09/13/2019, 4:17 PMkpgalligan
09/13/2019, 4:42 PMkpgalligan
09/13/2019, 4:42 PM@ThreadLocal
object Altimeter{
val altimeter = CMAltimeter()
fun startUpdates() {
if(!isMainThread)
throw java.lang.IllegalStateException("Nah")
val callback = StableRef.create({ data: CMAltitudeData ->
update(data.pressure.doubleValue)
})
altimeter?.startRelativeAltitudeUpdatesToQueue(NSOperationQueue.mainQueue) { data: CMAltitudeData?, error: NSError? ->
if(data != null && isMainThread){
callback.get().invoke(data)
}
}.freeze()
}
fun update(){
//???
}
}
kpgalligan
09/13/2019, 4:43 PMolonho
09/13/2019, 4:50 PMKris Wong
09/13/2019, 5:11 PMkpgalligan
09/13/2019, 5:14 PMupdate
is manipulating something that should only be touched in main thread).kpgalligan
09/13/2019, 5:17 PMfun startUpdates() {
val callback = StableRef.create({ data: CMAltitudeData ->
update(data.pressure.doubleValue)
})
altimeter?.startRelativeAltitudeUpdatesToQueue() { data: CMAltitudeData?, error: NSError? ->
inMainThread{
callback.get().invoke(data!!)
}
}.freeze()
}
kpgalligan
09/13/2019, 5:18 PMSebastian Keller
09/13/2019, 5:21 PMSebastian Keller
09/13/2019, 5:22 PM