问题
I'm develop an Android app with Realm integrated and I have a problem when I try to search with a query. I try to search a product by its "id" into product list passed by parameter. This method worked perfect but suddenly I got the next exception:
Process: com.singularfactory.ecommerceapp.ecommerceapp, PID: 9372
io.realm.exceptions.RealmException: This method is only available in managed mode
at io.realm.RealmList.where(RealmList.java:260)
at com.singularfactory.ecommerceapp.utility.UtilityDB.findProductIntoChildrenByEntityId(UtilityDB.java:176)
at com.singularfactory.ecommerceapp.fragment.ProductDetailFragment.setProductRealmToView(ProductDetailFragment.java:378)
at com.singularfactory.ecommerceapp.fragment.ProductDetailFragment.initializeEnvironment(ProductDetailFragment.java:119)
at com.singularfactory.ecommerceapp.fragment.ProductDetailFragment.onActivityCreated(ProductDetailFragment.java:96)
at android.support.v4.app.Fragment.performActivityCreated(Fragment.java:1797)
at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:979)
at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1138)
at android.support.v4.app.BackStackRecord.run(BackStackRecord.java:740)
at android.support.v4.app.FragmentManagerImpl.execPendingActions(FragmentManager.java:1501)
at android.support.v4.app.FragmentManagerImpl.executePendingTransactions(FragmentManager.java:490)
at com.singularfactory.ecommerceapp.fragment.ListsFragment.openProductDetailFragment(ListsFragment.java:256)
at com.singularfactory.ecommerceapp.fragment.ListsFragment$5.onClick(ListsFragment.java:193)
at com.singularfactory.ecommerceapp.adapter.ListsContentAdapter.onClick(ListsContentAdapter.java:78)
at android.view.View.performClick(View.java:4438)
at android.view.View$PerformClick.run(View.java:18422)
at android.os.Handler.handleCallback(Handler.java:733)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:136)
at android.app.ActivityThread.main(ActivityThread.java:5001)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:515)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:785)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:601)
at dalvik.system.NativeStart.main(Native Method)
My code:
/**
* Find a product into Children by entityId
* @param products
* @param entityId
* @return
*/
public static ProductRealm findProductIntoChildrenByEntityId(RealmList<ProductRealm> products, String entityId) {
ProductRealm product = null;
if(products != null) {
**product = products.where()** //The problem is here
.equalTo("entityId", entityId)
.findFirst();
}
return product;
}
My model:
@RealmClass
public class ProductRealm extends RealmObject implements Serializable {
private String catId; //Only one category per product. In generic case many category per product
private String entityId;
private String parentId;
private String typeId; //"simple" | "configurable"
private String sku;
private String canaryProduct;
private String premium;
private String brand;
private String dinitosQty;
private String name;
private String ean;
private String description;
private String shortDescription;
private String weight;
private String isSaleable;
private String imageUrl;
private String unit;
private String unitFresh;
private String childrenSelected;
private String manageStock;
private float price;
private float minWeight;
private float quantityWeight;
private int quantity;
private int stock;
private int typeOfAttribute; //0: Per units | 1: Per weight | 2: Both
private int typeOfChildrenSelected; //0: Per units | 2: Per weight | -1: Undefined
private boolean isIntoShoppingCart; //Flag to indicate if the product is into the Shopping Cart
private RealmList<AttributeRealm> attributeItems;
private RealmList<ProductRealm> children;
...
/* Getters & Setters */
I hope that the question is well detailed.
Thanks in advance :)
回答1:
This is happening because the RealmList you are providing to the findProductIntoChildrenByEntityId method is a standalone one, meaning, not managed by Realm. A standalone RealmList only lives in memory (and not in Realm), which means that it's not possible to query on it.
回答2:
Your public static ProductRealm findProductIntoChildrenByEntityId(RealmList<ProductRealm> products, String entityId) { realmList you provide here must be bound to an existing, open realm instance. The query would run in Realm, and it would run if the objects within the RealmList are copied to Realm.
来源:https://stackoverflow.com/questions/31429239/this-method-is-only-available-in-managed-mode