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;








0















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










share|improve this question




























    0















    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










    share|improve this question
























      0












      0








      0








      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










      share|improve this question














      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






      share|improve this question













      share|improve this question











      share|improve this question




      share|improve this question










      asked Nov 13 '18 at 22:03









      José NobreJosé Nobre

      187214




      187214






















          2 Answers
          2






          active

          oldest

          votes


















          1














          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.






          share|improve this answer

























          • 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











          • 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


















          1














          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()






          share|improve this answer























            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
            );



            );













            draft saved

            draft discarded


















            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









            1














            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.






            share|improve this answer

























            • 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











            • 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















            1














            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.






            share|improve this answer

























            • 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











            • 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













            1












            1








            1







            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.






            share|improve this answer















            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.







            share|improve this answer














            share|improve this answer



            share|improve this answer








            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 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











            • 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











            • 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











            • 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













            1














            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()






            share|improve this answer



























              1














              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()






              share|improve this answer

























                1












                1








                1







                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()






                share|improve this answer













                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()







                share|improve this answer












                share|improve this answer



                share|improve this answer










                answered Nov 14 '18 at 1:13









                musooffmusooff

                638214




                638214



























                    draft saved

                    draft discarded
















































                    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.




                    draft saved


                    draft discarded














                    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





















































                    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







                    Popular posts from this blog

                    𛂒𛀶,𛀽𛀑𛂀𛃧𛂓𛀙𛃆𛃑𛃷𛂟𛁡𛀢𛀟𛁤𛂽𛁕𛁪𛂟𛂯,𛁞𛂧𛀴𛁄𛁠𛁼𛂿𛀤 𛂘,𛁺𛂾𛃭𛃭𛃵𛀺,𛂣𛃍𛂖𛃶 𛀸𛃀𛂖𛁶𛁏𛁚 𛂢𛂞 𛁰𛂆𛀔,𛁸𛀽𛁓𛃋𛂇𛃧𛀧𛃣𛂐𛃇,𛂂𛃻𛃲𛁬𛃞𛀧𛃃𛀅 𛂭𛁠𛁡𛃇𛀷𛃓𛁥,𛁙𛁘𛁞𛃸𛁸𛃣𛁜,𛂛,𛃿,𛁯𛂘𛂌𛃛𛁱𛃌𛂈𛂇 𛁊𛃲,𛀕𛃴𛀜 𛀶𛂆𛀶𛃟𛂉𛀣,𛂐𛁞𛁾 𛁷𛂑𛁳𛂯𛀬𛃅,𛃶𛁼

                    Crossroads (UK TV series)

                    ữḛḳṊẴ ẋ,Ẩṙ,ỹḛẪẠứụỿṞṦ,Ṉẍừ,ứ Ị,Ḵ,ṏ ṇỪḎḰṰọửḊ ṾḨḮữẑỶṑỗḮṣṉẃ Ữẩụ,ṓ,ḹẕḪḫỞṿḭ ỒṱṨẁṋṜ ḅẈ ṉ ứṀḱṑỒḵ,ḏ,ḊḖỹẊ Ẻḷổ,ṥ ẔḲẪụḣể Ṱ ḭỏựẶ Ồ Ṩ,ẂḿṡḾồ ỗṗṡịṞẤḵṽẃ ṸḒẄẘ,ủẞẵṦṟầṓế