我想使用扩展realm对象的类作为常规类,但是我的getFilter()中有例外
ThrowingException 8,Realm从错误的线程访问。在/Users/cm/Realm/realm-java/realm/realm-library/src/main/cpp/io_realm_internal_OsResults.cpp第157行,。 抛出了异常:从不正确的线程访问Realm。在/Users/cm/Realm/realm-java/realm/realm-library/src/main/cpp/io_realm_internal_OsResults.cpp第157行
这是我的适配器
List<ProductRealm> productsList, productsFiltered;
Context context;
public ProductAdapter(List<ProductRealm> productsList, Context context) {
this.productsList = productsList;
this.context = context;
}
@Override
public Filter getFilter() {
Filter filter = new Filter() {
@Override
protected FilterResults performFiltering(CharSequence charSequence) {
String charString = charSequence.toString();
if (charString.isEmpty()) {
productsFiltered = productsList;
} else {
List<ProductRealm> filteredList = new ArrayList<>();
for (ProductRealm row : productsList) {
if (row.getName().toLowerCase().contains(charString.toLowerCase())) {
filteredList.add(row);
}
}
productsFiltered = filteredList;
}
FilterResults filterResults = new FilterResults();
filterResults.values = productsFiltered;
return filterResults;
}
@Override
protected void publishResults(CharSequence charSequence, FilterResults filterResults) {
notifyDataSetChanged();
}
};
return filter;
}
这是我的片段
viewModel.productsList.observe(this, new Observer<List<ProductRealm>>() {
@Override
public void onChanged(@Nullable List<ProductRealm> productRealms) {
productAdapter = new ProductAdapter(viewModel.productsList.getValue(), getContext());
rvProduct.setAdapter(productAdapter);
}
});
查看模型
public MutableLiveData<List<ProductRealm>> productsList = new MutableLiveData<>();
RealmResults<ProductRealm> results = realm.where(ProductRealm.class).findAll();
productsList.setValue(results);
答案 0 :(得分:0)
将对象更改为unmanaged
域对象后,可以使用将RealmObject扩展为常规对象的类。
这可以使用文档中描述的方法copyFromRealm
来实现
https://realm.io/docs/java/latest/api/io/realm/Realm.html#copyFromRealm-E-