Configure Logging Tasks Properly

Hello, I am building an iOS application that records and retrieves accelerometer signals.
I have referred to document "https://mbientlab.com/tutorials/SwApple.html#logging" below to make accelerometerStartLog() and accelerometerStopLog(). Below are their code.

func accelerometerStartLog(_ sender: Any) {
    let signal = mbl_mw_acc_bosch_get_acceleration_data_signal(device.board)!
    mbl_mw_datasignal_log(signal, bridge(obj: self)) { (context, logger) in
        let _self: DeviceDetailViewController = bridge(ptr: context!)
        let cString = mbl_mw_logger_generate_identifier(logger)!
        let identifier = String(cString: cString)
        _self.loggers[identifier] = logger!
    }
    mbl_mw_logging_start(device.board, 0)
    mbl_mw_acc_enable_acceleration_sampling(device.board)
    mbl_mw_acc_start(device.board)
}

func accelerometerStopLog(_ sender: Any) {
    guard let logger = loggers.removeValue(forKey: "acceleration") else {
        return
    }
    mbl_mw_acc_stop(device.board)
    mbl_mw_acc_disable_acceleration_sampling(device.board)
    mbl_mw_logger_subscribe(logger, bridge(obj: self)) { (context, obj) in
        let acceleration: MblMwCartesianFloat = obj!.pointee.valueAs()
    }
    var handlers = MblMwLogDownloadHandler()
    handlers.context = bridgeRetained(obj: self)
    handlers.received_progress_update = { (context, remainingEntries, totalEntries) in
        let _self: DeviceDetailViewController = bridge(ptr: context!)
        let progress = Double(totalEntries - remainingEntries) / Double(totalEntries)
        if remainingEntries == 0 {
            _self.logCleanup { error in
                DispatchQueue.main.async {
                    if error != nil {
                        _self.deviceConnected()
                    }
                }
            }
        }
    }
    handlers.received_unknown_entry = { (context, id, epoch, data, length) in
        print("received_unknown_entry")
    }
    handlers.received_unhandled_entry = { (context, data) in
        print("received_unhandled_entry")
    }
    mbl_mw_logging_download(device.board, 100, &handlers)
}

My question is how and where should I define the "loggers" dictionary. I configured it as a class property like "var loggers: [String: OpaquePointer]!" but it didn't work properly. I activated accelerometerStartLog() by clicking the "Start Logging" UIButton after connecting to the board, but the "loggers" dictionary remains nil. In other words, the code inside the closure did not work. Subsequently, when the [Stop Log] button is pressed to invoke the stopLogging() function, of course, an error occurs in the line "guard let logger = self.loggers.removeValue". I already did Xcode debugging but I have no idea how to solve it. How can I do this properly?

Comments

Sign In or Register to comment.