11 Replies Latest reply: Jan 29, 2018 7:34 AM by Richard Bartley RSS

    Qlik Sense Mashup - Unloading Objects from Memory

    Richard Bartley

      Hi everyone,

       

      I am having some issues with memory when loading a large number of objects that include GeoAnalytics maps. Can someone tell me how to unload an object once loaded with app.getObject()?

       

      I have experimented with getSessionObject but without any success e.g.:

      mapMY5YMY=app.getObject('mapMY5YMY','4fe5658d-f64f-4d2f-8df3-3cf3d697b882').then(function(model){
        strQID= model.id;
        });
      
      app.destroySessionObject(strQID);
      

       

       

       

      Thanks in advance.

       

      Richard

        • Re: Qlik Sense Mashup - Unloading Objects from Memory
          Richard Bartley

          I found that the way to do this is by using model.close().  However, I am using bootstrap tabs, so before doing this, I needed to identify which tab had been activated and then close all the models of the other objects.

           

          The way I resolved this was be adding some code to the .then event of the app.getObject method.  The naming of the tab links and hrefs is important. Effectively, if the tab that is being activated is different from the sheet on which the visualization is located, then the objects on all other sheets are 'unloaded' from memory.  This seems to work well and, unsurprisingly, navigation between tabs is much quicker from a user perspective.

           

          It would be nice for unloading to be dealt with on the Qlik Playground as I had to hunt around for this.

           

          app.getObject(strDivID,strQID).then( function( model ) 
          {  
            //unload when navigate away from tab
            $('a[data-toggle="tab"]').on('shown.bs.tab', function (e) {
             var target = $(e.target).attr("href") // activated tab
             if (target!='#tab'+strHTMLTab)
             {
            model.close();
             }
            });
            
          });
          
            • Re: Qlik Sense Mashup - Unloading Objects from Memory
              Richard Bartley

              p.s. If anyone knows a better way of doing this, please let me know.

                • Re: Qlik Sense Mashup - Unloading Objects from Memory
                  Jonas Hörström

                  If you use the app.visualization.get(id) instead you are able to use the visualization.close() which does just that and frees up the memory.

                   

                  viz = app.visualization.get('xGhjKl');

                  ...

                  viz.close()


                  get method ‒ Qlik Sense Developers

                  close method ‒ Qlik Sense Developers

                    • Re: Qlik Sense Mashup - Unloading Objects from Memory
                      Richard Bartley

                      Hi Jonas,

                       

                      Thanks for your response.  I am not familiar with the Visualization API and I have not seen any examples of it being used on the Playground or Qlik Community (perhaps I've been looking in the wrong place). They all seem to use app.getObject e.g. Getting started building mashups ‒ Qlik Sense Developers

                      and https://www.youtube.com/watch?v=OzzsynxIUMM

                       

                      The API seems fairly limited, so I'd be interested to hear where it is better to use the Visualization app than the 'standard' getObject approach.

                        • Re: Qlik Sense Mashup - Unloading Objects from Memory
                          Francis Kabinoff

                          The Visualization API can create a new object or fetch an existing one, and exposes methods for showing, destroying, and updating the object. The "Walker Dashboard" on Qlik Playground uses it, and I recommend you use it exclusively over app.getObject.

                            • Re: Qlik Sense Mashup - Unloading Objects from Memory
                              Richard Bartley

                              Hi Francis,

                               

                              Thanks for responding.  If i use this method, does it prevent me from using objects from the Capabilities API, such as the selection object?  Are there any limitations that I should be aware of?  Why do you recommend using it over app.getObject?

                               

                              Thanks again.

                               

                              Richard

                                • Re: Qlik Sense Mashup - Unloading Objects from Memory
                                  Francis Kabinoff

                                  For the 'CurrentSelections' object, you still need to use app.getObject. For any objects you've created in the app, you can use the Visualization API.

                                   

                                  The reason for using the Visualization API instead of the getObject method is as follows -

                                  1. Using the Visualization API, I can both get objects that exist in the app, as well as create new objects.

                                  2. With the getObject method, all I can do is get an object and show it immediately. With the Visualization API, there are methods for showing, closing, resizing, and setting options on an object. So I can delay showing the object if I'd like, or I can set the options on it easily (you can change dimensions and measures, colors, and pretty much anything about the object progamatically). Also, I can close it easily. And I see you've found a way to close the object using the getObject method, but it's an undocumented way of doing it, to me better to stick to documented way of doing it if one exists.

                                   

                                  Of course, you can use both the getObject method as well as the Visualization API at the same time, they're both made available when loading the Capability APIs. But the Visualization API is capable of more than the getObject method, with the exception of grabbing the "CurrentSelections" object, so I always use the Visualization API.

                                    • Re: Qlik Sense Mashup - Unloading Objects from Memory
                                      Richard Bartley

                                      Ok Francis, that's clearer now.  Thanks for explaining.  It would be helpful if the documentation and demos talked about these advantages more.

                                       

                                      I'm not sure which of these responses to mark as correct since Jonas came up with the answer, but you provided additional detail.  I don't think it's possible to make both correct is it?

                                        • Re: Qlik Sense Mashup - Unloading Objects from Memory
                                          Francis Kabinoff

                                          I'd say mark Jonas' as correct and mine as helpful ;-)

                                            • Re: Qlik Sense Mashup - Unloading Objects from Memory
                                              Richard Bartley

                                              Done.  Thanks to you both.

                                              • Re: Qlik Sense Mashup - Unloading Objects from Memory
                                                Richard Bartley

                                                Here's some more information that might prove useful to anyone else who needs to exporting the data behind the visualizations that was previously using the app.getObject method.

                                                 

                                                I originally hit an issue as follows

                                                 

                                                1) My original code using the app.getObject method I am able to export the data using:

                                                 

                                                 app.getObject(strDivID,strQID).then( function( model ) 
                                                  {  
                                                
                                                  $(strCmdID).prop('onclick',null).off('click');
                                                  $(strCmdID).on('click', function() 
                                                  {  
                                                  model.exportData().then(function( reply ) {  
                                                  console.log('qUrlModified', url);  
                                                  console.log('reply', reply);  
                                                  var url = 'http://' + config.host +'/auth'+reply.qUrl;
                                                  window.open(url);
                                                  });  
                                                  });
                                                  });
                                                

                                                 

                                                 

                                                2) However, when I replace this with the app.visualization.get() method, I receive an error: "Uncaught TypeError: visModel.exportData is not a function"

                                                 

                                                app.visualization.get(strQID).then(function(vis){
                                                  vis.show(strDivID);
                                                  visModel=vis.model;
                                                
                                                
                                                  $(strCmdID).prop('onclick',null).off('click');
                                                  $(strCmdID).on('click', function(visModel) 
                                                  {  
                                                
                                                  visModel.exportData().then(function( reply ) {  
                                                  console.log('qUrlModified', url);  
                                                  console.log('reply', reply);  
                                                  var url = 'http://' + config.host +'/auth'+reply.qUrl;
                                                  window.open(url);
                                                  }); 
                                                
                                                  });
                                                  });
                                                

                                                 

                                                 

                                                After looking around on the community site, I noticed that some people had used the exportData method of the Table API, so I tried this and after a bit of further de-bugging, the following works fine:

                                                 

                                                app.visualization.get(strQID).then(function(vis){
                                                  vis.show(strDivID);
                                                  visModel=vis.model;
                                                  var table = new Qlik.table(visModel);  
                                                  $(strCmdID).prop('onclick',null).off('click');
                                                  $(strCmdID).on('click', function(visModel) 
                                                  {  
                                                  table.exportData({download: true});
                                                  });
                                                });