Search code examples
rubymotionrubymotion-promotion

ProMotion lifecycle methods running out of order


I have been losing my mind over the last couple of weeks due to some very strange behaviour from ProMotion / RedPotion.

I have narrowed down the strange behaviour to a seemingly out of order execution of ProMotion's lifecycle methods and an API call.

I have a Screen that will get info from my API and display an image based on the image url returned from the API. I have oversimplified my project to a Screen, a Model and a Stylesheet as follows:

TestScreen:

class TestScreen < PM::Screen
  title "Your title here"
  stylesheet TestScreenStylesheet

  def on_load
    mp "ran on_load"
    @face_image = append!(UIImageView, :face_image)
    mp "getting data from API"
    Face.get(1) do |response, face|
      if response.success?
        mp "face returned from API:"
        mp face.inspect
        @face = face
      else
        @face = [{attributes: {name: "No faces found"}}]
      end
    end
    mp "should have printed data obtained from API"
  end

  def will_appear
    mp "ran on_will_appear"
    mp "face in will_appear:"
    if @face
      rmq(:face_image).attr(remote_image: @face.picture)
    else
      mp "@face is nil!!!"
    end
  end

end

Stylesheet:

class TestScreenStylesheet < ApplicationStylesheet

  def setup
  end

  def root_view(st)
    st.background_color = color.white
  end

  def face_image(st)
    st.frame = {l: 30, t: 140, w: 250, h: 250}
    st.placeholder_image = image.resource("placeholder_image.png")
  end
end

Model:

class Face
  attr_accessor :id, :name, :description, :local_description, :picture, :bio_picture, :star_ranking, :status, :facetype_id, :answers

  def initialize(response)
    @id = response[:data][0][:id]
    @name = response[:data][0][:attributes][:name]
    @description = response[:data][0][:attributes][:description]
    @local_description = response[:data][0][:attributes][:local_description]
    @picture = response[:data][0][:attributes][:picture]
    @bio_picture = response[:data][0][:attributes][:bio_picture]
    @star_ranking = response[:data][0][:attributes][:star_ranking]
    @status = response[:data][0][:attributes][:status]
    @facetype_id = response[:data][0][:attributes][:facetype_id]
    @answers = response[:data][0][:attributes][:answers]

  end

  def self.get(category_id,&callback)
    ApiClient.client.get "random_face?mycategory=#{category_id}" do |response|
      model = nil
      if response.success?
        model = self.new(response.object)
      end
      callback.call(response, model)
    end
  end

end

I have placed printout commands (mp) so that I can figure out what is being executed when, and as you can see from the results below, everything is out of order:

"ran on_load"
"getting data from API"
"should have printed data obtained from API"
"ran on_will_appear"
"face in will_appear:"
"@face is nil!!!"
"face returned from API:"
"#<Face:0x113c37c90 @id=\"1\" @name=\"Leonel Messi\" @description=\"Leonel Messi es un jugador portugués de fútbol, 4 veces ganador del Balón de Oro\" @local_description=\"translation missing: en.Leonel Messi_description\" @picture=\"default_url\" @bio_picture=\"default_url\" @star_ranking=1 @status=\"active\" @facetype_id=nil @answers=\"[\\\"Kun Aguero\\\", \\\"Nicolas Shevchenko\\\", \\\"Leonel Messi\\\", \\\"Clarence Seedorf\\\"]\">"

The on_load method fires first, as expected, but the API call, RIGHT IN THE MIDDLE OF THE on_load method, fires last. Therefore the image attrib I am trying to set in the view_did_load method is nil and fails.

I come from a Rails background and I am new to RubyMotion / ProMotion / RedPotion so I may be going about this all wrong, but it certainly seems something is very wrong here.


Solution

  • As andrewhavens pointed out here:

    https://github.com/infinitered/redpotion/issues/164

    the reason this happens is that my API call is asynchronous, so it will not finish executing and set the instance variable in time for the will_appear method to use it (it will still be nil at the time will_appear runs).

    In order to set the attributes properly, they must be set AFTER the API call completes, for example in the callback itself like this:

    ...
    
    if response.success?
      mp "face returned from API:"
      mp face.inspect
      @face = face
      rmq(:face_image).attr(remote_image: @face.picture)
    else
    
    ...
    

    then it will work just fine.

    Hope this saves someone hours of troubleshooting.