Coder Social home page Coder Social logo

Comments (3)

wendux avatar wendux commented on September 16, 2024

你native实现方式有问题,无论何时testAsyn都会被同步调用,只是completionHandler调用时才会触发js回调, sleep直接阻塞线程,所谓支持异步,是客户端的任务实现本身就要是异步的。

from dsbridge-ios.

mailworks avatar mailworks commented on September 16, 2024

相当于JS端必须同步的调Native端的方法,如果Native对应的方法耗时太多也会阻塞JS端的线程,所谓的“异步”只能在Native端方法写“异步”处理?

这样做就违背了WKWebView的设计原则了

No I don't believe it is possible due to the multi-process architecture of WKWebView. WKWebView runs in the same process as your application but it communicates with WebKit which runs in its own process (Introducing the Modern WebKit API). The JavaScript code will be running in the WebKit process. So essentially you are asking to have synchronous communication between two different processes which goes against their design

用原生的方式JS-Native异步调用和这个框架“异步”是有区别的

  [controller addScriptMessageHandler:self name:@"observe"];
- (void)userContentController:(WKUserContentController *)userContentController
      didReceiveScriptMessage:(WKScriptMessage *)message 

可以参加下这个Demo,加Native端加了sleep函数 JS端也不会block

JSMessageExample-master.zip

from dsbridge-ios.

wendux avatar wendux commented on September 16, 2024

Native对应的方法耗时太多的话,我觉得应该由程序员在Native进行显式的异步处理。你的这种场景,其实就是:只要js以异步方式调用,即使native端是同步处理的,对于js来说也要是异步的。这样的话如果所有异步任务,natvie端都以同步的方式来处理,同时使用messageHandler 来做,是有问题的,原因如下:WKWebView处理js消息是在一个单独的线程,如果端上还是同步的写法,那么任务多时,必定是要排队的,native端的任务就会阻塞,这会导致native端执行完所有的“异步任务”的总时长延长,因为只有一个线程。这样虽然js不会阻塞,但是native端还是会阻塞,而解决的方法还是要在MessageHandler中手动开线程。所以我认为,任务是否是异步应该接口提供方自己决定,而不是调用方。不过WKWebview中用MessageHandler的方式是好一些,职责分离,但由于目前DSBridge目前支持UIWebview,也是为了统一的行为,之后的版本可能会使用MessageHandler。

from dsbridge-ios.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.