简体   繁体   English

从 iOS cordova UIwebview 迁移到 wkwebview 禁用异步 ZDE9B9ED78D7E2E1DCEEFFEE780E2F91 执行以支持现有应用程序

[英]migration from iOS cordova UIwebview to wkwebview disable asynchronous javascript execution to support the existing application

In one of our older 'Save Data' examples, we had previously used the following and it was working fine without any issues in cordova UIWebview.在我们较早的“保存数据”示例之一中,我们之前使用过以下内容,并且在 cordova UIWebview 中没有任何问题。

  var filenameID;
  function getFilenameID() {
  $.ajax('/jquery/getdata',   // request url
  {
    success: function (data, status, xhr) {// success callback function
        kp_requestKioskId_callback(data);
   }});
  }
 function kp_requestKioskId_callback(kioskId) {
    filenameID = kioskId.split(" ").join("");
 }
function saveData(fileName, data) {
   getFilenameID();
   kp_FileAPI_writeToFile(filenameID + ".xls", data, "writeFile_callback");
 }

After migrating from UIWebview to WKWebview, In WKWebView since JavaScript code is executed asynchronously, the 'getFilenameID' call is not completed before the 'kp_FileAPI_writeToFile' call is executed, resulting in an undefined filename.从 UIWebview 迁移到 WKWebview 后,在 WKWebView 中,由于 JavaScript 代码异步执行,在执行 'kp_FileAPI_writeToFile' 调用之前,'getFilenameID'调用没有完成,导致未定义的文件名。

Copying the kp_FileAPI_writeToFile function inside kp_requestKioskId_callback will solve the issue.复制 kp_requestKioskId_callback 中的 kp_FileAPI_writeToFile function 将解决该问题。

But we have many similar kind of functions in our application.但是我们的应用程序中有许多类似的功能。

Is there a way to solve or disable the asynchronous javascript execution to avoid major changes on the application有没有办法解决或禁用异步 javascript 执行以避免应用程序发生重大变化

Is there a way to solve or disable the asynchronous javascript execution to avoid major changes on the application有没有办法解决或禁用异步 javascript 执行以避免应用程序发生重大变化

No, in modern browsers/webviews XmlHttpRequest (which is what $.ajax uses behind the scenes) disallows synchronous requests from the main thread.不,在现代浏览器/网络视图中XmlHttpRequest (这是$.ajax在幕后使用的)不允许来自主线程的同步请求。 The flag to disable asynchronous behaviour still exists but most browsers will throw an exception if you use it inside the main thread.禁用异步行为的标志仍然存在,但如果您在主线程中使用它,大多数浏览器都会抛出异常。

You can use synchronous XmlHttpRequest inside web workers, but that isn't making your application synchronous, and you can't use Cordova plugin APIs from web workers either, so you'll find this quite limiting. You can use synchronous XmlHttpRequest inside web workers, but that isn't making your application synchronous, and you can't use Cordova plugin APIs from web workers either, so you'll find this quite limiting.

The code sample shown you aren't even using the async option, and by default the option is set to true , which means your network request running on UIWebView was also running asynchronously, thus you have is an nondeterministic race condition.代码示例显示您甚至没有使用async选项,默认情况下该选项设置为true ,这意味着您在UIWebView上运行的网络请求也在异步运行,因此您有一个不确定的竞争条件。

I have never encountered this issue on the UIWebview, after migrating to WKWebview javascript code behaves asynchrously.在迁移到 WKWebview javascript 代码后,我在 UIWebview 上从未遇到过这个问题。

For eg: We have 3 API calls which are dependent on one after the other for a single product例如:我们有 3 个 API 调用,它们一个接一个地依赖于单个产品

Consider if we have 2 products, we are iterating the products in a loop and triggering 3 API for each product.考虑如果我们有 2 个产品,我们将循环迭代产品并为每个产品触发 3 个 API。

In WKWebview, it finishes the first thread and at last 2nd flow在 WKWebview 中,它完成了第一个线程和最后的第二个流程

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM