'NotificationCenter notification doesn't occur until user drop SliderUI

I have:

  • run Procces in MacOS SwiftUI application

  • UI Slider widget that updated value and force Process to perform some job.

  • Handler process responses (NotificationCenter.default.addObserver)

      outHandle  = FileHandle()
      outputPipe = Pipe()
      inputPipe  = Pipe()
      process    = Process()
    
      process.standardInput       = inputPipe
      process.standardOutput      = outputPipe
    
      outHandle = outputPipe.fileHandleForReading
      outHandle.waitForDataInBackgroundAndNotify()
    
      var observer : NSObjectProtocol!
    
      observer = NotificationCenter.default.addObserver(forName: NSNotification.Name.NSFileHandleDataAvailable, object: outHandle, queue: nil) {  notification -> Void in
      let data = self.outHandle.availableData // #1
      ...
      }
    

    try process.run()


View:

Slider(value: Binding(
     get: { myValue }, 
     set: { (newVal) in  
              myValue = newValue  
              inputPipe.fileHandleForWriting.write("{ value: \(myValue) }") // #2 
          }), in: 0...1)

Requests to process (comment #2) sends simultaniously to slider moving and I see process writes result into console. I expect handler of observer (comment #1) will run simultaniosly to process responses. But line (comment #1) executes only after I drop (mouseUP) slider.

If I move slider very fast and drop it (mouseUP) and process doesn't responded for all request yet - I will enter into #1 several times (for each process response that was occur after interaction with slider finished). If I move slider very slow so process responded to all request during movement - after dropping (mouseUP) slider #1 executes only one time (for last process response).

Tried to use different values of queue: OperationQueue.main, OperationQueue.current, nil.



Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source