User interaction sometimes screws up jQuery ajax requests in UIWebView

馋奶兔 提交于 2020-02-02 02:16:25

问题


I'm building an iPhone app that displays a UIWebView pointing to a web application I've created.

The web application makes frequent web service calls for data items which are used to animate controls on a canvas. The calls for data use jQuery ajax, passing parameters via JSON and receiving an XML response.

I'm finding that while user interactions with the UIWebView are occurring, the javascript setTimeout method is blocked and doesn't seem to execute at all. Fair enough; there are ways around this.

But the major problem is that every now and then after user interactions (zooming, panning etc), the ajax web service calls will just fail all the time and I can't establish a reason why. Even if they are made repeatedly, for the next few minutes none of them will even get through to the web service. If you completely leave the UIWebView alone, they will never fail as long as the web service is up and connectivity is present.

Can anyone suggest why, and how to fix/work around this?

Quick update: according to the Safari mobile debugger, the 'response' object in the error function is undefined. (It works if, for example, I make the URL invalid. This can then be called from objective-c by [webView stringByEvaluatingJavascript:@"lastError"], but throws an exception for this 'touched the uiwebview' error):

    $.ajax({
    type: "POST",
    url: "WebService.asmx/GetValues",
    async: true,
    data: "{'pageVersionIndex': " + PageVersionIndex + " , 'timeStreamIndex': '" + TimeStream + "'}",
    contentType: "application/json; charset=utf-8",
    dataType: "xml",
    success: function (response) { UpdateControls(response); },
    error: function (response, status, errorthrown) {
        calling = false;
        lastError = response.statusText; //Throws exception
        connectionInterrupted = true;
        DataRoutine = window.setTimeout(DataService, dataFrequency); }
    });

回答1:


I'm afraid you are toasted... in a way. In iOS Safari and in UIWebView respectively system processes have priority over browser and if there is a sudden demand for more CPU power or memory for native processes (like handling touch etc) it might happen that any running javascript will be stopped from executing to reduce the memory load or cpu usage. The worst part is that it won't throw any errors or anything... it just stops your code execution as if nothing happened.

Afraid that if it happens a lot in your app the only way would be to add some kind of timer that would listen if the request wasn't blocked if so - do it again until successful.

Ups and downs of iOS - they really like you to go native rather then web :)

hope it helps, Tom



来源:https://stackoverflow.com/questions/4691812/user-interaction-sometimes-screws-up-jquery-ajax-requests-in-uiwebview

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!