question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

java.lang.IllegalStateException: Object is no longer managed by Realm. Has it been deleted?

See original GitHub issue

Goal

Realm partial query Async

Expected Results

Execute normal with RealmChangeListener

Actual Results

Process: net.ddns.zimportant.lovingpeople, PID: 23689 java.lang.IllegalStateException: Object is no longer managed by Realm. Has it been deleted? at io.realm.internal.InvalidRow.getStubException(InvalidRow.java:188) at io.realm.internal.InvalidRow.getString(InvalidRow.java:89) at io.realm.net_ddns_zimportant_lovingpeople_service_common_model_UserChatRealmProxy.realmGet$id(net_ddns_zimportant_lovingpeople_service_common_model_UserChatRealmProxy.java:113) at net.ddns.zimportant.lovingpeople.service.common.model.UserChat.getId(UserChat.java:46) at net.ddns.zimportant.lovingpeople.fragment.MessageFragment.lambda$setUpUser$0$MessageFragment(MessageFragment.java:89) at net.ddns.zimportant.lovingpeople.fragment.MessageFragment$$Lambda$0.onChange(Unknown Source:0) at io.realm.ProxyState$RealmChangeListenerWrapper.onChange(ProxyState.java:50) at io.realm.internal.OsObject$ObjectObserverPair.onChange(OsObject.java:73) at io.realm.ProxyState$QueryCallback.onCalled(ProxyState.java:70) at io.realm.ProxyState$QueryCallback.onCalled(ProxyState.java:65) at io.realm.internal.ObserverPairList.foreach(ObserverPairList.java:109) at io.realm.ProxyState.notifyQueryFinished(ProxyState.java:132) at io.realm.ProxyState.onQueryFinished(ProxyState.java:199) at io.realm.internal.PendingRow.notifyFrontEnd(PendingRow.java:247) at io.realm.internal.PendingRow.access$000(PendingRow.java:20) at io.realm.internal.PendingRow$1.onChange(PendingRow.java:48) at io.realm.internal.PendingRow$1.onChange(PendingRow.java:45) at io.realm.internal.ObservableCollection$RealmChangeListenerWrapper.onChange(ObservableCollection.java:39) at io.realm.internal.ObservableCollection$CollectionObserverPair.onChange(ObservableCollection.java:20) at io.realm.internal.ObservableCollection$Callback.onCalled(ObservableCollection.java:64) at io.realm.internal.ObservableCollection$Callback.onCalled(ObservableCollection.java:54) at io.realm.internal.ObserverPairList.foreach(ObserverPairList.java:109) at io.realm.internal.OsResults.notifyChangeListeners(OsResults.java:451) at android.os.MessageQueue.nativePollOnce(Native Method) at android.os.MessageQueue.next(MessageQueue.java:379) at android.os.Looper.loop(Looper.java:144) at android.app.ActivityThread.main(ActivityThread.java:7425) at java.lang.reflect.Method.invoke(Native Method) at com.android.internal.os.Zygote$MethodAndArgsCaller.run(Zygote.java:245) at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:921)

Steps & Code to Reproduce

When I started the fragment immediately after login, it throws this error. But when I started after few seconds, it loaded normal (?)

Code Sample

	private void setUpUser() {
		realm
				.where(UserChat.class)
				.findAllAsync();

		realm
				.where(UserChat.class)
				.equalTo("id", SyncUser.current().getIdentity())
				.findFirstAsync()
				.addChangeListener((UserChat userChat) -> {
					currentUser = userChat;
					if (!(userChat.getId()).equals(SyncUser.current().getIdentity())) { // this is the line produce error 
						((MainActivity) getContext()).logOutRealm();
						return;
					}
					setUpInformation();
					setUpSwitchButton();
					setUpRecyclerView();
					setUpFab();
				});
	}

Version of Realm and tooling

Realm version(s): Platform: 3.6.4 Android: 5.2.0

Realm sync feature enabled: yes

Android Studio version: 3.0.1

Which Android version and device: Android 8.0.0

Issue Analytics

  • State:closed
  • Created 5 years ago
  • Comments:8 (4 by maintainers)

github_iconTop GitHub Comments

1reaction
Zhuindencommented, Apr 29, 2020

You need to store the RealmResults as a field ref otherwise GC can kill your queries.

1reaction
zimportantcommented, Jun 9, 2018

Thank you for your supporting, btw I have learned a lot because of this bug 😄

Read more comments on GitHub >

github_iconTop Results From Across the Web

java.lang.IllegalStateException: Object is no longer managed ...
IllegalStateException : Object is no longer managed by Realm. Has it been deleted? This is my code @Override public void onDeleteClick(int ...
Read more >
io.realm.RealmObject.isValid java code examples - Tabnine
Checks if the RealmObject is still valid to use i.e. the RealmObject hasn't been deleted nor has the io.realm.Realm been closed. It will...
Read more >
Realm (Realm 4.1.0) - MongoDB
The Realm class is the storage and transactional manager of your object persistent store. It is in charge of creating instances of your...
Read more >
The things I've learned using Realm | by Gabor Varadi - Medium
In this article, I try to sum up all the things I've learned from using Realm, and how it all relates with the...
Read more >
Realm – Object delete – Object is no longer valid to operate on
java.lang.IllegalStateException: Illegal State: Object is no longer valid to operate on. Was it deleted by another thread? Assumptions
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found