Welcome toVigges Developer Community-Open, Learning,Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
4.1k 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 Answer

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

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to Vigges Developer Community for programmer and developer-Open, Learning and Share
...