6 Replies Latest reply on Nov 24, 2014 6:46 AM by Tom Carvin

    Calling Model.search from within a search callback hangs client

    Tom Carvin

      Accidentally posted this in RhoConnect section first, but it really belongs here...

       

      I've got a model (Item) that gets populated via a call to Item.search.  In the callback, when @params['status'] is complete, a second search against Item is initiated.  Unfortunately this appears to block, as logging after that statement does happen, and even the ensure block (with more logging) is not run.  I'm stumped, any ideas?

        • Re: Calling Model.search from within a search callback hangs client
          Jon Tara
          I've got a model (Item) that gets populated via a call to Item.search


          What is "search"? Did you mean "find"?


            In the callback, when @params['status'] is complete, a second search against Item is initiated.


          Oh, do you mean that you have defined a method called "search" in a controller for a model called Item?  Not giving us much to go on here...


          Your problem is with your controller, not with Rhom.


          Each request to the Rhodes server is queued. Rhodes spins-up a controller instance, runs the controller method, the method eventually returns, and the controller instance is destroyed. Each request gets a new controller instance, with instance variables set with @params, etc. from that request.


          Callbacks are really just server requests. They are queued just like any other server request.


          To queue a request from within a controller instance, you can either use Network, passing a callback URL, or else use Rho::Timer with an interval of 0.


          If this has something to do with your "one-way-synchronization" problem, might I suggest looking at using lambda callbacks? That's my preferred way of handling Rho::Network callbacks in 5.x. You don't need to use a controller. You can use lambda callbacks to implement asynchronous network operations without needed to use a controller. (In fact, I just finished up a "one-way sync" implemented in a stand-alone module along with a mixin that gets included in those models that participate in an API, using lambda callbacks. I don't delete records after they have been uploaded, since our requirement is a bit different than yours.

            • Re: Re: Calling Model.search from within a search callback hangs client
              Tom Carvin

              No, I mean search, as seen here: http://docs.rhomobile.com/en/2.2.0/rhodes/synchronization#filtering-datasets-with-search

               

              This is an excerpt from that page:

               

               

               

              def search

                page = @params['page'] || 0

                page_size = @params['page_size'] || 10

                Contact.search(

                 :from => 'search',

                 :search_params => {

                 :FirstName => @params['FirstName'],

                 :LastName => @params['LastName'],

                 :Company => @params['Company']

                },

                 :offset => page * page_size,

                 :max_results => page_size,

                 :callback => url_for(:action => :search_callback),

                 :callback_param => ""

                )

                render :action => :search_wait

              end

              Your callback might look like:

              def search_callback
                status = @params["status"]
                if (status and status == "ok")
                  WebView.navigate(
                    url_for(
                      :action => :show_page,
                      :query => @params['search_params']
                    )
                  )
                else
                  render :action => :search_error
                end
              end

              The search is running fine on the first invocation, and the data comes down from the RhoConnect source adapter.  But the problem I have is that in that "search_callback", on success some logic runs that determines it needs a further search, and at that point the second Model.search blocks and I'm stuck.

            • Re: Calling Model.search from within a search callback hangs client
              Tom Carvin

              Yeah, using the timer to kick each search was my next attempt at a work-around.  I don't see why it would be needed, as the other sync APIs (do_syncsource) that callback with a completion status allow you to then kick off the next model sync from the callback just fine.  It is the same underlying sync plumbing for both, so Timer feels kludgey to me.