Drawing in NSView from background operation is not affected

I am trying to draw in NSViewfrom a background operation, but I see no effect.

let queue = OperationQueue()
queue.addOperation() {
  doTheBackgroundStuff()
}

launches a background mode that does a lot of computing. In AppDelegate I have

@IBOutlet weak var image: NSImageView!  // some image to show
@IBOutlet weak var number: NSTextField! // a corresponding number
@IBOutlet weak var mainView: NSView!    // the main view holding the above

Appointment

number.intValue = Int32(someNumber)

regularly issued from a background operation (often). But the text never changes. I set "can draw at the same time" in IB for presentation, as well as for TextField. I also tried

if mainView.lockFocusIfCanDraw() {
  mainView.setNeedsDisplay(mainView.rectPreservedDuringLiveResize)
  mainView.unlockFocus()
}

after assigning a text field. Also to no avail.

+4
source share
2 answers

, , :

DispatchQueue.main.async {
    // your view update code
}

doTheBackgroundStuff , , .. , mainView.

, , -UI, Dispatch.main.async.

+2

flushGraphics, fooobar.com/questions/1512030/...:

NSGraphicsContext .

:

- , . Cocoa , , flushGraphics NSGraphicsContext, . , .

+3

Source: https://habr.com/ru/post/1661505/


All Articles