我需要通过发出异步URL请求来遍历具有未知数量节点的树。我目前的方法如下:
class NumberValidator < ActiveModel::Validator
def validate(record, attribute, value)
puts record
puts attribute
puts value
end
end
require File.expand_path('../boot', __FILE__)
require "rails"
# Pick the frameworks you want:
require "active_model/railtie"
require "active_job/railtie"
require "active_record/railtie"
require "action_controller/railtie"
require "action_mailer/railtie"
require "action_view/railtie"
require "sprockets/railtie"
# require "rails/test_unit/railtie"
# Require the gems listed in Gemfile, including any gems
# you've limited to :test, :development, or :production.
Bundler.require(*Rails.groups)
module ApplicationName
class Application < Rails::Application
# Settings in config/environments/* take precedence over those specified here.
# Application configuration should go into files in config/initializers
# -- all .rb files in that directory are automatically loaded.
# Set Time.zone default to the specified zone and make Active Record auto-convert to this zone.
# Run "rake -D time" for a list of tasks for finding time zone names. Default is UTC.
# config.time_zone = 'Central Time (US & Canada)'
# The default locale is :en and all translations from config/locales/*.rb,yml are auto loaded.
# config.i18n.load_path += Dir[Rails.root.join('my', 'locales', '*.{rb,yml}').to_s]
# config.i18n.default_locale = :de
# Do not swallow errors in after_commit/after_rollback callbacks.
config.active_record.raise_in_transactional_callbacks = true
config.autoload_paths += %W["#{config.root}/app/validators/"]
end
end
我目前的方法存在的问题是我不知道加载何时完成。我不在乎是否有些孩子无法加载,但我需要在父母的所有孩子(及其子女)被加载时进行UI更新。
我研究了承诺和调度组等各种方法,但我很难让它发挥作用。我正在使用Swift 2,理想情况下,父节点会有这样的函数:
class Parent {
var foo: String
var id: Int
var children: [Child]?
func loadChildren() {
for child in self.children {
self.getAllChildren(child)
}
}
func getAllChildren(current: Child) {
current.load() {(success) in
if (success) {
if let children = current.children {
for child in children {
self.getAllChildren(child)
}
}
}
}
}
}
class Child {
var bar: String
var id: Int
var children: [Child]?
func load(success: (Bool) -> ()) {
// Load from API and initalize values
}
}