2

有没有人让 Rubymotion 使用 RestKit 的 RKObjectManager 并成功地从服务器加载了一些对象?我有很多麻烦。我让 RestKit 的 RKClient 运行良好。我可以成功获取并发布,这很棒。但是我不能用 RKObjectManager 加载资源。我的 Rakefile 看起来像这样:

$:.unshift("/Library/RubyMotion/lib")
require 'motion/project'

Motion::Project::App.setup do |app|
  # Use `rake config' to see complete project settings.
  app.name = 'RestKitTest'
  app.frameworks += %w(CoreData CFNetwork Security MobileCoreServices SystemConfiguration QuartzCore)
  app.vendor_project('vendor/RestKit', :xcode, :target => 'RestKit', :headers_dir => '../Headers/RestKit/')
end

我的应用程序委托如下所示:

class AppDelegate
  def application(application, didFinishLaunchingWithOptions:launchOptions)
    @window = UIWindow.alloc.initWithFrame UIScreen.mainScreen.bounds
    @window.rootViewController = TestViewController.alloc.init
    @window.rootViewController.wantsFullScreenLayout = true
    @window.makeKeyAndVisible
    true
  end
end

我的存根 TestViewController 看起来像这样:

class TestViewController < UIViewController
  def init
    super

    puts "checkpoint 1"

    manager = RKObjectManager.managerWithBaseURLString "http://mlpong.herokuapp.com"
    puts "checkpoint 2"

    mapping = RKObjectMapping.mappingForClass League.class
    puts "checkpoint 3"

    # mapping.mapAttributes("id", "name", "url", nil)
    mapping.mapKeyPath("id", toAttribute:"id")
    mapping.mapKeyPath("name", toAttribute:"name")
    mapping.mapKeyPath("url", toAttribute:"url")
    puts "checkpoint 4"

    manager.mappingProvider.setObjectMapping(mapping, forKeyPath:"")
    puts "checkpoint 5"

    manager.loadObjectsAtResourcePath("/leagues.json?auth_token=my_auth_token", delegate:self)
    puts "checkpoint 6"

    self
  end

  def objectLoader(loader, didFailWithError:error)
    puts "failed with error: #{error.domain}"
  end

  def objectLoader(loader, didLoadObjects:objects)
    puts "success!"
  end
end

不幸的是,您无法使用此确切代码进行测试,因为您需要我的站点身份验证令牌。

RKObjectMapping类的mapAttributes方法(上面已注释掉)不起作用。如果我离开它,应用程序会输出检查点 1-3,然后会发疯。rake --trace揭示了这一点:

** Invoke default (first_time)
** Invoke simulator (first_time)
** Invoke build:simulator (first_time)
** Execute build:simulator
** Execute simulator
/usr/bin/defaults write com.apple.iphonesimulator "SimulateDevice" "'iPhone'"
DYLD_FRAMEWORK_PATH="/Applications/Xcode.app/Contents/Developer/../Frameworks":"/Applications/Xcode.app/Contents/Developer/../OtherFrameworks" /Library/RubyMotion/bin/sim 2 1 5.1 "/Applications/Xcode.app/Contents/Developer" "./build/iPhoneSimulator-5.1-Development/RestKitTest.app"
checkpoint 1
checkpoint 2
checkpoint 3
(main)> ** Execute default

如果我注释掉 mapAttributes 行,并使用另一个(更长)版本的对象映射(在检查点 4 之前未注释的 3 行),我会通过所有检查点,但是当我rake --trace时会收到这个:

** Invoke default (first_time)
** Invoke simulator (first_time)
** Invoke build:simulator (first_time)
** Execute build:simulator
** Execute simulator
/usr/bin/defaults write com.apple.iphonesimulator "SimulateDevice" "'iPhone'"
DYLD_FRAMEWORK_PATH="/Applications/Xcode.app/Contents/Developer/../Frameworks":"/Applications/Xcode.app/Contents/Developer/../OtherFrameworks" /Library/RubyMotion/bin/sim 2 1 5.1 "/Applications/Xcode.app/Contents/Developer" "./build/iPhoneSimulator-5.1-Development/RestKitTest.app"
checkpoint 1
checkpoint 2
checkpoint 3
checkpoint 4
checkpoint 5
checkpoint 6
(main)> terminate called without an active exception** Execute default

我整天都被这些错误所困扰。如果有人有任何想法,请告诉我。非常感谢帮助。谢谢,

帕春

4

1 回答 1

0

您调用了一些不存在的方法。

manager.mappingProvider.setObjectMapping(mapping, forKeyPath:"")
puts "checkpoint 5"

manager.loadObjectsAtResourcePath("/leagues.json?auth_token=my_auth_token", delegate:self)
puts "checkpoint 6"

应该。

manager.loadObjectsAtResourcePath("/leagues.json?auth_token=my_auth_token", mapping:mapping, delegate:self)
puts "checkpoint 5"

请参阅我在以下位置找到答案的类似问题:loadObjectsAtResourcePath (RestKit)

于 2013-12-15T07:38:09.960 回答