ACS Retrieve data with callback

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

I am having a VERY difficult time figuring out how to retrieve data from ACS. I know that callbacks are required (at least I think they are) and I'm having a tough time figuring out how to structure and call the function. I did get table updates to work see my question and answer here. The way this works is that there is a method to set the contents of a table after it is created so even though the execution of the javascript continues once I retrieve the tabledata, I can set it.

Now I want to use ACS more like SQLITE or any regular database. The behaviour I am looking for is below. For example, I have a Photo with the name 'photo.png' stored in ACS. Now I want to query ACS with the photo name (photo.png) then retrieve one of the photo URL's and assign it to a imageView object.

So the code I have is something like this: (simplified example only)

var Cloud = require('ti.cloud');
Cloud.Photos.search({
    filename: 'photo.png'
}, function (e) {
    if (e.success) {
            var photo = e.photos[0].urls.thumb_100;
    } else {
        alert('Error:\\n' +
            ((e.error && e.message) || JSON.stringify(e)));
    }
});
 
var image = Ti.UI.createImageView({
  image:photo
});
Of course, the above doesn't work because the variable 'photo' is not set by the time we get to creating the imageview.

So, how do you code this thing so that you ensure the variable 'photo' is set before you try and use it in the imageView. I know it has something to do with callbacks but I am having a terrible time understanding this...

Anyone's help would be GREATLY appreciated.

Ray

2 Answers

Maybe something similar to this:

var Cloud = require('ti.cloud');
var image = Ti.UI.createImageView();
Cloud.Photos.search({
    filename: 'photo.png'
}, function (e) {
    if (e.success) {
            image.image = e.photos[0].urls.thumb_100;
    } else {
        alert('Error:\\n' +
            ((e.error && e.message) || JSON.stringify(e)));
    }
});

— answered 12 months ago by Adam Paxton
answer permalink
3 Comments
  • The problem here is that I can't get the main javascript thread to wait for the 'image' object to be returned. So, even though the image object is assigned as part of the success return, in the meantime in my application, I want to add the image object to the window or view I am building. When I executre win.add(image); it won't be assigned as the callback hasn't returned. So we get an error.

    So, is there a way to suspend execution while the photo is retrieved? If not, what would be the best practice to update objects that are only updated as part of the callback? It feels like I would have to create a 'update all visible objects on this window' callback that would know about any object that might have ACS data as it's source, then update/redraw any object that makes the call to the 'updateACSObjects' callback... I'm concerned that would look weird having objects updating on the screen while a user is looking at it.....

    Not sure if this is practical or if anyone has a better idea. (I vote for the 'stop execution' while I wait for data route) It feels more familiar and then in the future I could store data in SQLite or ACS using the same database type CRUD calls, just replacing the backend to point to SQL or ACS.

    Ray

    — commented 12 months ago by Ray Belisle

  • What is the error you are getting? Also, what platform and SDK levels are you running against?

    — commented 12 months ago by Adam Paxton

  • Try and add a timer as uploading photos in ACS takes some time. Its meant to be synchronous but for some reason its not.

    setTimeout(function() {
        code();
    }, 5000);

    — commented 10 months ago by Hasnaad Din

why dont you just use the image you just uploaded as a place holder until you can query ACS and get it back from the server... Isnt it the exact same image?

Your Answer

Think you can help? Login to answer this question!