2016-07-11 6 views
0

ProMotion/RedPotionの非常に奇妙な動作のために、私はここ数週間、私の心を失いました。ProMotionのライフサイクルメソッドが異常終了する

私はProMotionのライフサイクルメソッドとAPI呼び出しの一見外れた実行に変わった動作を絞り込んだ。

私はAPIから情報を取得し、APIから返された画像URLに基​​づいて画像を表示するスクリーンを持っています。次のように私はスクリーン、モデルやスタイルシートへの私のプロジェクトを単純化しすぎている:

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 

スタイルシート:

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 

私はいつ実行されているのか把握できるように、そしてあなたが

"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方法火災まず、予想通り、しかしRIGHT on_load方法の途中でAPI呼び出し、火災最後:下記の結果、すべてが故障しています。したがって、view_did_loadメソッドで設定しようとしている画像の属性はnilで失敗します。

私はRubyMotion/ProMotion/RedPotionの新機能ですので、このすべてが間違っているかもしれませんが、確かに何かが間違っているようです。

答えて

0

andrewhavensは、ここに指摘したように:

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

をこの問題が発生した理由は、私のAPI呼び出しが非同期であるということですので、それが実行を終了しwill_appear方法を使用するための時間に、インスタンス変数を設定しませんそれはwill_appearが実行されているときはまだ0になります。

APIの呼び出しが完了した後、適切な属性を設定するために、彼らはこのようなコールバック自体は、たとえば、設定する必要があります。

... 

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

... 

それはうまく動作します。

これにより、何時間ものトラブルシューティングが省けます。

関連する問題