Realm “Cannot create asynchronous query” while using multiple addChangeListeners
I'm getting this type of error:
Fatal Exception: java.lang.IllegalStateException: Cannot create asynchronous query while in a write transaction in /Users/cm/Realm/realm-java/realm/realm-library/src/main/cpp/io_realm_internal_OsResults.cpp line 252
at io.realm.internal.OsResults.nativeStartListening(OsResults.java)
at io.realm.internal.OsResults.addListener(OsResults.java:405)
at io.realm.internal.OsResults.addListener(OsResults.java:412)
at io.realm.RealmResults.addChangeListener(RealmResults.java:173)
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 io.realm.internal.OsSharedRealm.nativeBeginTransaction(OsSharedRealm.java)
at io.realm.internal.OsSharedRealm.beginTransaction(OsSharedRealm.java:269)
at io.realm.BaseRealm.beginTransaction(BaseRealm.java:401)
at io.realm.Realm.beginTransaction(Realm.java:146)
How can I make sure that I'm not in transaction while I am adding addChangeListeners? A simple approach of mRealm.isInTransaction
could help or the problem is in codes structure?
public void update() {
sData = dataQuery.sort(Data.ORDER, Sort.ASCENDING).findAllAsync();
sData.addChangeListener(new RealmChangeListener<RealmResults<Data>>() {
@Override
public void onChange(RealmResults<Data> element) {
sDiffData = mRealm.where(DiffData.class)
.greaterThanOrEqualTo(...)
.lessThan(...)
.equalTo(..)
.findAllAsync();
sDiffData.addChangeListener(new RealmChangeListener<RealmResults<...>>() {
@Override
public void onChange(RealmResults<...> element) {
...
});
}
});
}
I cannot re-produce error myself, but I can see it in crashlytics showing up pretty often.
android realm
add a comment |
I'm getting this type of error:
Fatal Exception: java.lang.IllegalStateException: Cannot create asynchronous query while in a write transaction in /Users/cm/Realm/realm-java/realm/realm-library/src/main/cpp/io_realm_internal_OsResults.cpp line 252
at io.realm.internal.OsResults.nativeStartListening(OsResults.java)
at io.realm.internal.OsResults.addListener(OsResults.java:405)
at io.realm.internal.OsResults.addListener(OsResults.java:412)
at io.realm.RealmResults.addChangeListener(RealmResults.java:173)
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 io.realm.internal.OsSharedRealm.nativeBeginTransaction(OsSharedRealm.java)
at io.realm.internal.OsSharedRealm.beginTransaction(OsSharedRealm.java:269)
at io.realm.BaseRealm.beginTransaction(BaseRealm.java:401)
at io.realm.Realm.beginTransaction(Realm.java:146)
How can I make sure that I'm not in transaction while I am adding addChangeListeners? A simple approach of mRealm.isInTransaction
could help or the problem is in codes structure?
public void update() {
sData = dataQuery.sort(Data.ORDER, Sort.ASCENDING).findAllAsync();
sData.addChangeListener(new RealmChangeListener<RealmResults<Data>>() {
@Override
public void onChange(RealmResults<Data> element) {
sDiffData = mRealm.where(DiffData.class)
.greaterThanOrEqualTo(...)
.lessThan(...)
.equalTo(..)
.findAllAsync();
sDiffData.addChangeListener(new RealmChangeListener<RealmResults<...>>() {
@Override
public void onChange(RealmResults<...> element) {
...
});
}
});
}
I cannot re-produce error myself, but I can see it in crashlytics showing up pretty often.
android realm
add a comment |
I'm getting this type of error:
Fatal Exception: java.lang.IllegalStateException: Cannot create asynchronous query while in a write transaction in /Users/cm/Realm/realm-java/realm/realm-library/src/main/cpp/io_realm_internal_OsResults.cpp line 252
at io.realm.internal.OsResults.nativeStartListening(OsResults.java)
at io.realm.internal.OsResults.addListener(OsResults.java:405)
at io.realm.internal.OsResults.addListener(OsResults.java:412)
at io.realm.RealmResults.addChangeListener(RealmResults.java:173)
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 io.realm.internal.OsSharedRealm.nativeBeginTransaction(OsSharedRealm.java)
at io.realm.internal.OsSharedRealm.beginTransaction(OsSharedRealm.java:269)
at io.realm.BaseRealm.beginTransaction(BaseRealm.java:401)
at io.realm.Realm.beginTransaction(Realm.java:146)
How can I make sure that I'm not in transaction while I am adding addChangeListeners? A simple approach of mRealm.isInTransaction
could help or the problem is in codes structure?
public void update() {
sData = dataQuery.sort(Data.ORDER, Sort.ASCENDING).findAllAsync();
sData.addChangeListener(new RealmChangeListener<RealmResults<Data>>() {
@Override
public void onChange(RealmResults<Data> element) {
sDiffData = mRealm.where(DiffData.class)
.greaterThanOrEqualTo(...)
.lessThan(...)
.equalTo(..)
.findAllAsync();
sDiffData.addChangeListener(new RealmChangeListener<RealmResults<...>>() {
@Override
public void onChange(RealmResults<...> element) {
...
});
}
});
}
I cannot re-produce error myself, but I can see it in crashlytics showing up pretty often.
android realm
I'm getting this type of error:
Fatal Exception: java.lang.IllegalStateException: Cannot create asynchronous query while in a write transaction in /Users/cm/Realm/realm-java/realm/realm-library/src/main/cpp/io_realm_internal_OsResults.cpp line 252
at io.realm.internal.OsResults.nativeStartListening(OsResults.java)
at io.realm.internal.OsResults.addListener(OsResults.java:405)
at io.realm.internal.OsResults.addListener(OsResults.java:412)
at io.realm.RealmResults.addChangeListener(RealmResults.java:173)
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 io.realm.internal.OsSharedRealm.nativeBeginTransaction(OsSharedRealm.java)
at io.realm.internal.OsSharedRealm.beginTransaction(OsSharedRealm.java:269)
at io.realm.BaseRealm.beginTransaction(BaseRealm.java:401)
at io.realm.Realm.beginTransaction(Realm.java:146)
How can I make sure that I'm not in transaction while I am adding addChangeListeners? A simple approach of mRealm.isInTransaction
could help or the problem is in codes structure?
public void update() {
sData = dataQuery.sort(Data.ORDER, Sort.ASCENDING).findAllAsync();
sData.addChangeListener(new RealmChangeListener<RealmResults<Data>>() {
@Override
public void onChange(RealmResults<Data> element) {
sDiffData = mRealm.where(DiffData.class)
.greaterThanOrEqualTo(...)
.lessThan(...)
.equalTo(..)
.findAllAsync();
sDiffData.addChangeListener(new RealmChangeListener<RealmResults<...>>() {
@Override
public void onChange(RealmResults<...> element) {
...
});
}
});
}
I cannot re-produce error myself, but I can see it in crashlytics showing up pretty often.
android realm
android realm
asked Nov 13 '18 at 9:06
PeteWonderPeteWonder
9811
9811
add a comment |
add a comment |
1 Answer
1
active
oldest
votes
This happens if you have a UI-thread write transaction and also have a RealmChangeListener
in which you also execute an async query (or addChangeListener
).
Solution 1: don't use UI thread write transactions because they can potentially block your UI and your app will be slow and bad
Solution 2: if you're in a write transaction inside the RealmChangeListener, then execute the logic in a handler.post {
block
private void fetchDiff() {
sDiffData = mRealm.where(DiffData.class)
.greaterThanOrEqualTo(...)
.lessThan(...)
.equalTo(..)
.findAllAsync();
sDiffData.addChangeListener(new RealmChangeListener<RealmResults<...>>() {
@Override
public void onChange(RealmResults<...> element) {
...
});
}
public void update() {
sData = dataQuery.sort(Data.ORDER, Sort.ASCENDING).findAllAsync();
sData.addChangeListener(new RealmChangeListener<RealmResults<Data>>() {
@Override
public void onChange(RealmResults<Data> element) {
Realm realm = element.getRealm();
if(realm.isInTransaction()) {
handler.post(() -> {
fetchDiff();
});
} else {
fetchDiff();
}
}
});
}
add a comment |
Your Answer
StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");
StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});
function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53277341%2frealm-cannot-create-asynchronous-query-while-using-multiple-addchangelisteners%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
This happens if you have a UI-thread write transaction and also have a RealmChangeListener
in which you also execute an async query (or addChangeListener
).
Solution 1: don't use UI thread write transactions because they can potentially block your UI and your app will be slow and bad
Solution 2: if you're in a write transaction inside the RealmChangeListener, then execute the logic in a handler.post {
block
private void fetchDiff() {
sDiffData = mRealm.where(DiffData.class)
.greaterThanOrEqualTo(...)
.lessThan(...)
.equalTo(..)
.findAllAsync();
sDiffData.addChangeListener(new RealmChangeListener<RealmResults<...>>() {
@Override
public void onChange(RealmResults<...> element) {
...
});
}
public void update() {
sData = dataQuery.sort(Data.ORDER, Sort.ASCENDING).findAllAsync();
sData.addChangeListener(new RealmChangeListener<RealmResults<Data>>() {
@Override
public void onChange(RealmResults<Data> element) {
Realm realm = element.getRealm();
if(realm.isInTransaction()) {
handler.post(() -> {
fetchDiff();
});
} else {
fetchDiff();
}
}
});
}
add a comment |
This happens if you have a UI-thread write transaction and also have a RealmChangeListener
in which you also execute an async query (or addChangeListener
).
Solution 1: don't use UI thread write transactions because they can potentially block your UI and your app will be slow and bad
Solution 2: if you're in a write transaction inside the RealmChangeListener, then execute the logic in a handler.post {
block
private void fetchDiff() {
sDiffData = mRealm.where(DiffData.class)
.greaterThanOrEqualTo(...)
.lessThan(...)
.equalTo(..)
.findAllAsync();
sDiffData.addChangeListener(new RealmChangeListener<RealmResults<...>>() {
@Override
public void onChange(RealmResults<...> element) {
...
});
}
public void update() {
sData = dataQuery.sort(Data.ORDER, Sort.ASCENDING).findAllAsync();
sData.addChangeListener(new RealmChangeListener<RealmResults<Data>>() {
@Override
public void onChange(RealmResults<Data> element) {
Realm realm = element.getRealm();
if(realm.isInTransaction()) {
handler.post(() -> {
fetchDiff();
});
} else {
fetchDiff();
}
}
});
}
add a comment |
This happens if you have a UI-thread write transaction and also have a RealmChangeListener
in which you also execute an async query (or addChangeListener
).
Solution 1: don't use UI thread write transactions because they can potentially block your UI and your app will be slow and bad
Solution 2: if you're in a write transaction inside the RealmChangeListener, then execute the logic in a handler.post {
block
private void fetchDiff() {
sDiffData = mRealm.where(DiffData.class)
.greaterThanOrEqualTo(...)
.lessThan(...)
.equalTo(..)
.findAllAsync();
sDiffData.addChangeListener(new RealmChangeListener<RealmResults<...>>() {
@Override
public void onChange(RealmResults<...> element) {
...
});
}
public void update() {
sData = dataQuery.sort(Data.ORDER, Sort.ASCENDING).findAllAsync();
sData.addChangeListener(new RealmChangeListener<RealmResults<Data>>() {
@Override
public void onChange(RealmResults<Data> element) {
Realm realm = element.getRealm();
if(realm.isInTransaction()) {
handler.post(() -> {
fetchDiff();
});
} else {
fetchDiff();
}
}
});
}
This happens if you have a UI-thread write transaction and also have a RealmChangeListener
in which you also execute an async query (or addChangeListener
).
Solution 1: don't use UI thread write transactions because they can potentially block your UI and your app will be slow and bad
Solution 2: if you're in a write transaction inside the RealmChangeListener, then execute the logic in a handler.post {
block
private void fetchDiff() {
sDiffData = mRealm.where(DiffData.class)
.greaterThanOrEqualTo(...)
.lessThan(...)
.equalTo(..)
.findAllAsync();
sDiffData.addChangeListener(new RealmChangeListener<RealmResults<...>>() {
@Override
public void onChange(RealmResults<...> element) {
...
});
}
public void update() {
sData = dataQuery.sort(Data.ORDER, Sort.ASCENDING).findAllAsync();
sData.addChangeListener(new RealmChangeListener<RealmResults<Data>>() {
@Override
public void onChange(RealmResults<Data> element) {
Realm realm = element.getRealm();
if(realm.isInTransaction()) {
handler.post(() -> {
fetchDiff();
});
} else {
fetchDiff();
}
}
});
}
answered Nov 13 '18 at 9:46
EpicPandaForceEpicPandaForce
48.3k14128253
48.3k14128253
add a comment |
add a comment |
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53277341%2frealm-cannot-create-asynchronous-query-while-using-multiple-addchangelisteners%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown