0

I have a function that takes a callback as a parameter, this function has a child function inside of it that I need to actually execute the passed callback that was passed to the parent when it's (the child function) done.

Example:

function Update(callback){
     var db = Ti.Database.open('xxxxx');    //Open local database
     var data = Ti.Network.createHTTPClient(); //Open a URL to obtain data to put into db.

    data.onload = function(){
        //loop through JSON file obtained from HTTPClient and place data into the DB.

       callback(); //I need THIS to trigger the parents callback to tell the next function that Update() is done.
    }

data.open("GET","URL");
data.send();

}

I'm pretty unexperienced when it comes to callbacks, so maybe I'm over thinking this one. Any help would be appreciated! Thanks all!

EDIT: Fixed - This code above works as expected. Somehow, because this Update function was being called while the DB was being created (another function creates the DB, then that callback function calls this Update function), it was still updating the DB, but wasn't calling the callback inside onload. I called the callback to trigger Update later, and it now works as expected. There were never any errors in the Appcelerator Console, so I'm not exactly sure how or why the DB was updated without the callback being called, but it was.

  • Did you try it and see if it works? – Paul S. Oct 28 '15 at 16:52
  • That looks like exactly how it should be done. Do you have a problem with it? – Felix Kling Oct 28 '15 at 16:53
  • If it is failing, potentially you are re declaring or killing the callback variable sometime before the inner function? – Jesse Oct 28 '15 at 16:54
  • @Jesse - I don't think anything should be killing the callback variable. It's just a simple for in loop that's pulling data from a JSON file and putting it into the DB. Thanks for the comments, at least I know the 'structure' is right. There must be something else going on here. – Nathan Follmer Oct 28 '15 at 17:00
  • 2
    Is the rest in `onload` executed? Is there maybe an error before `callback()` that prevents it from being called? How did you verify that it is not called? What have you done to debug the issue? We cannot really help you if you don't provide sufficient information. As it is now, we can only say: "This is how it should be, if it doesn't work, there must be a problem somewhere else". But that isn't really helpful for anybody. – Felix Kling Oct 28 '15 at 17:01
  • Sorry @FelixKling I hit enter too quickly on my reply... I have some console.log's set up in the function that should be called next and the for in loop inside the onload function is completing and updating the DB as intended. I will submit an answer once I find the error, but at least you've confirmed this _should_ work this way. Thank you for all the comments! – Nathan Follmer Oct 28 '15 at 17:05
  • ... So, it was an error somewhere else that was causing this not to work. I was calling the callback, to call this Update function, too early. I have no idea why the DB was actually getting updated, but the callback in onload wasn't called, but it was. When I moved the callback function to call Update(), it now fires every time and moves on as expected. There were never any errors in the logs, even when this wasn't working, so I wonder if there is some kind of bug with Appcelerator... Anyway, thanks for the help all! – Nathan Follmer Oct 28 '15 at 18:28

0 Answers0