ProMotion 生命周期方法 运行 乱序
ProMotion lifecycle methods running out of order
过去几周,由于 ProMotion / RedPotion 的一些非常奇怪的行为,我一直在失去理智。
我已将奇怪的行为缩小为 ProMotion 的生命周期方法和 API 调用看似乱序的执行。
我有一个屏幕,可以从我的 API 获取信息,并根据 API 返回的图像 url 显示图像。我已将我的项目过度简化为屏幕、模型和样式表,如下所示:
测试屏幕:
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
样式表:
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
型号:
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
我已经放置了打印输出命令 (mp),这样我就可以弄清楚什么时候执行了什么,正如您从下面的结果中看到的那样,一切都乱七八糟:
"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 方法首先触发,正如预期的那样,但是 API 调用,就在 on_load 方法的中间,最后触发。因此,我试图在 view_did_load 方法中设置的图像属性为 nil 并且失败了。
我来自 Rails 背景,我是 RubyMotion / ProMotion / RedPotion 的新手,所以我可能会把这一切都弄错了,但这里肯定有些地方很不对劲。
正如 andrewhavens 在这里指出的那样:
https://github.com/infinitered/redpotion/issues/164
发生这种情况的原因是我的API调用是异步的,所以它不会完成执行并及时设置实例变量供will_appear方法使用它(它仍然是will_appear 运行时为零)。
为了正确设置属性,必须在 API 调用完成后设置它们,例如在回调本身中这样设置:
...
if response.success?
mp "face returned from API:"
mp face.inspect
@face = face
rmq(:face_image).attr(remote_image: @face.picture)
else
...
那就可以正常工作了。
希望这可以节省一些人的故障排除时间。
过去几周,由于 ProMotion / RedPotion 的一些非常奇怪的行为,我一直在失去理智。
我已将奇怪的行为缩小为 ProMotion 的生命周期方法和 API 调用看似乱序的执行。
我有一个屏幕,可以从我的 API 获取信息,并根据 API 返回的图像 url 显示图像。我已将我的项目过度简化为屏幕、模型和样式表,如下所示:
测试屏幕:
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
样式表:
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
型号:
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
我已经放置了打印输出命令 (mp),这样我就可以弄清楚什么时候执行了什么,正如您从下面的结果中看到的那样,一切都乱七八糟:
"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 方法首先触发,正如预期的那样,但是 API 调用,就在 on_load 方法的中间,最后触发。因此,我试图在 view_did_load 方法中设置的图像属性为 nil 并且失败了。
我来自 Rails 背景,我是 RubyMotion / ProMotion / RedPotion 的新手,所以我可能会把这一切都弄错了,但这里肯定有些地方很不对劲。
正如 andrewhavens 在这里指出的那样:
https://github.com/infinitered/redpotion/issues/164
发生这种情况的原因是我的API调用是异步的,所以它不会完成执行并及时设置实例变量供will_appear方法使用它(它仍然是will_appear 运行时为零)。
为了正确设置属性,必须在 API 调用完成后设置它们,例如在回调本身中这样设置:
...
if response.success?
mp "face returned from API:"
mp face.inspect
@face = face
rmq(:face_image).attr(remote_image: @face.picture)
else
...
那就可以正常工作了。
希望这可以节省一些人的故障排除时间。