Posting NSNotification on the main thread

前端 未结 4 538
深忆病人
深忆病人 2020-12-29 19:41

I found the following code snippet which allows NSNotification to be posted on the main thread from any background thread. I would like to know if this is a saf

4条回答
  •  不思量自难忘°
    2020-12-29 19:58

    YES

    Swift 2 syntax

    dispatch_async(dispatch_get_main_queue()) {
        NSNotificationCenter.defaultCenter().postNotificationName("updateSpinner", object: nil, userInfo: ["percent":15])
    }
    

    Swift 3 syntax

    DispatchQueue.main.async {
        NotificationCenter.default.post(name: "updateSpinner", object: nil, userInfo: ["percent":15])
    }
    

提交回复
热议问题