Homan Mohammadi
Posts: 0
Joined: Mon Feb 17, 2014 10:30 pm

Synchronicity Issue

Hi,

I am iterating through a JSON response and executing a chain of services after each iteration. After executing the services, I want to be able to know where I was in the JSON when I triggered the service. So for example, for the second iteration of the JSON, I want to know upon success of the execution of the services, that the response is for the second iteration. I tried using LocalStorage in my iteration (i.e.for loop) to keep track of a variable “count” (so that I could know where in the for loop I am). I could then get this variable after success of the services. However, it seems like there is a sync issue, whereby the loop runs to completion before the services are executed, and so I am only picking up the last iteration using localStorage.

How would I go about keeping track of my location in the iteration? Is there a way for me to send that information through the service? (i.e. get the iteration number as a response parameter?)

Thank you for your help!
Homan

Illya Stepanov
Posts: 0
Joined: Mon Mar 18, 2013 8:48 am

Synchronicity Issue

Hi Homan,

Can you please clarify how exactly you're executing services?

Homan Mohammadi
Posts: 0
Joined: Mon Feb 17, 2014 10:30 pm

Synchronicity Issue

Hi Illya,

The following is my order of operations:

  1. I run a contact_service where I load contacts from my phone

  2. I then use the JSON response of this service, and iterate through every phone number, looking it up in a collection called userinfo. This is done as follows:

    var m = data;
    for(var i = 0; i < m.length; i++) {
    var obj = m;
    //execute service for every phone number
    userinfo_query.execute({"data": {"where": '{"mobilenum": "' + obj.phoneNumbers[0].value + '"}'}});
    }

  3. Upon completion of the userinfo_query service, I check to see if the phone number exists in the collection, or if the response is empty. If the response is empty, I execute some other service (not important). If the result is not empty, however, I want to update a particular label in a list with a message (i.e. for the fifth phone number in my contacts, I want to update the fifth label). To do this, I need to know the number "i" from the for loop above, which repeats below in "eq(i)".

    if(data.length === 0){
    requester_query.execute();
    }

    else {
    //this updates the label
    var $a = $("#AddFriend_mobilelist_12").find(".AddFriend_mobilelabel_status").eq(i);
    var text = $a.text().replace ("Status", "Already Friends");
    $a.text (text);
    }

    However, when I try to run the above code and pass the variable "i" as a local storage variable (i.e. use localStorage.setItem in the for loop, and use localStorage.getItem in the next part), only the last i value goes through. For example, if i is 10, for all 10 runs of the userinfo_query service, i=10, instead of i=1, i=2, i=3 ... i =10. I think this may be because the for loop is running to completion and the local variable being updated to the last value before the other services have finished running. If this is in fact the case, how do I work around it?

    Please let me know if this clarifies the situation.

    Thanks!
    Homan

Alena Prykhodko
Posts: 0
Joined: Tue Apr 09, 2013 7:36 am

Synchronicity Issue

Hi Homan.

Try to use execution and success together, paste the code:

pre

var ExecuteQuery = function(data){
var obj = data&#46;obj;
var i = data&#46;i;

Code: Select all

 &#47;&#47;This is a success eventHandler&#46; 
 var onSuccess = function(response){ 
     &#47;&#47;Here is avaliable "i" variable and "obj" variable&#46; 
     console&#46;log(i + " response"); 
 }; 

 userinfo_query&#46;execute({ 
     "data": { 
         "where": '{"mobilenum": "' + obj&#46;phoneNumbers[0]&#46;value + '"}' 
     }, 

     success: onSuccess 
 }); 

};

var m = data;
for (var i = 0; i < m&#46;length; i++) {
var obj = m;

Code: Select all

 &#47;&#47;execute service for every phone number  
 ExecuteData({obj: obj, i: i}); 

};/pre

Homan Mohammadi
Posts: 0
Joined: Mon Feb 17, 2014 10:30 pm

Synchronicity Issue

Hi Alena,

Thank you so much for your help! Unfortunately, upon success of the service, I need to check if the service response is null. However, it seems like I can only get access to the initial JSON response here, and not the one that results from running the userinfo_query service (I tried checking "data" in the onSuccess function). Is it possible for me to get access to that service's response data this way?

Thanks!
Homan

Igor
Posts: 0
Joined: Tue Apr 02, 2013 12:05 pm

Synchronicity Issue

Hi,

Please try to put this code on success event:
pre
code
var onSuccess = function(response){
&#47;&#47;Here is avaliable "i" variable and "obj" variable&#46;
console&#46;log(i + " response");
};
/code
/pre
In such way you can you get "response"(instead of "data") and make a decision about it content.

if service response is in the JSON format then you can parse JSON using next code
code
JSON&#46;parse(jsonString)
/code

Homan Mohammadi
Posts: 0
Joined: Mon Feb 17, 2014 10:30 pm

Synchronicity Issue

Thank you so much Igor and Alena for your help! I am now able to pass the "i" value to the service success response. This was incredibly helpful!

However, I have another related issue: as I mentioned previously, I am trying to execute a series of services, each triggered by the success of the past service. And I need this "i" value to be moved on to each service. How can I use the structure you showed me here to keep on passing the "i" value to future success events? For example, if in the onSuccess function, I trigger another service query, how could I then check the "response" of that service? In particular, could different success responses be triggered? (For instance, if a particular service runs, I trigger onSuccessA, and if another service runs, I trigger onSuccessB?)

Thank you again for your help,
Homan

Illya Stepanov
Posts: 0
Joined: Mon Mar 18, 2013 8:48 am

Synchronicity Issue

Hi Homan,

Please read JS topics about closure and following links for understand how it works:

http://stackoverflow.com/questions/13...
http://stackoverflow.com/questions/72...

Regards.

Homan Mohammadi
Posts: 0
Joined: Mon Feb 17, 2014 10:30 pm

Synchronicity Issue

Thanks again for your help!

Return to “Issues”