首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >ProMotion生命周期方法无序运行

ProMotion生命周期方法无序运行
EN

Stack Overflow用户
提问于 2016-07-11 02:54:52
回答 1查看 15关注 0票数 0

在过去的几周里,由于ProMotion / RedPotion的一些非常奇怪的行为,我一直在失去理智。

我已经将奇怪的行为缩小到了似乎无序执行促销的生命周期方法和API调用。

我有一个屏幕,它将从我的API中获取信息,并根据API返回的图像url显示一个图像。我已经将项目简化为屏幕、模型和样式表,如下所示:

TestScreen:

代码语言:javascript
运行
复制
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

样式表:

代码语言:javascript
运行
复制
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

型号:

代码语言:javascript
运行
复制
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

我已经放置了printout命令(mp),这样我就可以知道什么时候正在执行,正如您从下面的结果中可以看到的那样,所有事情都是不正常的:

代码语言:javascript
运行
复制
"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\\\"]\">"

on_load方法会像预期的那样首先触发,但是on_load方法中间的API调用将在最后触发。因此,我试图在view_did_load方法中设置的图像attrib为零,并且失败。

我来自Rails背景,我是RubyMotion / ProMotion / RedPotion新手,所以我可能做错了这件事,但这里肯定有些地方很不对劲。

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2016-07-11 04:51:30

正如安德雷霍文斯在这里指出的那样:

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

发生这种情况的原因是我的API调用是异步的,因此它不会及时完成执行和设置实例变量,以便will_appear方法使用它(在will_appear运行时它仍然是零)。

为了正确设置属性,必须在API调用完成后设置它们,例如,在回调本身中设置如下:

代码语言:javascript
运行
复制
...

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

...

那它就能正常工作了。

希望这能节省某人数小时的疑难解答。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/38298509

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档