我正在寻找一种方法,使用ORMLite将对象列表保存到数据库,并阅读这个问题:Best way to store several ArrayLists in ORMLite for ApplicationSettings
接受的答案对我有意义:
let filterPredicate = NSPredicate(format: "ANY liftName CONTAINS[c] %@", liftNameFilter)
我唯一不理解的是这一行public class YourClass {
@GeneratedId
private int id;
@ForeignCollectionField
private Collection<MyString> bunchOfStrings = new ArrayList<MyString>();
}
public class MyString{
@DatabaseField(canBeNull = true, foreign = true)
private YourClass yourClass;
@DatabaseField
private String text;
}
。为什么我们将private Collection<MyString> bunchOfStrings = new ArrayList<MyString>()
保存为ForeignCollectionField
而不是Collection<MyString>
?使用上面的ArrayList<MyString>
对象时,是否始终需要将其强制转换为bunchOfStrings
?
答案 0 :(得分:2)
为什么我们将ForeignCollectionField保存为Collection 而不是作为ArrayList?
这是设计考虑因素,摘自doc
The field type of orders must be either ForeignCollection<T> or Collection<T> – no other
collections are supported because they are much heavier with many methods to support
使用上面的bunchOfStrings对象时,我们是否总是需要 将其强制转换为ArrayList
你不必初始化那个字段,Ormlite会这样做。因此,只有可用的方法是Collection或ForeignCollection中存在的方法