Learn what Felgo offers to help your business succeed. Start your free evaluation today! Felgo for Your Business

Forums

OverviewFelgo 1 Support › storage – not persistent

Viewing 5 posts - 1 through 5 (of 5 total)
  • Author
    Posts
  • #6236

    Michael

    Hi

     

    I’m sure this is a noob issue. I’m loading words into Storage (read/parsed from a data file), so I presume once I set a value there (using setValue) that it becomes persistent (i.e. next time, I run the data is available) but when I run the following test on my Windows PC, I never get persistent storage, i.e. I have to reload upon each build/run. Please let me know where i’ve gone wrong. Thanks. BTW I will persue the better ways in future, but this first step is just to help me along with some basic understandings.

    import VPlay 1.0
    import QtQuick 1.1
    
    GameWindow {
        activeScene: scene
        width: 960; height: 640
    
        property string loadStatus: "load failed"
    
        Storage {
          id: myLocalStorage
    //      databaseName: "words"
    
          onStorageError: {
            console.debug("myLocalStorage: there was an error:", errorData.message)
          }
        }
    
        Component.onCompleted: {
    
          var isFirstStartApplication = myLocalStorage.getValue("ZEBRA")
    
          if (isFirstStartApplication == undefined) {
    
              // if undefined is returned, load up database
    
              var storageData = fileUtils.readFile("files/dictionary.txt")
    
              if (storageData) {
    
                  loadStatus = "loaded OK!"
    
                  var arr = storageData.split(",");
                  console.debug("words read = " + arr.length)
                  loadStatus = "words read = " + arr.length
    
                  // parse cvs file to populate storage
    
                  var obj = {};
                  for (var i = 0; i < arr.length - 1; i += 1) {
                      myLocalStorage.setValue(arr[i + 1], true)
                  }
    
              } else {
                  loadStatus = "loaded failure!"
                  console.debug("loaded file FAILED")
              }
    
            }
    
        }
    
        Scene {
            id: scene
            width: 480; height: 320
    
            Text { // notification
                id: status
                text: loadStatus
                anchors.centerIn: parent
                color: "white"
            }
    
            Rectangle {
                id: rectangle
                color: "grey"
                width: 100; height: 50
    
                Text {
                    id: textElement
                    text: qsTr("Lookup word")
                    color: "#ffffff"
                    anchors.centerIn: parent
                }
    
                MouseArea {
                    anchors.fill: parent
                    onPressed: {
    
                        if (myLocalStorage.getValue("ZEBRA")) {
                            status.text = "word found!"
                        }
                        else {
                            status.text = "word NOT found!"
                        }
    
                    }
    
                }
            }
        }
    }

     

    #6239

    Christian
    Felgo Team

    Hi Michael,

    the problem here is not the storage persistence, but the readFile function. As I do not have your file contents I cannot reproduce the problem – but you can test it without the file reading, if ou just call something like the following in onCompleted:

    myLocalStorage.setValue("ZEBRA", "abc")

    If you did that, you will also see that your else-branch should be one colon behind, like in this modified version:

    var isFirstStartApplication = myLocalStorage.getValue("ZEBRA")
    
          if (isFirstStartApplication == undefined) {
    
              // if undefined is returned, load up database
    
              var storageData = fileUtils.readFile("files/dictionary.txt")
    
              if (storageData) {
    
                  loadStatus = "loaded OK!"
    
                  var arr = storageData.split(",");
                  console.debug("words read = " + arr.length)
                  loadStatus = "words read = " + arr.length
    
                  // parse cvs file to populate storage
    
                  var obj = {};
                  for (var i = 0; i < arr.length - 1; i += 1) {
                      myLocalStorage.setValue(arr[i + 1], true)
                  }
    
              } /* DONT put the else branch here, but in the following position
              else {
                  loadStatus = "loaded failure!"
                  console.debug("loaded file FAILED")
              }*/
    
            } else {
              loadStatus = "loaded failure!"
              console.debug("loaded file FAILED")
            }
            
          }// end of onCompleted

    Cheers, Chris

     

     

     

    #6245

    Michael

    Thanks alot for your help. I’ve realised the issue. I’ve found often that not all my words are being stored in the sqlite file. Since I’m testing isFirstStartApplication using a word that is near the end of my word list, that word is sometimes not stored or present in the database, so it fails causing the reload. I’ve had a look in the sqlite database and noticed my all my words from the start are being stored to a point, but not all the words. I repeated this test exercise using

    var isFirstStartApplication = myLocalStorage.getValue("AA")

    where AA is the first word read in & stored, and the persistent test is passed, i.e. words were saved, and things are better.

    However as an exercise, when I truncated my word list to 126 words & use the following to populate the sqlite db

    for (var i = 0; i < arr.length; i += 1) {
      myLocalStorage.setValue(arr[i], true)
    }

    sometimes all the words make it, but sometimes not. Why is that?

    I guess this loop is not the best method, and I should use the json file method.

    Cheers,

    Michael.

     

    #6247

    Michael

    I’ve done a lot of testing, and moved to reading the data, then using JSON.parse, but unless there is some effecient JSON to storage conversion routine, I’ve realised my issue. After parsing the JSON to an obj, I’m looping through the object one by one to populate the local storage one by one using setValue. It’s too slow. I’ve realised that I’m quiting the test app before all the data has been written. This is my issue. I think it’s impractical to use this method to prepare a large database.

    #6255

    Christian
    Felgo Team

    Hi Michael,

    you are right. In your case (and especially as you are not writing any new words), using a read-only map is the best solution.

    Alternatively, you can add your own SQL database logic and only call a single write transaction which will give you the best performance for your scenario.

    Cheers, Chris

Viewing 5 posts - 1 through 5 (of 5 total)

RSS feed for this thread

You must be logged in to reply to this topic.

Qt_Technology_Partner_RGB_475 Qt_Service_Partner_RGB_475_padded