scorpiontahir02
Visitor
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
01-23-2014
11:38 PM
roUrlTransfer.AsyncGetToFile not working
Hi,
I am working on an application in which i need to fetch some images from internet. I want to do it asynchronusly as not all the images are used at the same time and i do not want my application to halt for some time. But the problem is roUrlTransfer.AsyncGetToFile is not creating file in tmp directory. I have add my async function in urlUtils.brs and here is the code:
It does not work when i do this:
but it does work when i do this:
and i am calling this function like this:
So can you please guide why it is not working in completely async way (1st implementation)?
I am using the images after 20 seconds so this cannot be the case that images could not be downloaded as in 2nd implementation 1 image takes only 1 second or 2.
I am working on an application in which i need to fetch some images from internet. I want to do it asynchronusly as not all the images are used at the same time and i do not want my application to halt for some time. But the problem is roUrlTransfer.AsyncGetToFile is not creating file in tmp directory. I have add my async function in urlUtils.brs and here is the code:
It does not work when i do this:
function http_async_get_to_file(filename as String)
m.Http.EnableFreshConnection(true) 'Don't reuse existing connections
res = m.Http.AsyncGetToFile(filename)
end function
but it does work when i do this:
function http_async_get_to_file(filename as String)
m.Http.EnableFreshConnection(true) 'Don't reuse existing connections
res = m.Http.AsyncGetToFile(filename)
if (res)
event = wait(0, m.Http.GetPort())
if type(event) = "roUrlEvent"
else if event = invalid
m.Http.AsyncCancel()
return false
endif
endif
return true
end function
and i am calling this function like this:
for i = 1 to m.adImageCount - 1
asyncGetImage(m.content.adImageUrls.GetEntry(i))
end for
function asyncGetImage(url As string)
fileName = url
while fileName.Instr("/") <> -1
fileName = fileName.Mid(fileName.Instr("/") + 1)
end while
http = NewHttp(url)
print http.AsyncGetToFile("tmp:/" + fileName)
end function
So can you please guide why it is not working in completely async way (1st implementation)?
I am using the images after 20 seconds so this cannot be the case that images could not be downloaded as in 2nd implementation 1 image takes only 1 second or 2.
3 REPLIES 3

TheEndless
Channel Surfer
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
01-24-2014
12:07 AM
Re: roUrlTransfer.AsyncGetToFile not working
The problem is that your "http" variable is going out of scope as soon as your asyncGetImage function returns, so it's getting disposed before it has a chance to complete the transfer. You could try something like this, instead:
That will keep the url transfers in scope via the imageTransfers array, which you can clean up after the images have been downloaded.
imageTransfers = []
for i = 1 to m.adImageCount - 1
imageTransfers.Push(asyncGetImage(m.content.adImageUrls.GetEntry(i)))
end for
function asyncGetImage(url As string)
fileName = url
while fileName.Instr("/") <> -1
fileName = fileName.Mid(fileName.Instr("/") + 1)
end while
http = NewHttp(url)
print http.AsyncGetToFile("tmp:/" + fileName)
return http
end function
That will keep the url transfers in scope via the imageTransfers array, which you can clean up after the images have been downloaded.
My Channels: http://roku.permanence.com - Twitter: @TheEndlessDev
Instant Watch Browser (NetflixIWB), Aquarium Screensaver (AQUARIUM), Clever Clocks Screensaver (CLEVERCLOCKS), iTunes Podcasts (ITPC), My Channels (MYCHANNELS)
Instant Watch Browser (NetflixIWB), Aquarium Screensaver (AQUARIUM), Clever Clocks Screensaver (CLEVERCLOCKS), iTunes Podcasts (ITPC), My Channels (MYCHANNELS)
scorpiontahir02
Visitor
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
01-24-2014
12:22 AM
Re: roUrlTransfer.AsyncGetToFile not working
Thank you very much TheEndless! It solved the problem. I never thought it could be an issue regarding to scope of objects....
phoang
Visitor
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
05-16-2014
10:28 AM
Re: roUrlTransfer.AsyncGetToFile not working
Is there a complete example of this because I'm having trouble getting the actual image so I can create a bitmap object from it.