locked
Lifetime of ListDataSource and ListDataAdapter RRS feed

  • Question

  • Hi,

    we have something like the following code:

    QueryDataAdapter = function(query) {
        this.query = query;
      };

    // QueryDataAdapter.prototype.getCount

    // QueryDataAdapter.prototype.itemsFromIndex

    QueryDataAdapter.prototype.setNotificationHandler = function(notificationHandler) { return this.query.addEventListener('change', function(event) { return notificationHandler.reload(); }); };

    QueryDataSource = WinJS.Class.derive(WinJS.UI.VirtualizedDataSource, function(query) {
        return this._baseDataSourceConstructor(new QueryDataAdapter(query));
      });

    We implemented a custom IListDataAdapter, to bind a ListView to a custom data source. The adapter has a custom query object that is used to fetch the actual data. It also adds an event listener to the query that will call notificationHandler.reload() when the query changes.

    Now there is one problem: The lifetime of the query object is not limited to the lifetime of the adapter. Therefore, even when the ListDataSource that uses the adapter is no longer used in a ListView, notificationHandler.reload() is still called. So, we are wondering if there is an elegant way to remove the event listener once the data source is no longer used by any ListView. Or should it just work if we only keep a copy of the original query object? Would the adapter and query then be released once the data source is no longer used, and would the event listener be implicitly removed by releasing the query?

    Thanks,

    Guido

    Tuesday, February 28, 2012 12:37 PM

Answers

  • No there is no 'removeNotificationHandler' or equivalent.

    Once there are no references to this it would not be used, go out of scope and eventually be cleaned up.  As you finish with object you create you can set them to null.

    Is there a reason you are worried about this?

    -Jeff


    Jeff Sanders (MSFT)

    Friday, March 2, 2012 8:05 PM
    Moderator