使用`on_load`回调中的`on_refresh`回调不刷新Tabledata

时间:2015-04-19 16:00:28

标签: rubymotion rubymotion-promotion

我的tableview不会重新加载数据,它只显示noob但不会显示更新的数据。

我是否遗漏了代码中的任何步骤?

class ReservoirsScreen < PM::TableScreen
  title "I want to refresh"
  refreshable
  def on_load
    @returned_data ||= [{title:'noob'}]
    on_refresh
  end
  def table_data
    [{
      cells: @returned_data.map do |reservoir|
        {
          title: reservoir["title"],
          action: :select_reservoir,
          arguments: { reservoir: reservoir["title"] }
        }
      end
    }]
  end


  def select_reservoir(reservoir)
    PM.logger.info reservoir
  end 


  def on_refresh
    start_refreshing
    get_reserviors
    stop_refreshing
    update_table_data          
  end  

  def get_reserviors
    start_refreshing
    url_string = "http://128.199.223.114:10080/"
    AFMotion::JSON.get(url_string) do |result|
      if result.success?
        @returned_data = []
        result.object["data"].each do |r|
          @returned_data << {
            title: [r["reservoirName"], r["immediatePercentage"]].join(":")
          }
        end
      else
        @returned_data = ['failed','d']
      end
    end 
    stop_refreshing
    update_table_data      
  end

end

1 个答案:

答案 0 :(得分:1)

AFMotion使用异步回调,因此您的代码实际上是这样执行的:

  • 开始刷新
  • 发送JSON请求
  • 立即停止刷新
  • 更新表格数据
  • 收到JSON的回复

相反,将stop_refreshingupdate_table_data行移动到回调中(if result.success? / else块),这样在JSON响应之前它们不会被执行收到:

if result.success?
  @returned_data = []
  result.object["data"].each do |r|
    @returned_data << {
      title: [r["reservoirName"], r["immediatePercentage"]].join(":")
    }
  end
  update_table_data
  stop_refreshing
else
  @returned_data = ['failed','d']
  update_table_data
  stop_refreshing
end