0

使用 RubyMotion(第一次!),我想使用 Twitter 的搜索 API 为一些用户检索一些最近的推文,所以将下面的类放在一起。

的值tweets始终是一个空数组。我怀疑它BW::HTTP.get(url)会产生导致问题的自己的线程。

真的,我只是想twitter_search_results回来response.body.to_str,但我不知道该怎么做。

如何使用 RubyMotion(或 BubbleWrap)将一组 Tweet 对象放入我的 UIViewController?

class TweetsController
  def initialize
    @twitter_accounts = %w(dhh google)
    @tweets = []
  end

  def tweets
    twitter_search_results
    puts @tweets.count
    @tweets
  end

  def create_tweets(response)
    BW::JSON.parse(response)["results"].each do |result|
      @tweets << Tweet.new(result)
    end
  end

  def twitter_search_results
    query = @twitter_accounts.map{ |account| "from:#{account}" }.join(" OR ")
    url = "http://search.twitter.com/search.json?q=#{query}"
    BW::HTTP.get(url) do |response|
      create_tweets(response.body.to_str)
    end
  end
end

class TwitterViewController < UIViewController
  def viewDidLoad
    super
    self.view.backgroundColor = UIColor.blueColor
    @table = UITableView.alloc.initWithFrame(self.view.bounds)
    self.view.addSubview @table
    @table.dataSource = self
    @tweets_controller = TweetsController.new
  end

  def initWithNibName(name, bundle: bundle)
    super
    self.tabBarItem = UITabBarItem.alloc.initWithTitle(
      "Twitter",
      image: UIImage.imageNamed('twitter.png'),
      tag: 1)
    self
  end

  def tableView(tableView, numberOfRowsInSection: section)
    @tweets_controller.tweets.length
  end

  def tableView(tableView, cellForRowAtIndexPath: indexPath)
    @reuse_id = "Tweet"
    cell = UITableViewCell.alloc.initWithStyle(UITableViewCellStyleDefault, reuseIdentifier:@reuse_id)
    cell.textLabel.text = @tweets_controller.tweets[indexPath.row].text
    return cell
  end
end

class Tweet
  attr_reader :created_at, :from_user, :text
  def initialize(tweet_result)
    @created_at = tweet_result["created_at"]
    @from_user = tweet_result["from_user"]
    @text = tweet_result["text"]
  end
end
4

2 回答 2

0

这里的问题是异步性。我想你快到了,但是create_tweets之前没有调用该方法puts @tweets。在这种情况下,我建议使用通知,因为我认为它们很好;-)

TweetsReady = 'TweetsReady'  # constants are nice
NSNotificationCenter.defaultCenter.postNotificationName(TweetsReady, object:@tweets)

In your controller, register for this notification in `viewWillAppear` and unregister in `viewWillDisappear`

NSNotificationCenter.defaultCenter.addObserver(self, selector: 'tweets_ready:', name: TweetsReady, object:nil)  # object:nil means 'register for all events, not just ones associated with 'object'
# ...
NSNotificationCenter.defaultCenter.removeObserver(self, name:TweetsReady, object:nil)

并且您的 tweets_ready 方法应该实现您的 UI 更改。

def tweets_ready(notification)
  @table.reloadData
end
于 2012-11-07T18:07:31.683 回答
0

完整的控制器代码如下。我也把项目放在了 GitHub 上

class TweetsController
  def initialize
    @twitter_accounts = %w(dhh google)
    @tweets = []
    create_tweets
  end

  def tweets
    @tweets
  end

  def create_tweets
    json_data = twitter_search_results.dataUsingEncoding(NSUTF8StringEncoding)
    e = Pointer.new(:object)
    dict = NSJSONSerialization.JSONObjectWithData(json_data, options:0, error: e)
    dict["results"].each do |result|
      p result.class
      p result
      @tweets << Tweet.new(result)
    end
  end

  def twitter_search_results
    query = @twitter_accounts.map{ |account| "from:#{account}" }.join(" OR ")
    url_string = "http://search.twitter.com/search.json?q=#{query}"
    url_string_escaped = url_string.stringByAddingPercentEscapesUsingEncoding(NSUTF8StringEncoding)
    url = NSURL.URLWithString(url_string_escaped)
    request = NSURLRequest.requestWithURL(url)
    response = nil
    error = nil
    data = NSURLConnection.sendSynchronousRequest(request, returningResponse: response, error: error)
    raise "BOOM!" unless (data.length > 0 && error.nil?)
    json = NSString.alloc.initWithData(data, encoding: NSUTF8StringEncoding)
  end
end
于 2012-11-08T16:55:20.593 回答