Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. [SOLVED] Having trouble with a custom model and asynchronous data
QtWS25 Last Chance

[SOLVED] Having trouble with a custom model and asynchronous data

Scheduled Pinned Locked Moved General and Desktop
8 Posts 2 Posters 2.6k Views
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • 0 Offline
    0 Offline
    0Wayne
    wrote on last edited by
    #1

    Hi,

    I am hoping that someone may be able to give me some pointers that may help me solve a problem I am having, in the age of the internet and web services I am pretty sure I am not the first person having this problem. It may be because it is Friday? who knows?

    I have created a model class derived from QAbstractTableModel. Inside that class I have a string list of unique ids that reference some JSON that must be downloaded from the web. It is this downlaoded JSON that will provide the model with the data that the view requires.

    When the data with Qt::DisplayRole is requested from my model I am making a call to a web service to which I must wait for the JSON blob to be returned. When I have received the JSON I update the data using begin/end model reset.

    However what I am finding is that I am making a lot of extra calls to the web services while I wait for the first request to be fulfilled. There is is obviously a race of sorts occurring. Even if there is only one ID to be resolved there are about 20 extra requests on data with the Qt::DisplayRole.

    I feel that making the asynchronous call to web from my data override is probably not the best thing to be doing.

    Does any of this make sense, am I way off or is there a better way.

    Thanks

    W

    1 Reply Last reply
    0
    • SGaistS Offline
      SGaistS Offline
      SGaist
      Lifetime Qt Champion
      wrote on last edited by
      #2

      Hi,

      One thing that happens is that data is called several times anyway since you have several cells that are shown and thus updated when e.g. scrolling, showing the widget etc.

      One thing you could do is prefetch the data to have a "startup set" and then only request additional information when needed. You could also "compress" a burst of request in only one but that means that your endpoint should be able to support getting one request for several ids.

      Hope it helps

      Interested in AI ? www.idiap.ch
      Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

      1 Reply Last reply
      0
      • 0 Offline
        0 Offline
        0Wayne
        wrote on last edited by
        #3

        So all this additional calls are only natural due to to all the events that are triggering interactions between the view and the model.

        I think I will try a different approach then, so whenever I get a new list of IDs, in a separate thread, make a request for the data associated with each ID and update the model once all requests return something or timeout after a give time, or simpler may be to update the model when each request returns.

        If the list of IDs change then fetch from the endpoints and only update the rows affected and reload the model with new data.

        Sound sensible?

        1 Reply Last reply
        0
        • SGaistS Offline
          SGaistS Offline
          SGaist
          Lifetime Qt Champion
          wrote on last edited by
          #4

          You don't need to reload the complete model, just update what is needed and emit the dataChanged accordingly

          Interested in AI ? www.idiap.ch
          Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

          1 Reply Last reply
          0
          • 0 Offline
            0 Offline
            0Wayne
            wrote on last edited by
            #5

            Ok I think I am getting some where with this now, I am initially loading my model with the list of IDs in column 0. The resolved JSON will then fill the other columns.

            My problem is when the endpoints return the data, I update the affected rows and then emit dataChanged all cells in the updated rows are wiped clean. I can still see each empty row in my view, so the model knows something about my data. If the indexes I reference in the dataChanged signal are wrong, could this wipe the model or would it just gracefully fail to update?

            On a side note, will I need to do something with setData?

            1 Reply Last reply
            0
            • SGaistS Offline
              SGaistS Offline
              SGaist
              Lifetime Qt Champion
              wrote on last edited by
              #6

              AFAIK, it should not. There must be something else.

              Technically, you don't need to call setData since you manage the underlying data yourself.

              Are you sure your structure doesn't get somehow reset when you are updating the json data ?

              Interested in AI ? www.idiap.ch
              Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

              1 Reply Last reply
              0
              • 0 Offline
                0 Offline
                0Wayne
                wrote on last edited by
                #7

                Sorry for getting back so late, but once I got to the bottom of of the issue, it was full speed ahead. The reason the columns were blank was because my data was not formatted correctly instead of a list of things, it was a list of lists of things. Fixed it and I am no longer spamming the network while waiting on asynchronous data to be returned.

                Thanks for the help :

                1 Reply Last reply
                0
                • SGaistS Offline
                  SGaistS Offline
                  SGaist
                  Lifetime Qt Champion
                  wrote on last edited by
                  #8

                  Nice you found out !

                  You're welcome !

                  Happy coding :)

                  Interested in AI ? www.idiap.ch
                  Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

                  1 Reply Last reply
                  0

                  • Login

                  • Login or register to search.
                  • First post
                    Last post
                  0
                  • Categories
                  • Recent
                  • Tags
                  • Popular
                  • Users
                  • Groups
                  • Search
                  • Get Qt Extensions
                  • Unsolved