RubyMotion异步编程与气泡布 [英] RubyMotion async programming with BubbleWrap

查看:147
本文介绍了RubyMotion异步编程与气泡布的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我很困惑着如何使用大量异步code时写的体面code。

I am confused with how to write decent code when using a lot of asynchronous code.

在以下code片段我登录得到验证cookie并使用cookie的下一个请求获得的项目名称列表(作为一个例子):

In the following code snippet I log in to get the authentication cookie and use that cookie for the next request to get a list of projects name (as an example):

def self.populateProjectsTable(projects_controller)
  payload = {email: "email", password: "pass"}
  HTTP.post("http://example.com/login", {payload: payload}) do |response|
    authCookie = response.headers['Set-Cookie']
    HTTP.get("http://example.com/projects.json", {cookie: authCookie}) do |response|
      projects = JSON.parse(response.body.to_str)
      projects_controller.projects = projects
      projects_controller.reloadData
    end
  end
end

虽然这将工作code感觉很脏。没有真正遵循单一职责原则。我想一些方法来提取这一点:

While this will work the code feels dirty. Not really following the single responsibility principle. I would like to extract this in a few methods:

def self.populateProjectsTable(projects_controller)
  @taskList = TaskList.new
  @taskList.doLogin
  projects = @taskList.getProjects
  projects_controller.projects = projects
  projects_controller.reloadData
end

def doLogin
  payload = {email: "email", password: "pass"}
  HTTP.post("http://example.com/login", {payload: payload}) do |response|
    @authCookie = response.headers['Set-Cookie']
  end
end

def getProjects
  HTTP.get("http://example.com/projects.json", {cookie: @authCookie}) do |response|
    projects = JSON.parse(response.body.to_str)
  end
end

这显然是行不通的。在 getProjects 方法之前, doLogin 叫结束,项目仅在块的范围内已知的,不给将数据备份到 populateProjectsTable 方法。

This obviously does not work. The getProjects method is called before doLogin is finished and the projects are only known in the scope of the block, not giving back the data to the populateProjectsTable method.

如何一个节目这样的应用程序,而在第一个例子中所示的嵌套

How does one program such applications without the nesting shown in the first example?

推荐答案

您不是要完全摆脱嵌套了。以艾伦的答案,按摩它一下,这是什么我已经出来了。它涉及到传递一个块通过几个方法。

You're not going to totally get away from the nesting. Taking Alan's answer and massaging it a bit, this is what I've come up with. It involves passing a block through a couple of methods.

def self.populateProjectsTable(projects_controller)
  @taskList = TaskList.new
  @taskList.loginAndGetProjects do |projects|
    projects_controller.projects = projects
    projects_controller.reloadData
  end
end

def loginAndGetProjects(&block)
  payload = {email: "email", password: "pass"}
  HTTP.post("http://example.com/login", {payload: payload}) do |response|
    @authCookie = response.headers['Set-Cookie']
    getProjects(&block)
  end
end

def getProjects(&block)
  HTTP.get("http://example.com/projects.json", {cookie: @authCookie}) do |response|
    projects = JSON.parse(response.body.to_str)
    block.call(projects)
  end
end

这篇关于RubyMotion异步编程与气泡布的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆