Skip to content

Instantly share code, notes, and snippets.

@adamtarmstrong
Last active July 3, 2019 16:11
Show Gist options
  • Save adamtarmstrong/5608741d454c71b2552d638cd7fa0e44 to your computer and use it in GitHub Desktop.
Save adamtarmstrong/5608741d454c71b2552d638cd7fa0e44 to your computer and use it in GitHub Desktop.
Issue updating @object in Class and reflecting in View
class ResultsObserver : NSObject, SNResultsObserving { //https://developer.apple.com/documentation/soundanalysis/analyzing_audio_to_classify_sounds
@ObjectBinding var whistle = Whistle()
func request(_ request: SNRequest, didProduce result: SNResult) {
... //removed for simplicity
if (classification.identifier == "my classification") {
print("Detected my classification - setting Object to true") //This shows in console (when appropriate)
DispatchQueue.main.async {
self.whistle.detected = true
}
}
}///end func
}///end class
...// All AVAudioEngine and SoundClassifier functions removed for simplicity
struct ViewDetail: View {
@ObjectBinding var whistle = Whistle()
var timer: Timer {
Timer.scheduledTimer(withTimeInterval: 1, repeats: true) {_ in
DispatchQueue.main.async {
... //removed for simplicity
print("> checking whistle state, detected=\(self.whistle.detected)") //I see this in the console, every second, but ALWAYS shows false
}
}
}
... //removed all SwiftUI Views and Timer logic for simplicity
}///end struct
class Whistle: BindableObject {
var didChange = PassthroughSubject<Void, Never>()
var detected: Bool = false {
didSet {
didChange.send(())
}
}
}
struct ContentView_Previews : PreviewProvider {
static var previews: some View {
ContentView(userData: UserData(users: testData), whistle: Whistle())
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment