Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
4.6k views
in Technique[技术] by (71.8m points)

ios - DispatchSemaphore stopping all function

I'm trying to return a boolean to a delegate of an SDK, but I need my function to wait for the callback of another function. I think it becomes clear what I want to do with the code below:

func conversation(_ conversation: SKTConversation, shouldShowInAppNotificationFor message: SKTMessage) -> Bool {

    NSLog("shouldShow0")
    let semaphore = DispatchSemaphore(value: 0)

    // Default = true
    var shouldShow: Bool = true

    self.channel?.invokeMethod("shouldShow", arguments: nil, result: { result in

      NSLog("invokeMethod result: (String(describing: result))")

      if let result = result as? FlutterError {
        NSLog("Flutter Error! Message: (result.message) | Description: (result.description)")
      }

      guard let result = result as? NSNumber else {
        NSLog("inside !NSNumber")
        semaphore.signal()
        return
      }

      NSLog("inside NSNumber")
      // I need to return this result:
      shouldShow = result.boolValue
      semaphore.signal()
    })

    NSLog("shouldShow1")
    semaphore.wait()
    NSLog("shouldShow2")

    return shouldShow
  }

The problem is that my application is breaking when calling semaphore.wait() since I can't get a callback when this is called. These are my logs:

shouldShow0
shouldShow1
flutter: reached here!
flutter: returning true!

I'm returning the value in flutter but the callback is not being called because of the semaphore.wait() Obs: If I comment the semaphore.wait(), the callback works!

Sorry if it's a newbie question, I'm a Flutter developer and I'm getting into the native world


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)
等待大神解答

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

1.4m articles

1.4m replys

5 comments

57.0k users

...