How to check values from query using DAO
.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty height:90px;width:728px;box-sizing:border-box;
I have this app that has a start tutorial and I am saving in room database if the user already saw the tutorial. For that I save the data when the user finishes the tutorial and do a query in the onCreateMethod of the TutorialActivity to see if I skip to other activity. My DAO as two methods, one to insert and another to load. My load method(what I want to ask) has the following code:
@Query("SELECT * FROM tutorial LIMIT 1")
fun load(): Single<TutorialEntity>
@Insert(onConflict = OnConflictStrategy.REPLACE)
fun insert(userEntity: TutorialEntity): Single<Long>
My entity:
@Entity(tableName = "tutorial")
class TutorialEntity (
@PrimaryKey
@ColumnInfo(name = "as_seen_tutorial")
val asSeenTutorial: Long
)
This is my tutorial activity:
Note: I use dagger to inject the tutorialDao(not important here)
//here is where I check if the value is already 1 to skip
onCreate()
val load = tutorialDao.load()
if(load.? contains 1) -> This is my doubt
//OBJECTIVE: does not show and skips to other activity
//here is where I insert the flag into the database
fun showTutorial()
val entity = TutorialEntity(1)
tutorialDao.insert(entity)
I wanted to know how can I check if the value that cames from the select query is one
android sqlite kotlin android-room
add a comment |
I have this app that has a start tutorial and I am saving in room database if the user already saw the tutorial. For that I save the data when the user finishes the tutorial and do a query in the onCreateMethod of the TutorialActivity to see if I skip to other activity. My DAO as two methods, one to insert and another to load. My load method(what I want to ask) has the following code:
@Query("SELECT * FROM tutorial LIMIT 1")
fun load(): Single<TutorialEntity>
@Insert(onConflict = OnConflictStrategy.REPLACE)
fun insert(userEntity: TutorialEntity): Single<Long>
My entity:
@Entity(tableName = "tutorial")
class TutorialEntity (
@PrimaryKey
@ColumnInfo(name = "as_seen_tutorial")
val asSeenTutorial: Long
)
This is my tutorial activity:
Note: I use dagger to inject the tutorialDao(not important here)
//here is where I check if the value is already 1 to skip
onCreate()
val load = tutorialDao.load()
if(load.? contains 1) -> This is my doubt
//OBJECTIVE: does not show and skips to other activity
//here is where I insert the flag into the database
fun showTutorial()
val entity = TutorialEntity(1)
tutorialDao.insert(entity)
I wanted to know how can I check if the value that cames from the select query is one
android sqlite kotlin android-room
add a comment |
I have this app that has a start tutorial and I am saving in room database if the user already saw the tutorial. For that I save the data when the user finishes the tutorial and do a query in the onCreateMethod of the TutorialActivity to see if I skip to other activity. My DAO as two methods, one to insert and another to load. My load method(what I want to ask) has the following code:
@Query("SELECT * FROM tutorial LIMIT 1")
fun load(): Single<TutorialEntity>
@Insert(onConflict = OnConflictStrategy.REPLACE)
fun insert(userEntity: TutorialEntity): Single<Long>
My entity:
@Entity(tableName = "tutorial")
class TutorialEntity (
@PrimaryKey
@ColumnInfo(name = "as_seen_tutorial")
val asSeenTutorial: Long
)
This is my tutorial activity:
Note: I use dagger to inject the tutorialDao(not important here)
//here is where I check if the value is already 1 to skip
onCreate()
val load = tutorialDao.load()
if(load.? contains 1) -> This is my doubt
//OBJECTIVE: does not show and skips to other activity
//here is where I insert the flag into the database
fun showTutorial()
val entity = TutorialEntity(1)
tutorialDao.insert(entity)
I wanted to know how can I check if the value that cames from the select query is one
android sqlite kotlin android-room
I have this app that has a start tutorial and I am saving in room database if the user already saw the tutorial. For that I save the data when the user finishes the tutorial and do a query in the onCreateMethod of the TutorialActivity to see if I skip to other activity. My DAO as two methods, one to insert and another to load. My load method(what I want to ask) has the following code:
@Query("SELECT * FROM tutorial LIMIT 1")
fun load(): Single<TutorialEntity>
@Insert(onConflict = OnConflictStrategy.REPLACE)
fun insert(userEntity: TutorialEntity): Single<Long>
My entity:
@Entity(tableName = "tutorial")
class TutorialEntity (
@PrimaryKey
@ColumnInfo(name = "as_seen_tutorial")
val asSeenTutorial: Long
)
This is my tutorial activity:
Note: I use dagger to inject the tutorialDao(not important here)
//here is where I check if the value is already 1 to skip
onCreate()
val load = tutorialDao.load()
if(load.? contains 1) -> This is my doubt
//OBJECTIVE: does not show and skips to other activity
//here is where I insert the flag into the database
fun showTutorial()
val entity = TutorialEntity(1)
tutorialDao.insert(entity)
I wanted to know how can I check if the value that cames from the select query is one
android sqlite kotlin android-room
android sqlite kotlin android-room
asked Nov 13 '18 at 22:03
José NobreJosé Nobre
187214
187214
add a comment |
add a comment |
2 Answers
2
active
oldest
votes
Well. I don't know if i understand you well but i had some questions before respond your doubt.
Why are you using a Single? (You are going to receive a NPE) (https://kotlinlang.org/api/latest/jvm/stdlib/kotlin.collections/single.html)
I recommend you to use just the class as nullable, so just check your variable for
if (load?.asSeenTutorial == 1)
// Do your stuff
And also remember that you need to do the insert operation under an AsyncTask.
So what do you recommend to do on the dao. Do i change the return type?
– José Nobre
Nov 13 '18 at 22:32
Yes. Remove the Single, just leave it asfun load(): TutorialEntity?
– Hamlet Leon
Nov 13 '18 at 22:33
And also the single on the insert statement
– Hamlet Leon
Nov 13 '18 at 22:36
Ok I will do that. 2 minutes
– José Nobre
Nov 13 '18 at 22:37
the insert statement should be returning Long our TutorialEntity?
– José Nobre
Nov 13 '18 at 22:38
|
show 4 more comments
While you can do this using Room, it is suggested that you better not use Room for such operations. These kinds of single value checks are done using SharedPreference
.
Quoted by Google Documentation
If you have a relatively small collection of key-values that you'd like to save, you should use the SharedPreferences APIs.
With Room you have to consider creating @Entity
, @Dao
, call them in background thread. which are so much work
Here is how you do it with SharedPreference
val sharedPref = activity?.getSharedPreferences("AppPreference", Context.MODE_PRIVATE)
And you check it as below
if (sharedPref.getBoolean("as_seen_tutorial", false))
// show tutorial.
// false is a default value
else
// start another activity
And after showing your tutorial, you save "as_seen_tutorial" as true
like below
with (sharedPref.edit())
putBoolean("as_seen_tutorial", true)
commit()
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%2f53290187%2fhow-to-check-values-from-query-using-dao%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
Well. I don't know if i understand you well but i had some questions before respond your doubt.
Why are you using a Single? (You are going to receive a NPE) (https://kotlinlang.org/api/latest/jvm/stdlib/kotlin.collections/single.html)
I recommend you to use just the class as nullable, so just check your variable for
if (load?.asSeenTutorial == 1)
// Do your stuff
And also remember that you need to do the insert operation under an AsyncTask.
So what do you recommend to do on the dao. Do i change the return type?
– José Nobre
Nov 13 '18 at 22:32
Yes. Remove the Single, just leave it asfun load(): TutorialEntity?
– Hamlet Leon
Nov 13 '18 at 22:33
And also the single on the insert statement
– Hamlet Leon
Nov 13 '18 at 22:36
Ok I will do that. 2 minutes
– José Nobre
Nov 13 '18 at 22:37
the insert statement should be returning Long our TutorialEntity?
– José Nobre
Nov 13 '18 at 22:38
|
show 4 more comments
Well. I don't know if i understand you well but i had some questions before respond your doubt.
Why are you using a Single? (You are going to receive a NPE) (https://kotlinlang.org/api/latest/jvm/stdlib/kotlin.collections/single.html)
I recommend you to use just the class as nullable, so just check your variable for
if (load?.asSeenTutorial == 1)
// Do your stuff
And also remember that you need to do the insert operation under an AsyncTask.
So what do you recommend to do on the dao. Do i change the return type?
– José Nobre
Nov 13 '18 at 22:32
Yes. Remove the Single, just leave it asfun load(): TutorialEntity?
– Hamlet Leon
Nov 13 '18 at 22:33
And also the single on the insert statement
– Hamlet Leon
Nov 13 '18 at 22:36
Ok I will do that. 2 minutes
– José Nobre
Nov 13 '18 at 22:37
the insert statement should be returning Long our TutorialEntity?
– José Nobre
Nov 13 '18 at 22:38
|
show 4 more comments
Well. I don't know if i understand you well but i had some questions before respond your doubt.
Why are you using a Single? (You are going to receive a NPE) (https://kotlinlang.org/api/latest/jvm/stdlib/kotlin.collections/single.html)
I recommend you to use just the class as nullable, so just check your variable for
if (load?.asSeenTutorial == 1)
// Do your stuff
And also remember that you need to do the insert operation under an AsyncTask.
Well. I don't know if i understand you well but i had some questions before respond your doubt.
Why are you using a Single? (You are going to receive a NPE) (https://kotlinlang.org/api/latest/jvm/stdlib/kotlin.collections/single.html)
I recommend you to use just the class as nullable, so just check your variable for
if (load?.asSeenTutorial == 1)
// Do your stuff
And also remember that you need to do the insert operation under an AsyncTask.
edited Nov 13 '18 at 22:32
answered Nov 13 '18 at 22:30
Hamlet LeonHamlet Leon
1998
1998
So what do you recommend to do on the dao. Do i change the return type?
– José Nobre
Nov 13 '18 at 22:32
Yes. Remove the Single, just leave it asfun load(): TutorialEntity?
– Hamlet Leon
Nov 13 '18 at 22:33
And also the single on the insert statement
– Hamlet Leon
Nov 13 '18 at 22:36
Ok I will do that. 2 minutes
– José Nobre
Nov 13 '18 at 22:37
the insert statement should be returning Long our TutorialEntity?
– José Nobre
Nov 13 '18 at 22:38
|
show 4 more comments
So what do you recommend to do on the dao. Do i change the return type?
– José Nobre
Nov 13 '18 at 22:32
Yes. Remove the Single, just leave it asfun load(): TutorialEntity?
– Hamlet Leon
Nov 13 '18 at 22:33
And also the single on the insert statement
– Hamlet Leon
Nov 13 '18 at 22:36
Ok I will do that. 2 minutes
– José Nobre
Nov 13 '18 at 22:37
the insert statement should be returning Long our TutorialEntity?
– José Nobre
Nov 13 '18 at 22:38
So what do you recommend to do on the dao. Do i change the return type?
– José Nobre
Nov 13 '18 at 22:32
So what do you recommend to do on the dao. Do i change the return type?
– José Nobre
Nov 13 '18 at 22:32
Yes. Remove the Single, just leave it as
fun load(): TutorialEntity?
– Hamlet Leon
Nov 13 '18 at 22:33
Yes. Remove the Single, just leave it as
fun load(): TutorialEntity?
– Hamlet Leon
Nov 13 '18 at 22:33
And also the single on the insert statement
– Hamlet Leon
Nov 13 '18 at 22:36
And also the single on the insert statement
– Hamlet Leon
Nov 13 '18 at 22:36
Ok I will do that. 2 minutes
– José Nobre
Nov 13 '18 at 22:37
Ok I will do that. 2 minutes
– José Nobre
Nov 13 '18 at 22:37
the insert statement should be returning Long our TutorialEntity?
– José Nobre
Nov 13 '18 at 22:38
the insert statement should be returning Long our TutorialEntity?
– José Nobre
Nov 13 '18 at 22:38
|
show 4 more comments
While you can do this using Room, it is suggested that you better not use Room for such operations. These kinds of single value checks are done using SharedPreference
.
Quoted by Google Documentation
If you have a relatively small collection of key-values that you'd like to save, you should use the SharedPreferences APIs.
With Room you have to consider creating @Entity
, @Dao
, call them in background thread. which are so much work
Here is how you do it with SharedPreference
val sharedPref = activity?.getSharedPreferences("AppPreference", Context.MODE_PRIVATE)
And you check it as below
if (sharedPref.getBoolean("as_seen_tutorial", false))
// show tutorial.
// false is a default value
else
// start another activity
And after showing your tutorial, you save "as_seen_tutorial" as true
like below
with (sharedPref.edit())
putBoolean("as_seen_tutorial", true)
commit()
add a comment |
While you can do this using Room, it is suggested that you better not use Room for such operations. These kinds of single value checks are done using SharedPreference
.
Quoted by Google Documentation
If you have a relatively small collection of key-values that you'd like to save, you should use the SharedPreferences APIs.
With Room you have to consider creating @Entity
, @Dao
, call them in background thread. which are so much work
Here is how you do it with SharedPreference
val sharedPref = activity?.getSharedPreferences("AppPreference", Context.MODE_PRIVATE)
And you check it as below
if (sharedPref.getBoolean("as_seen_tutorial", false))
// show tutorial.
// false is a default value
else
// start another activity
And after showing your tutorial, you save "as_seen_tutorial" as true
like below
with (sharedPref.edit())
putBoolean("as_seen_tutorial", true)
commit()
add a comment |
While you can do this using Room, it is suggested that you better not use Room for such operations. These kinds of single value checks are done using SharedPreference
.
Quoted by Google Documentation
If you have a relatively small collection of key-values that you'd like to save, you should use the SharedPreferences APIs.
With Room you have to consider creating @Entity
, @Dao
, call them in background thread. which are so much work
Here is how you do it with SharedPreference
val sharedPref = activity?.getSharedPreferences("AppPreference", Context.MODE_PRIVATE)
And you check it as below
if (sharedPref.getBoolean("as_seen_tutorial", false))
// show tutorial.
// false is a default value
else
// start another activity
And after showing your tutorial, you save "as_seen_tutorial" as true
like below
with (sharedPref.edit())
putBoolean("as_seen_tutorial", true)
commit()
While you can do this using Room, it is suggested that you better not use Room for such operations. These kinds of single value checks are done using SharedPreference
.
Quoted by Google Documentation
If you have a relatively small collection of key-values that you'd like to save, you should use the SharedPreferences APIs.
With Room you have to consider creating @Entity
, @Dao
, call them in background thread. which are so much work
Here is how you do it with SharedPreference
val sharedPref = activity?.getSharedPreferences("AppPreference", Context.MODE_PRIVATE)
And you check it as below
if (sharedPref.getBoolean("as_seen_tutorial", false))
// show tutorial.
// false is a default value
else
// start another activity
And after showing your tutorial, you save "as_seen_tutorial" as true
like below
with (sharedPref.edit())
putBoolean("as_seen_tutorial", true)
commit()
answered Nov 14 '18 at 1:13
musooffmusooff
638214
638214
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%2f53290187%2fhow-to-check-values-from-query-using-dao%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