当我在AsyncTask#doInBackground中使用Android Context时,它是否是线程安全的?上下文是通过构造函数或来自周围Activity的getApplicationContext()提供的.这个简单的问题在stackoverflow上被问到了很多问题,但在哪里我发现不是一个明确的答案?
例如,在doInBackground()中,我使用上下文来实例化DAO类.
@Override
protected Void doInBackground(Void... params) {
ExampleDao dao = new ExampleDao(context);
...
}
Run Code Online (Sandbox Code Playgroud)
我看到了几个这样做的例子,但是我无法想象这是线程安全的,因为现在主要的脚步(UI线程)和工作线程访问了上下文.
以下模型给出:
class Question < ActiveRecord::Base
has_many :answers
end
class Answers < ActiveRecord::Base
belongs_to: question
validates :comment, presence: true
end
Run Code Online (Sandbox Code Playgroud)
打电话的时候
question = Question.new
question.answers.build
question.valid?
Run Code Online (Sandbox Code Playgroud)
valid?返回,false因为相关的答案无效.写作时
has_many :answers, validate: false
Run Code Online (Sandbox Code Playgroud)
在Question valid?回报中true.
它是一个错误还是在使用has_many相关模型时自动验证是否需要?Rails指南明确解释了validate_associated与has_many关系的使用:http://guides.rubyonrails.org/active_record_validations_callbacks.html#validates_associated
我想在SimpleForm gem的帮助下创建表单上输入的maxlength html属性.我知道我可以通过在创建表单时手动传入maxlength属性来完成此操作,例如:
<%= f.input :username, input_html: { maxlength: 20 } %>
Run Code Online (Sandbox Code Playgroud)
但这并不是我想要的,因为根据SimpleForm配置文件中的注释,您应该启用maxlength扩展,当给出最大长度验证时,该扩展会自动将此html属性添加到字符串属性的输入标记.
## Optional extensions
# They are disabled unless you pass `f.input EXTENSION_NAME => :lookup`
# to the input. If so, they will retrieve the values from the model
# if any exists. If you want to enable the lookup for any of those
# extensions by default, you can change `b.optional` to `b.use`.
# Calculates maxlength from length validations for string inputs
b.use :maxlength
Run Code Online (Sandbox Code Playgroud)
不幸的是,所提到的两种可能性都不起作 我是否完全误解了maxlength扩展的使用?