AsyncHttp call within the callback action of another AsyncHttp call

I have an asynchronous call to a web service that retrieves data. This data is then parsed out, and stored in Rhom.

Because there are quite a few records, the saving into Rhom can take up to 20 seconds on the RhoSimulator.

I found this article to do with saving large amounts of data, so thought I'd try and implement it

http://docs.rhomobile.com/faq#how-can-i-create-a-lot-of-objects-in-controller-action

The code I have is as follows:

def do_download

    Rho::AsyncHttp.post(

        :url => url,

        :callback => url_for(:action => :download_callback))

    render :action => :wait_download

end

def download_callback    

    if @params['status'] == 'ok'

        @@parsed = Rho::JSON.parse(@params['body'])

        if @@parsed

            WebView.navigate ( url_for :action => :start_objects_generator )

        end

    end

end

def start_objects_generator

    #callback has to be provided, overwise operation will be syncronous

    Rho::AsyncHttp.get(

        :url => url_for(:action => do_objects_generation),

        :callback => (url_for :action => :generation_callback))

    #render page with progress updated by javascript

    render :action => :wait_update_rhom

end 

def do_objects_generation

    #If you create a lot of objects enable GC at the beginning of action

    GC.enable

    #Do some heavy operations

     @@parsed.each do |item|

          #Save off records to Rhom

     end

end

def generation_callback

    WebView.navigate ( url_for :controller => :Event, :action => :index )

end

As you can see, the do_download function makes a call to the web service, and renders a wait page (wait_download)

When this call completes, I am then telling the web view to navigate to start_object_generator, which then makes another asynchronous call to the controller action do_objects_generation.

I then tell it to render a new wait page, which contains a progress bar (wait_update_rhom).

The problem is that this new wait page is not rendered, and it merely sits on the previous wait page until the second asynchronous call is completed.

Is there any way around this, or am I doing something wrong?

Thanks in advance.

Evgeny Vovchenko
Hello, Read this also:http:/

Hello,

Read this also:

http://docs.rhomobile.com/faq#how-can-i-seed-a-large-amount-of-data-into...

This is about transactions. With transactions it should be less then a second

Also use Create not Save Model method

AsyncHttp parse json automatically if Content-Type is json:

http://docs.rhomobile.com/rhodes/connect-to-web-services#asynchttp-callback

Try to modify your code this way:

If it will not help, try WebView.execute_js to update progress on current page

def do_download

Rho::AsyncHttp.post(

:url => url,

:callback => url_for(:action => :download_callback))

render :action => :wait_download

end

def download_callback

if @params['status'] == 'ok'

@@parsed = Rho::JSON.parse(@params['body'])

if @@parsed

WebView.navigate ( url_for :action => :start_objects_generator )

end

end

end

def start_objects_generator

WebView.navigate ( url_for :action => :start_objects_generator1 )

#render page with progress updated by javascript

render :action => :wait_update_rhom

end

def start_objects_generator1

#callback has to be provided, overwise operation will be syncronous

Rho::AsyncHttp.get(

:url => url_for(:action => do_objects_generation),

:callback => (url_for :action => :generation_callback))

#render page with progress updated by javascript

render :action => :wait_update_rhom

end

def do_objects_generation

#If you create a lot of objects enable GC at the beginning of action

GC.enable

#Do some heavy operations

@@parsed.each do |item|

#Save off records to Rhom

end

end

def generation_callback

WebView.navigate ( url_for :controller => :Event, :action => :index )

end

Vote: 
Vote up!
Vote down!

Points: 1

You voted ‘up’


Darryn Campbell
Hi evgeny,Thanks for the

Hi evgeny,

Thanks for the reply.

I have tried the above code, but unfortunately this doesn't get round the issue, and the wait page is not updated.

But I have now put everything inside a transaction, and it is very quick, so I don't really have any need for the second asynchttp call.

I am curious as to why the display won't update though?

Anyway, thanks a lot for your help.

Vote: 
Vote up!
Vote down!

Points: 0

You voted ‘up’


Ron Wilson
Unfortunately I don't have an

Unfortunately I don't have an answer to this (yet) but wanted to acknowledge that I have the same issue.  In my case I am not making the multiple calls because of volume of data but because I need to grab data from two different web services.  This is on my list this week to try to sort out but if anyone has any further information it would be appreciated.

Thanks...

Vote: 
Vote up!
Vote down!

Points: 0

You voted ‘up’


Log in to post comments