Keeping track of http.request() success/failure with mutiple files?

@Simeon, i started to use http.request() to load in some files from a filesever. This all works very well and is a quite powerful way to load data into my program as the files don’t have to be locally on the ipad. This all works nicely.

My problem is that i load in five files and they load in asynchrously calling callbacks depending whether the file is loaded with success or failure. As things are, i don’t really see a way to keep track of which of my multiple files have loaded successfully or not. A solution would be if the callback also passed the URLname back to the success/failure callbacks. That way i could keep track of which of the files worked or did not.

@piinthesky It’s been quite a while since I played with http. I don’t know how you’re doing you requests, but if the https are in a table and you go thru the table for each request, then you should be able to know where you are in the table and display the name if it didn’t load.

@piinthesky Here’s an example of trying to get 4 images. The third image doesn’t exist so it shows an error. I print a success or error message along with the http address. I have the img commented out.

displayMode(STANDARD)

function setup()
    tab={
        "http://www.freeimageslive.com/galleries/home/playroom/pics/objects00016g.jpg",
        "http://www.freeimageslive.com/galleries/transtech/auto/pics/03250046.jpg",
        "http://www.freeimageslive.com/galleries/transtech/auto/pics/car00709.jpg",
        "http://www.freeimageslive.com/galleries/transtech/auto/pics/car00708.jpg",
        }
    z=0
    getNext=true
end

function draw()
    background(40,40,50)
    if getNext then
        getNext=false
        z=z+1
        if z<=#tab then
            http.request(tab[z],gotImage,noImage)
        end
    end
    if img~=nil then
        sprite(img,WIDTH/2,HEIGHT/2)
    end
end

function noImage()
    print("Error:  ",tab[z])
    getNext=true
end

function gotImage(image1,status,header)
    print("Success:  ",tab[z])
    --img=image1 
    getNext=true  
end

@dave1707 thanks, yes that would work, but needs dedicated handling in the draw. I think it would be better if the callback would indicate to which http.request it corresponds by passing back the initial url.

@piinthesky As far as I can tell, the next request doesn’t start until either the previous one finishes (gotImage) or fails (noImage). You can try different sized files if you want to test what you’re asking.

If you use a closure you can bind the URL into the closure so you know what the request relates to, and then act with that URL when the callback fires:

(Using dave’s images)

-- Multi-Request
function setup()

    urls = {
        "http://www.freeimageslive.com/galleries/home/playroom/pics/objects00016g.jpg",
        "http://www.freeimageslive.com/galleries/transtech/auto/pics/03250046.jpg",
        "http://www.freeimageslive.com/galleries/transtech/auto/pics/car00709.jpg",
        "http://www.freeimageslive.com/galleries/transtech/auto/pics/car00708.jpg",
    }
    
    images = {}
    
    for _,url in pairs(urls) do
        
        http.request(url, function(img)
            table.insert(images, {image=img, url=url})
        end, 
        
        function()
            print("Error loading '" .. url .. "'")
        end)
        
    end
end

function draw()
    -- This sets a dark background color 
    background(40, 40, 50)

    -- This sets the line thickness
    strokeWidth(5)

    -- Do your drawing here
    local offset = 0
    for _,data in pairs(images) do
        
        sprite(data.image, 400 + offset, HEIGHT/2, 300)
        
        textWrapWidth(300)
        text(data.url, 400 + offset, HEIGHT - 200)
        
        offset = offset + 300
    end
end

@piinthesky if you restart the viewer a couple times you will see sometimes the requests resolve in a different order but the correct URLs are always listed atop the correct images

@Simeon, perfect! thanks