How to deal with RxJava and Retrofit with many access to Realm ORM database and avoid Realm access from incorrect thread Announcing the arrival of Valued Associate #679: Cesar Manara Planned maintenance scheduled April 23, 2019 at 23:30 UTC (7:30pm US/Eastern) Data science time! April 2019 and salary with experience The Ask Question Wizard is Live!How to combine Retrofit 2 with Realm and RxJavausing Rxjava with retrofit and realmStoring Data using Realm with Retrofit and RxJavaRxJava Multithreading with Realm - Realm access from incorrect threadRealm access from incorrect threadMoshi's Custom Adapter with RxAndroid & Retrofit & KotlinAndroid, Retrofit , & RxJava: “Realm access from incorrect thread”Rxjava + Realm access from incorrect threadRxjava 2 - fetch data with realm and retrofitRealm access from incorrect thread in rx and dagger
Does GDPR cover the collection of data by websites that crawl the web and resell user data
Is "ein Herz wie das meine" an antiquated or colloquial use of the possesive pronoun?
Why isn't everyone flabbergasted about Bran's "gift"?
Is Bran literally the world's memory?
Reflections in a Square
Weaponising the Grasp-at-a-Distance spell
Compiling and throwing simple dynamic exceptions at runtime for JVM
Can gravitational waves pass through a black hole?
Trying to enter the Fox's den
What is the evidence that custom checks in Northern Ireland are going to result in violence?
Are there any AGPL-style licences that require source code modifications to be public?
Why did Israel vote against lifting the American embargo on Cuba?
A German immigrant ancestor has a "Registration Affidavit of Alien Enemy" on file. What does that mean exactly?
Putting Ant-Man on house arrest
2 sample t test for sample sizes - 30,000 and 150,000
How to mute a string and play another at the same time
Why are two-digit numbers in Jonathan Swift's "Gulliver's Travels" (1726) written in "German style"?
Pointing to problems without suggesting solutions
Can this water damage be explained by lack of gutters and grading issues?
Why these surprising proportionalities of integrals involving odd zeta values?
Why is one lightbulb in a string illuminated?
A journey... into the MIND
Marquee sign letters
How do I deal with an erroneously large refund?
How to deal with RxJava and Retrofit with many access to Realm ORM database and avoid Realm access from incorrect thread
Announcing the arrival of Valued Associate #679: Cesar Manara
Planned maintenance scheduled April 23, 2019 at 23:30 UTC (7:30pm US/Eastern)
Data science time! April 2019 and salary with experience
The Ask Question Wizard is Live!How to combine Retrofit 2 with Realm and RxJavausing Rxjava with retrofit and realmStoring Data using Realm with Retrofit and RxJavaRxJava Multithreading with Realm - Realm access from incorrect threadRealm access from incorrect threadMoshi's Custom Adapter with RxAndroid & Retrofit & KotlinAndroid, Retrofit , & RxJava: “Realm access from incorrect thread”Rxjava + Realm access from incorrect threadRxjava 2 - fetch data with realm and retrofitRealm access from incorrect thread in rx and dagger
.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty height:90px;width:728px;box-sizing:border-box;
This question is often asked and resolved in many ways, but if I come back to the same question, it's because I'm a bit confused. This is my first time to deal with Realm ORM.
This repository class state for the implementation on db storage. Its take instance of Realm and ApiService
class LoginRepositoryImpl(var realm: Realm, var apiService: ApiService)
override fun doLogin(email: String, password: String)
apiService.loginCheck(email, password)
.flatMap t: EntityToken ->
// check if user already exist and delete it
val checkIfUserExist = getUserIfExist("email", email)
if (checkIfUserExist != null)
realm.executeTransactionAsync
checkIfUserExist.deleteFromRealm()
// insert user into DB
realm.executeTransactionAsync bgRealm ->
val user = bgRealm.createObject(UserModel::class.java, UserModel.cachedNextId)
user.email = email
user.logged = true
// i want to make another request to server
return@flatMap apiService.pingServer(t.refresh_token)
.subscribeOn(Schedulers.io())
.observeOn(AndroidSchedulers.mainThread())
.subscribe( uR ->
// make update of user table
realm.executeTransactionAsync
getUserIfExist("email", email)?.fullname = uR.fullname
//function to check if user instance already exist
private fun getUserIfExist(field: String, email: String): UserModel?
return realm.where(UserModel::class.java)
.equalTo(field, email)
.findFirstAsync()
Ps.help me to resolve Realm access from incorrect thread
android realm retrofit2 rx-java2
add a comment |
This question is often asked and resolved in many ways, but if I come back to the same question, it's because I'm a bit confused. This is my first time to deal with Realm ORM.
This repository class state for the implementation on db storage. Its take instance of Realm and ApiService
class LoginRepositoryImpl(var realm: Realm, var apiService: ApiService)
override fun doLogin(email: String, password: String)
apiService.loginCheck(email, password)
.flatMap t: EntityToken ->
// check if user already exist and delete it
val checkIfUserExist = getUserIfExist("email", email)
if (checkIfUserExist != null)
realm.executeTransactionAsync
checkIfUserExist.deleteFromRealm()
// insert user into DB
realm.executeTransactionAsync bgRealm ->
val user = bgRealm.createObject(UserModel::class.java, UserModel.cachedNextId)
user.email = email
user.logged = true
// i want to make another request to server
return@flatMap apiService.pingServer(t.refresh_token)
.subscribeOn(Schedulers.io())
.observeOn(AndroidSchedulers.mainThread())
.subscribe( uR ->
// make update of user table
realm.executeTransactionAsync
getUserIfExist("email", email)?.fullname = uR.fullname
//function to check if user instance already exist
private fun getUserIfExist(field: String, email: String): UserModel?
return realm.where(UserModel::class.java)
.equalTo(field, email)
.findFirstAsync()
Ps.help me to resolve Realm access from incorrect thread
android realm retrofit2 rx-java2
add a comment |
This question is often asked and resolved in many ways, but if I come back to the same question, it's because I'm a bit confused. This is my first time to deal with Realm ORM.
This repository class state for the implementation on db storage. Its take instance of Realm and ApiService
class LoginRepositoryImpl(var realm: Realm, var apiService: ApiService)
override fun doLogin(email: String, password: String)
apiService.loginCheck(email, password)
.flatMap t: EntityToken ->
// check if user already exist and delete it
val checkIfUserExist = getUserIfExist("email", email)
if (checkIfUserExist != null)
realm.executeTransactionAsync
checkIfUserExist.deleteFromRealm()
// insert user into DB
realm.executeTransactionAsync bgRealm ->
val user = bgRealm.createObject(UserModel::class.java, UserModel.cachedNextId)
user.email = email
user.logged = true
// i want to make another request to server
return@flatMap apiService.pingServer(t.refresh_token)
.subscribeOn(Schedulers.io())
.observeOn(AndroidSchedulers.mainThread())
.subscribe( uR ->
// make update of user table
realm.executeTransactionAsync
getUserIfExist("email", email)?.fullname = uR.fullname
//function to check if user instance already exist
private fun getUserIfExist(field: String, email: String): UserModel?
return realm.where(UserModel::class.java)
.equalTo(field, email)
.findFirstAsync()
Ps.help me to resolve Realm access from incorrect thread
android realm retrofit2 rx-java2
This question is often asked and resolved in many ways, but if I come back to the same question, it's because I'm a bit confused. This is my first time to deal with Realm ORM.
This repository class state for the implementation on db storage. Its take instance of Realm and ApiService
class LoginRepositoryImpl(var realm: Realm, var apiService: ApiService)
override fun doLogin(email: String, password: String)
apiService.loginCheck(email, password)
.flatMap t: EntityToken ->
// check if user already exist and delete it
val checkIfUserExist = getUserIfExist("email", email)
if (checkIfUserExist != null)
realm.executeTransactionAsync
checkIfUserExist.deleteFromRealm()
// insert user into DB
realm.executeTransactionAsync bgRealm ->
val user = bgRealm.createObject(UserModel::class.java, UserModel.cachedNextId)
user.email = email
user.logged = true
// i want to make another request to server
return@flatMap apiService.pingServer(t.refresh_token)
.subscribeOn(Schedulers.io())
.observeOn(AndroidSchedulers.mainThread())
.subscribe( uR ->
// make update of user table
realm.executeTransactionAsync
getUserIfExist("email", email)?.fullname = uR.fullname
//function to check if user instance already exist
private fun getUserIfExist(field: String, email: String): UserModel?
return realm.where(UserModel::class.java)
.equalTo(field, email)
.findFirstAsync()
Ps.help me to resolve Realm access from incorrect thread
android realm retrofit2 rx-java2
android realm retrofit2 rx-java2
asked Mar 22 at 13:57
K. DononK. Donon
143
143
add a comment |
add a comment |
2 Answers
2
active
oldest
votes
You can create wrapper over realm that will use Schedulers.single()
. It will use the same thread everytime. But I think I had to get Realm instance from that thread too.
I did it the ugly way
public long count() throws RealmException
return Single.fromCallable(() -> realmService.getRealm().where(dataTypeClass).count())
.subscribeOn(Schedulers.single())
.blockingGet();
realmService.getRealm() returns Realm instance that is lazily loaded only once.
You can also just return Single
and chain calls using RxJava.
add a comment |
I think i solve the problem by creating a new instance of Realm everytime i need to make a request.
I transformed my private function into this
private fun getUserIfExist(db: Realm, email: String): UserModel?
return db.where(UserModel::class.java).equalTo("email", email).findFirst()
I created a new private function for the deleting
private fun deleteInDb(db: Realm, email: String)
db.executeTransactionAsync
it.where(UserModel::class.java)
.equalTo("email", email)
.findFirst()
To write into the table i created a private function too
private fun writeToDB(entityToken: EntityToken, email: String): String?
realm = Realm.getDefaultInstance()
realm.executeTransactionAsync bgRealm ->
var user = findInDb(bgRealm, email)
if (user != null)
deleteInDb(bgRealm, email)
user = bgRealm.createObject(UserModel::class.java, UserModel.cachedNextId)
user!!.email = email
user.token = entityToken.token
user.refreshToken = entityToken.refresh_token
user.logged = true
realm.close()
return entityToken.refresh_token
And finaly my doLogin function became
override fun doLogin(email: String, password: String)
apiService.loginCheck(email, password)
.subscribeOn(Schedulers.newThread())
.observeOn(Schedulers.computation())
.map
// write to DB
writeToDB(it, email, password)
return@map it
.flatMap ping ->
//doPing to refresh token if not success
if (!ping.success)
return@flatMap apiService.userInfoFromServer
.observeOn(AndroidSchedulers.mainThread())
.subscribe( uR ->
updateUserInfo(uR, email)
, error ->
Log.e("tt", error.message)
)
}
Its works but i got another problem. The fact that i am sending only Realm Object, so when i declared
realm = Realm.getDefaultInstance()
It changing my RealmConfiguration 'database name' by 'default name'
Every solution is welcome!
the db name comes from RealmConfiguration.
– EpicPandaForce
Mar 28 at 13:50
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%2f55301212%2fhow-to-deal-with-rxjava-and-retrofit-with-many-access-to-realm-orm-database-and%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
2 Answers
2
active
oldest
votes
2 Answers
2
active
oldest
votes
active
oldest
votes
active
oldest
votes
You can create wrapper over realm that will use Schedulers.single()
. It will use the same thread everytime. But I think I had to get Realm instance from that thread too.
I did it the ugly way
public long count() throws RealmException
return Single.fromCallable(() -> realmService.getRealm().where(dataTypeClass).count())
.subscribeOn(Schedulers.single())
.blockingGet();
realmService.getRealm() returns Realm instance that is lazily loaded only once.
You can also just return Single
and chain calls using RxJava.
add a comment |
You can create wrapper over realm that will use Schedulers.single()
. It will use the same thread everytime. But I think I had to get Realm instance from that thread too.
I did it the ugly way
public long count() throws RealmException
return Single.fromCallable(() -> realmService.getRealm().where(dataTypeClass).count())
.subscribeOn(Schedulers.single())
.blockingGet();
realmService.getRealm() returns Realm instance that is lazily loaded only once.
You can also just return Single
and chain calls using RxJava.
add a comment |
You can create wrapper over realm that will use Schedulers.single()
. It will use the same thread everytime. But I think I had to get Realm instance from that thread too.
I did it the ugly way
public long count() throws RealmException
return Single.fromCallable(() -> realmService.getRealm().where(dataTypeClass).count())
.subscribeOn(Schedulers.single())
.blockingGet();
realmService.getRealm() returns Realm instance that is lazily loaded only once.
You can also just return Single
and chain calls using RxJava.
You can create wrapper over realm that will use Schedulers.single()
. It will use the same thread everytime. But I think I had to get Realm instance from that thread too.
I did it the ugly way
public long count() throws RealmException
return Single.fromCallable(() -> realmService.getRealm().where(dataTypeClass).count())
.subscribeOn(Schedulers.single())
.blockingGet();
realmService.getRealm() returns Realm instance that is lazily loaded only once.
You can also just return Single
and chain calls using RxJava.
edited Mar 22 at 16:51
answered Mar 22 at 16:44
TubyTuby
1,85421022
1,85421022
add a comment |
add a comment |
I think i solve the problem by creating a new instance of Realm everytime i need to make a request.
I transformed my private function into this
private fun getUserIfExist(db: Realm, email: String): UserModel?
return db.where(UserModel::class.java).equalTo("email", email).findFirst()
I created a new private function for the deleting
private fun deleteInDb(db: Realm, email: String)
db.executeTransactionAsync
it.where(UserModel::class.java)
.equalTo("email", email)
.findFirst()
To write into the table i created a private function too
private fun writeToDB(entityToken: EntityToken, email: String): String?
realm = Realm.getDefaultInstance()
realm.executeTransactionAsync bgRealm ->
var user = findInDb(bgRealm, email)
if (user != null)
deleteInDb(bgRealm, email)
user = bgRealm.createObject(UserModel::class.java, UserModel.cachedNextId)
user!!.email = email
user.token = entityToken.token
user.refreshToken = entityToken.refresh_token
user.logged = true
realm.close()
return entityToken.refresh_token
And finaly my doLogin function became
override fun doLogin(email: String, password: String)
apiService.loginCheck(email, password)
.subscribeOn(Schedulers.newThread())
.observeOn(Schedulers.computation())
.map
// write to DB
writeToDB(it, email, password)
return@map it
.flatMap ping ->
//doPing to refresh token if not success
if (!ping.success)
return@flatMap apiService.userInfoFromServer
.observeOn(AndroidSchedulers.mainThread())
.subscribe( uR ->
updateUserInfo(uR, email)
, error ->
Log.e("tt", error.message)
)
}
Its works but i got another problem. The fact that i am sending only Realm Object, so when i declared
realm = Realm.getDefaultInstance()
It changing my RealmConfiguration 'database name' by 'default name'
Every solution is welcome!
the db name comes from RealmConfiguration.
– EpicPandaForce
Mar 28 at 13:50
add a comment |
I think i solve the problem by creating a new instance of Realm everytime i need to make a request.
I transformed my private function into this
private fun getUserIfExist(db: Realm, email: String): UserModel?
return db.where(UserModel::class.java).equalTo("email", email).findFirst()
I created a new private function for the deleting
private fun deleteInDb(db: Realm, email: String)
db.executeTransactionAsync
it.where(UserModel::class.java)
.equalTo("email", email)
.findFirst()
To write into the table i created a private function too
private fun writeToDB(entityToken: EntityToken, email: String): String?
realm = Realm.getDefaultInstance()
realm.executeTransactionAsync bgRealm ->
var user = findInDb(bgRealm, email)
if (user != null)
deleteInDb(bgRealm, email)
user = bgRealm.createObject(UserModel::class.java, UserModel.cachedNextId)
user!!.email = email
user.token = entityToken.token
user.refreshToken = entityToken.refresh_token
user.logged = true
realm.close()
return entityToken.refresh_token
And finaly my doLogin function became
override fun doLogin(email: String, password: String)
apiService.loginCheck(email, password)
.subscribeOn(Schedulers.newThread())
.observeOn(Schedulers.computation())
.map
// write to DB
writeToDB(it, email, password)
return@map it
.flatMap ping ->
//doPing to refresh token if not success
if (!ping.success)
return@flatMap apiService.userInfoFromServer
.observeOn(AndroidSchedulers.mainThread())
.subscribe( uR ->
updateUserInfo(uR, email)
, error ->
Log.e("tt", error.message)
)
}
Its works but i got another problem. The fact that i am sending only Realm Object, so when i declared
realm = Realm.getDefaultInstance()
It changing my RealmConfiguration 'database name' by 'default name'
Every solution is welcome!
the db name comes from RealmConfiguration.
– EpicPandaForce
Mar 28 at 13:50
add a comment |
I think i solve the problem by creating a new instance of Realm everytime i need to make a request.
I transformed my private function into this
private fun getUserIfExist(db: Realm, email: String): UserModel?
return db.where(UserModel::class.java).equalTo("email", email).findFirst()
I created a new private function for the deleting
private fun deleteInDb(db: Realm, email: String)
db.executeTransactionAsync
it.where(UserModel::class.java)
.equalTo("email", email)
.findFirst()
To write into the table i created a private function too
private fun writeToDB(entityToken: EntityToken, email: String): String?
realm = Realm.getDefaultInstance()
realm.executeTransactionAsync bgRealm ->
var user = findInDb(bgRealm, email)
if (user != null)
deleteInDb(bgRealm, email)
user = bgRealm.createObject(UserModel::class.java, UserModel.cachedNextId)
user!!.email = email
user.token = entityToken.token
user.refreshToken = entityToken.refresh_token
user.logged = true
realm.close()
return entityToken.refresh_token
And finaly my doLogin function became
override fun doLogin(email: String, password: String)
apiService.loginCheck(email, password)
.subscribeOn(Schedulers.newThread())
.observeOn(Schedulers.computation())
.map
// write to DB
writeToDB(it, email, password)
return@map it
.flatMap ping ->
//doPing to refresh token if not success
if (!ping.success)
return@flatMap apiService.userInfoFromServer
.observeOn(AndroidSchedulers.mainThread())
.subscribe( uR ->
updateUserInfo(uR, email)
, error ->
Log.e("tt", error.message)
)
}
Its works but i got another problem. The fact that i am sending only Realm Object, so when i declared
realm = Realm.getDefaultInstance()
It changing my RealmConfiguration 'database name' by 'default name'
Every solution is welcome!
I think i solve the problem by creating a new instance of Realm everytime i need to make a request.
I transformed my private function into this
private fun getUserIfExist(db: Realm, email: String): UserModel?
return db.where(UserModel::class.java).equalTo("email", email).findFirst()
I created a new private function for the deleting
private fun deleteInDb(db: Realm, email: String)
db.executeTransactionAsync
it.where(UserModel::class.java)
.equalTo("email", email)
.findFirst()
To write into the table i created a private function too
private fun writeToDB(entityToken: EntityToken, email: String): String?
realm = Realm.getDefaultInstance()
realm.executeTransactionAsync bgRealm ->
var user = findInDb(bgRealm, email)
if (user != null)
deleteInDb(bgRealm, email)
user = bgRealm.createObject(UserModel::class.java, UserModel.cachedNextId)
user!!.email = email
user.token = entityToken.token
user.refreshToken = entityToken.refresh_token
user.logged = true
realm.close()
return entityToken.refresh_token
And finaly my doLogin function became
override fun doLogin(email: String, password: String)
apiService.loginCheck(email, password)
.subscribeOn(Schedulers.newThread())
.observeOn(Schedulers.computation())
.map
// write to DB
writeToDB(it, email, password)
return@map it
.flatMap ping ->
//doPing to refresh token if not success
if (!ping.success)
return@flatMap apiService.userInfoFromServer
.observeOn(AndroidSchedulers.mainThread())
.subscribe( uR ->
updateUserInfo(uR, email)
, error ->
Log.e("tt", error.message)
)
}
Its works but i got another problem. The fact that i am sending only Realm Object, so when i declared
realm = Realm.getDefaultInstance()
It changing my RealmConfiguration 'database name' by 'default name'
Every solution is welcome!
answered Mar 22 at 18:06
K. DononK. Donon
143
143
the db name comes from RealmConfiguration.
– EpicPandaForce
Mar 28 at 13:50
add a comment |
the db name comes from RealmConfiguration.
– EpicPandaForce
Mar 28 at 13:50
the db name comes from RealmConfiguration.
– EpicPandaForce
Mar 28 at 13:50
the db name comes from RealmConfiguration.
– EpicPandaForce
Mar 28 at 13:50
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%2f55301212%2fhow-to-deal-with-rxjava-and-retrofit-with-many-access-to-realm-orm-database-and%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