本文介绍了Connection::dispatchIncomingMessages:在iOS中调用evaluateJavascript时触发了IPC节流的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我的应用程序中,我通过 TCP 套接字(带有串行的全局队列)连接我的应用程序,并从服务器获取数据并通过 RunLoop.main 中的 wkWebview.evaluateJavascript 将其发送到 webView

In my app I connected my app via TCP socket (global queue with serial) and get the data from the server and send it to the webView via wkWebview.evaluateJavascript in RunLoop.main

  • 插座
   func socket(_ sock: GCDAsyncSocket, didRead data: Data, withTag tag: Int) {
    delegate?.onReceive(encodedData: data)
     
    self.socket?.readData(withTimeout: -1, tag: 0)
  }

  • 从套接字接收的主题(组合)
  •      self.onReceivedSubject
          .receive(on: RunLoop.main)
          .sink { (_) in
             
          } receiveValue: { [weak self] (info) in
            self?.callJavascriptFunc(function: WebConstants.JSFunction.socketDataReceived,
                       param: "'\(info)'")
          }.store(in: &disposables)
    

    • 调用evaluateJavascript
    •    private func callJavascriptFunc(function: String, param: String) {
          let functionWithParams = WebConstants.JSFunction.javascript + "\(function)(\(param));"
           
          webView?.evaluateJavaScript(functionWithParams, completionHandler: { (result, error) in
      // Do nothing..
          })
        }
      

      来自 socket 的数据有点大,在几分钟内调用 evaluateJavaScript 一百个.

      The data from socket is little bit huge and called evaluateJavaScript a hundred in minutes.

      一切正常,但几秒钟后,我收到以下错误,我的应用程序的 UI 被完全阻止.

      Everything works fine but after few seconds, I got below errors and my app's UI is whole blocked.

      IConnection::dispatchIncomingMessages:IPC 节流被触发(有 689 个待处理的传入消息,在让步前只会处理 600 个)

      我该如何解决这个问题?有什么问题?

      How could I fixed this and what's the problem?

      谢谢.

      推荐答案

      好的,我想通了.

      在我的程序中,下面的代码被多次调用,似乎这个 evaluateJavascript 等待它的 completionHandler 到达并阻塞其他进程.换句话说,这会产生大量消息,它们的处理量超出预期.最后它阻塞进程.

      In my program, below code called in many times and seems this evaluateJavascript waits until its completionHandler arrive with blocking other process. In other words this produces lots of messages which can be processed than expected. Finally it blocks process.

          webView?.evaluateJavaScript(functionWithParams, completionHandler: { (result, error) in
      
      })
      

      所以我的解决方案是在等待它的 completionHandler 时运行 Run loop.

      So my solution is to run Run loop while it waits its completionHandler.

          webView?.evaluateJavaScript(functionWithParams, completionHandler: { (result, error) in
              isFinished = true
          })
          
          /// Prevent no to block the thread, just run once before receving the response.
          while !isFinished {
              RunLoop.current.run(mode: .default, before: Date.distantFuture)
          }
      

      这篇关于Connection::dispatchIncomingMessages:在iOS中调用evaluateJavascript时触发了IPC节流的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-15 04:01