I am having trouble using a query in realm.io. My code:
public static void delete(Context context, Workday workday) {
Realm realm = getRealm(context);
realm.beginTransaction();
RealmResults<Workday> workdays = realm.where(Workday.class)
.equalTo("date", workday.getDate())
.equalTo("hours", workday.getHours())
.equalTo("minutes", workday.getMinutes())
.findAll();
workdays.remove(0);
realm.commitTransaction();
}
Debug:
Why is the data visible in the JSON line but not in the fields itself? What am I doing wrong?
Thanks in advance!
UPDATE:
This is my delete function and it does find the workday1 object:
public static void delete(Context context, Workday workday) {
Realm realm = getRealm(context);
realm.beginTransaction();
Workday workday1 = realm.where(Workday.class)
.equalTo("date", workday.getDate())
.equalTo("hours", workday.getHours())
.equalTo("minutes", workday.getMinutes())
.findFirst();
workday1.removeFromRealm();
realm.commitTransaction();
}
When it executes the removeFromRealm method it crashes:
java.lang.IllegalStateException: Illegal State: Row/Object is no longer valid to operate on. Was it deleted?
How can I fix this?
You're doing nothing wrong! :)
Realm proxies your objects so that there won't be copies of your data all over the place. The getters and setters are overridden by the proxy classes and access your data directly in Realm. Of course this makes it harder to inspect the objects during debug, as you could notice, but that's why we've also overridden toString() to show something meaningful.
Related
I am applying filters on realm using RealmResults<>.
I begin to do like this -
RealmResults<data> filteredRealmResults;
List<data> tranfilteredlist;
private OrderedRealmCollectionChangeListener<RealmResults<data>> filteredTransChangeListener =
new OrderedRealmCollectionChangeListener<RealmResults<data>>() {
#Override
public void onChange(RealmResults<data> results, OrderedCollectionChangeSet changeSet) {
Log.d("realm", "filteredRealmResults.size():" + filteredRealmResults.size());
tranfilteredlist = results;
initFilterAdapter();
}
};
Now I want to delete the filteredRealmResults. I did like this -
void deleteFilteredRealmResults() {
realm.executeTransaction(new Realm.Transaction() {
#Override
public void execute(Realm realm) {
// Delete all matches
filteredRealmResults.deleteAllFromRealm();
}
});
}
After doing this my data in the realm got deleted. So I just try to delete the tranfilteredlist but it throws an exception that it does not support .clear();
I want to clear if from the memory whatever is holder the query data. Correct me if I am wrong or doesn't understand or just worrying too much.
I read This class holds all the matches of a RealmQuery for a given Realm. The objects are not copied from the Realm to the RealmResults list, but are just referenced from the RealmResult instead. This saves memory and increases speed.
I want to clear if from the memory whatever is holder the query data.
Correct me if I am wrong or doesn't understand or just worrying too
much.
Once you invoke filteredRealmResults.deleteAllFromRealm, it will clear the internal resultant elements object(which holds the elements) and as you know, resultant objects are reference so data will be deleted from realm database too. Hence, there is no need to call clear on the RealmResults object.
You can verify this by calling filteredRealmResults.size() after deletion, it will return 0.
I just try to delete the tranfilteredlist but it throws an exception
that it does not support .clear();
It is the expected behaviour as clear has been deprecated so don't use it.
Why deprecated?
deleteAllFromRealm automatically clears the list so no need to call it again explicitly.
Calling clear on RealmResults object will result in deletion of data from database, can cause unexpected behaviour if the user is not aware so API is being modified to avoid unexpected behaviours.
I used Realm in conjunction with RxJava it this way:
public Flowable<List<EventEntity>> getAll() {
try (final Realm realm = Realm.getInstance(mRealmConfiguration)) {
RealmQuery<RealmEvent> query = realm.where(RealmEvent.class);
Flowable<RealmResults<RealmEvent>> result;
if (realm.isAutoRefresh()) {
result = query
.findAllAsync()
.asFlowable()
.filter(RealmResults::isLoaded);
} else {
result = Flowable.just(query.findAll());
}
return result
.unsubscribeOn(AndroidSchedulers.mainThread());
}
}
I use this chain on multiple places in app. For example:
return Observable.merge(
mEventRepository.getAll()
.toObservable(),
subjectNotificationChange
.flatMapMaybe(notification ->
mEventRepository.getAll()
.firstElement()
)
)
Problem is that I obtain exception: java.lang.IllegalStateException: This Realm instance has already been closed, making it unusable.
I looked at implementation method from of RealmObservableFactory and each call of subscribe method should create new instance of Realm. Entire situation looks as problem with references counting.
Do you know where is problem?
Java's try-with-resource closes the resource as soon as you leave the code block, but RxJava being lazy and all, only begins working when you actually subscribe, which happens after your code exits the getAll() function.
Edit: since you build a special Realm instance each time, passing configuration to it, the instance is not shared and therefore definitively closed each time.
Instead, initialize your Realm earlier using Realm.setDefaultConfiguration(config). Then, use Realm.getDefaultInstance() in your function so you access the default shared instance instead of creating a new one each time.
Edit2: the easiest solution is to keep a reference to the Realm instance:
class MyRepository {
private final Realm realm;
public MyRepository(Realm realm) {
this.realm = realm;
}
public Flowable<List<EventEntity>> getAll() {
RealmQuery<RealmEvent> query = realm.where(RealmEvent.class);
// ...
}
}
Realm realm = Realm.getDefaultInstance();
MyRepository repository = MyRepository(realm);
repository.getAll()
// ...
I find solution. It is bug in official example. When you call mentioned chain than must exist other open Realm instance for same thread. In other cases RealmResult is invalidated. Can be used solution mentioned by ESala.
I started trying Realm for Android, so I created two classes:
class ProductSelection extends RealmObject {
private String selectedProductName;
private String selectedProductID;
…
}
class ProductProfile extends RealmObject {
private String profileTitle;
private RealmList< ProductSelection > productSelection;
…
}
I then created a couple of ProductSelection Objects and ProductProfile Objects.
realm.beginTransaction();
ProductSelection prodSelection = realm.createObject(ProductSelection.class);
prodSelection.setSelectedProductName(prodTv.getText().toString());
prodSelection.setSelectedProductID(prodIdTv.getText().toString());
…
realm.commitTransaction();
…
realm.beginTransaction();
ProductProfile profile = realm.createObject(ProductProfile.class);
profile.setProfileTitle(“Some Title”);
RealmResults< ProductSelection > results =
realm.allObjects(ProductSelection.class);
RealmList< ProductSelection > selectionList = new RealmList<>();
for (ProductSelection selection : results) {
selectionList.add(selection);
}
profile.setProductSelection(selectionList);
realm.commitTransaction();
Now I have the following questions:
When I call
realm.allObjects(ProductSelection.class).clear();
it seems not only my ProductSelection Objects are cleared but also the List of ProductSelection Objects that are part of ProductProfile Class, as they now don’t hold any Objects anymore. Is it possible to prevent this, that is removing all ProductSelection Objects but still keeping the Objects that are part ProductProfile’s list?
Is the other way around also possible, that is removing ProductProfile Objects without removing ProductSelection Objects, that are not part of the productSelection List?
No, there isn't. Realm is a typed database, so you can think of a RealmObject class a SQLite table. Clearing that, will remove all data, even if it is referenced from other classes.
The solution would be to make a query for the ProductSelection objects and delete those explicitly:
realm.executeTransaction(new Realm.Transaction() {
#Override
public void execute(Realm realm) {
realm.where(ProductSelection.class).equalTo("someField", "someValue")
.findAll()
.deleteAllFromRealm();
// deleteAllFromRealm() since 0.89.0, in previous versions it's `clear()`
}
});
I actually can't think of a simple way of supporting this explicit use-case, unless you use bi-directional mapping (if your ProductProfile class contains a ProductSelection, then that ProductSelection also contains its ProductProfile).
Because then you can do this
realm.where(ProductSelection.class).isEmpty("productProfileList").findAll().deleteAllFromRealm();
// clear before 0.89.0, deleteAllFromRealm() after 0.89.0+
You have to manage this mapping yourself, though.
I am using Realm with RxAndroid. i am having this strange issue where realm is not picking up the latest modification done on DB.
There are 2 methods that i am using.
Observable<Integer> save(Bitmap bitmap).
Observable<Integer> getImageList(Context applicationContext).
Like this
Activity 1
getImageList(applicationContext)
button click -> Activity 2
save(bitmap)
finish()
getImageList(applicationContext)
This method "save" basically adds a newly created model into RealmList.
private Observable<Integer> save(Bitmap bitmap) {
return Observable.create((Observable.OnSubscribe<Integer>) subscriber -> {
--------------------------------------
-----Various file creation stuff------
--------------------------------------
UserImagesModel model = realm
.where(UserImagesModel.class)
.findFirst();
//ImageModel class extends RealmObject
ImageModel imageModel = new ImageModel();
realm.beginTransaction();
//realm object must be Edited inside transaction
model.getResponse().add(0, imageModel);
realm.commitTransaction();
realm.close();
subscriber.onNext(1);
subscriber.onCompleted();
}
}
Ans this method fetches saved list.
public Observable<Integer> getImageList(Context applicationContext) {
return Observable.create((Observable.OnSubscribe<Integer>) subscriber -> {
AppUtils.logD("User image observable instance " + this);
UserImagesModel model;
Realm realm = Realm.getInstance(applicationContext);
model = realm.where(UserImagesModel.class).findFirst();
^
This model doesn't replicate data added in save call
------------------------------------------------
----Various validation and service calls.-------
------------------------------------------------
subscriber.onCompleted();
realm.close();
});
}
}
As i mentioned in code, UserImageModel that i get from Realm doesn't replicate changes i made in save method.
the problem occurs when i call getImageList method second time. also when i print this.toString inside Observable.create it prints same object that was returned first time.
So i believe this issue seems to be with the way i am using RxAndroid. can anyone tell me what i am missing? and how can i resolve it?
UPDATE :
After few tests i realized that this.toString inside Observable.create is actually points to parent object as i have used lamda expression so that is not seems to be the issue and now i am back to square one ;(
Turns out, this is expected behavior of Realm. as i was subscribing those observables on IO threads which doesn't have Looper.
Op here has similar issue. answer explains the case.
This is my delete function and it does find the workday1 object:
public static void delete(Context context, Workday workday) {
Realm realm = getRealm(context);
realm.beginTransaction();
Workday workday1 = realm.where(Workday.class)
.equalTo("date", workday.getDate())
.equalTo("hours", workday.getHours())
.equalTo("minutes", workday.getMinutes())
.findFirst();
workday1.removeFromRealm();
realm.commitTransaction();
}
When it executes the removeFromRealm method it crashes:
java.lang.IllegalStateException: Illegal State: Row/Object is no longer valid to operate on. Was it deleted?
How can I fix this? Any help would be greatly appreciated.
UPDATE (I can print the content returned by the following method):
Workday workday1 = realm.where(Workday.class)
.equalTo("date", workday.getDate())
.equalTo("hours", workday.getHours())
.equalTo("minutes", workday.getMinutes())
.findFirst();
System.out.println("--------------------------------");
System.out.println(workday1.getHours());
You are trying to remove an object you have not committed to the Realm yet.
In this particular case, if for some reason you don't want to commit the object anymore, you can simply cancel the transaction.
In my case, the problem was that the same Object I was deleting was in an Adapter. After I made the Adapter extend RealmBaseAdapter the problem stopped.