Trying to understand Ti.Network.createHTTPClient()

You must Login before you can answer or comment on any questions.

I'm creating my first app that uses an external datasource. I've already built a remote RESTful service that's working as expected. Now I'm trying to access and use the data in the iPhone app. Here's the basic (truncated) layout for the page:

exports.Preview = function(searchCriteria, searchData) {
    ...
    Ti.API.info("mCount1: " + mCount);
    getCount(searchCriteria, searchData);
    Ti.API.info("mCount3: " + mCount);
    ...
    function getCount(searchCriteria, searchData)
    {
        var url = "http://www.myDomain.com/service/myService.svc/GetCount/searchCriteria/searchData";
        var json
        var xhr = Ti.Network.createHTTPClient({
            onload: function() {
                json = JSON.parse(this.responseText);
                Ti.API.info("json.count: " + json.count);
                mCount = json.count;
                Ti.API.info("mCount2: " + mCount);
            }
        });
        xhr.open("GET", url);
        xhr.send();
    }
)
The service returns a very simple JSON string which looks exactly like this:
{"count":12}
You'll notice the various Ti.Info lines I'm using to track the value of the global variable mCount. Based on how this code is layed, I expected to see something like this in the Console Window:
[INFO] mCount1: 0
[INFO] json.count: 12
[INFO] mCount2: 12
[INFO] mCount3: 12
But instead, this is what I get:
[INFO] mCount1: 0
[INFO] mCount3: 0
[INFO] json.count: 12
[INFO] mCount2: 12
I really don't understand the order in which things are happening here. It appears as if the block inside the onload: function() doesn't execute until just before the window loads? If so, how can I get and use the data from the service? Will I need to build the entire page inside this onload: function()?

Thanks for any insights you can provide!

Casey

Titanium Studio 2.1.0, SDK 2.1.0, iPhone

— asked 11 months ago by Casey Crookston
1 Comment
  • I forgot one thing in the code block:

    var mCount = 0;
    I should have copied and pasted that right after the initial window function.

    — commented 11 months ago by Casey Crookston

1 Answer

HTTP calls are handled asynchronously. So the code in your load function doesn't execute until the response from your web service returns. Therefore count3 gets printed before count2. If you want to do something with the count, you have to do it in the load function of your http call.

— answered 11 months ago by Anthony Decena
answer permalink
4 Comments
  • Ok, that makes sense. Yet it also means I'm going to have to 100% re-architect this window. (sigh). Back to the drawing board.

    — commented 11 months ago by Casey Crookston

  • Here's another thing I thought would work, but doesn't:

    function getCount()
        {
            var json
            var xhr = Ti.Network.createHTTPClient({
                onload: function() {
                    json = JSON.parse(this.responseText);
                    lblResults2.text = json.count; //<<-- THIS NEVER SETS
                }
            });
            xhr.open("GET", "http://www.....");
            xhr.send();
        }
    I would have thought that the label, which was defined and added to the page earlier, would update here. But it doesn't.

    — commented 11 months ago by Casey Crookston

  • It might be because label.text is supposed to take a string and json.count is a number. Try setting the label text to json.count.toString()

    This is just a guess of course as I can't examine the rest of the code.

    — commented 11 months ago by Anthony Decena

  • Show 1 more comment

Your Answer

Think you can help? Login to answer this question!