3 Replies Latest reply on Jul 8, 2012 4:56 PM by Ron Wilson

    AsyncHttp call within the callback action of another AsyncHttp call

    Dave Philips

      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.

        • Re: AsyncHttp call within the callback action of another AsyncHttp call

          Hello,

           

           

           

          Read this also:

           

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

           

           

           

          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

          1 of 1 people found this helpful