Mike Dias
Mike Dias

Reputation: 195

Titanium - Webview is not rendered when is loaded by async callback

I have loop that for each item that call a async request to server, create a view with a webview for later add in a scrollview after calc the positions. In the callback, I get the server data and give to webview.

When the load method takes a little longer to perform (In a slow connection, for example), the webview is not rendered.

This happens many times in the device, but few times in the simulator. But, to simulate easily, just put a breakpoint in the load method, delaying the execution. I suspect that there is a problem when rendering an already added webview.

I simplified the code in a one block for better understanding:

function createView(){

    var view = Ti.UI.createView({
        //...
    });

    var webview = Ti.UI.createWebView({});
    view.add(webview);

    //Callback to process webview content after receive the server data     
    view.load = function(serverData){
        webview.setUrl(serverData.url);
        webview.addEventListener('beforeload', function(){
            webview.evalJS("var value="+serverData.value+";");
        });
    };

    return view;
};

var views = new Array();

for(i=0;i < data.length;i++){

     views[i] = createView();

     //Async request to server
     var req = Ti.Network.createHTTPClient();

     req.onload = function(){
          result = JSON.parse(this.responseText);
          views[i].load(result);
     }

     req.open("POST", "http://myserver.com.br/myservice");
     req.send({myParam: data[i].value}); 
}

var scrollView = Ti.UI.createScrollView({
    //...
});

for(i=0;i < views.length;i++){

    //Calc the positions, not relevant
    views[i].width = calculedValue; 
    views[i].height = calculedValue; 

    scrollView.add(views[i]);   
}

win.add(scrollView);

Im using Titanium SDK 1.8.2 and running on iPad 2 with iOS 5.0.1.

Upvotes: 0

Views: 1441

Answers (1)

Mike Dias
Mike Dias

Reputation: 195

I solved the problem evaluating the javascript data on the callback instead of on event listener

view.load = function(serverData){
    webview.setUrl(serverData.url);
    //Not on event 'beforeload'
    //webview.addEventListener('beforeload', function(){ not on 
        webview.evalJS("var value="+serverData.value+";");
    //});
};

I think that this is related with the UI thread is not called sometimes on event listener

Upvotes: 1

Related Questions