有人知道,我如何检查领域对象还没有保存到数据库吗?
我想实现以下示例中的逻辑:
protected boolean prepareToSave(Product product, Realm realm) {
// are we have enought space at storage?
int productLimit = getLimit();
if(
/* product.isStoredInDB() && */
realm.where(Product.class).count() >= productLimit)
Toast.makeText(this, "Not enought space at storage", Toast.LENGTH_LONG).show();
else
// do another stuff
}
编辑:
哪里:
public class Product extends RealmObject {
@PrimaryKey private long id;
@Required private String title;
private String note;
private byte[] image;
private byte[] thumbnail;
private double count = Double.NaN;
private double minimumCount = Double.NaN;
// getters and setters
}
和方法getLimit()仅返回存储的语法限制,而不返回实际的SD卡或内部存储容量.此限制用于提供应用内购买.
解决方法:
您可以使用
Product p = getProduct()
if (!p.isManaged()) {
// Object isn't saved in Realm
}
但是您的代码段似乎表明您想检查设备是否有足够的空间来存储您的更改.确实没有一种方法可以确定这一点,因为这将在很大程度上取决于您要存储的数据类型.