algolia / voice-overlay-ios

🗣 An overlay that gets your user’s voice permission and input as text in a customizable UI
https://alg.li/voice
MIT License
545 stars 62 forks source link

set locate crash #14

Closed salution closed 5 years ago

salution commented 5 years ago

I use set locate like this


   @IBOutlet weak var btn: UIButton!
    @IBOutlet weak var tv: UITextView!

    var voice2:VoiceOverlayController {
        let recordableHandler = {
            return SpeechController(locale: Locale(identifier: "ja_JP"))
        }
        return VoiceOverlayController(speechControllerHandler: recordableHandler)
    }

    override func viewDidLoad() {
        super.viewDidLoad()

        // Do any additional setup after loading the view.
        btn.addTarget(self, action: #selector(voiceButtonTapped2), for: .touchUpInside)

    }

    @objc func voiceButtonTapped2() {
        voice2.start(on: self, textHandler: { (text, final, extraInfo)  in
            print("voice output: \(String(describing: text))")
            print("voice output: is it final? \(String(describing: final))")
            if final{
                self.tv.text = text
            }

        }, errorHandler: { (error) in
            print("voice output: error \(String(describing: error))")
        })
    }
 Crash is

`Fatal error: Attempted to read an unowned reference but the object was already deallocated2019-03-18 17:08:43.601895+0700 test01[21328:8262914] Fatal error: Attempted to read an unowned reference but the object was already deallocated`
spinach commented 5 years ago

Hey @salution ,

Sorry for the late reply on this. After further investigation, I think I found out what is the issue. the voiceOverlayController instance is always being recalculated and we're losing reference to the one created everytime. In order to solve this, you can initialise your voiceOverlayController lazily like so and it will solve your problem:

lazy var voiceOverlayController: VoiceOverlayController = {
  let recordableHandler = {
    return SpeechController(locale: Locale(identifier: "en_US"))
  }
  return VoiceOverlayController(speechControllerHandler: recordableHandler)
}()

I also updated the snippet in the README

Cheers!