Search code examples
odatasapui5

v2.ODataModel.refresh(true) gets updated data but doesn't update the model


I am running into a weird issue with the sap.ui.model.odata.v2.ODataModel#refresh API that prevents my UI from updating properly.

I am using a Multi Select Table via which I allow two different types of Updates that I have implemented identically

// Base Controller
getModel(name, global = false) {
   const scope = global ? this.getOwnerComponent() : this.getView();
   return scope.getModel(name);
},

updateEntry(model, path, data, options) {
    return new Promise((resolve, reject) => {
        const parameters = {
            ...options,
            success: (data, response) => {
                resolve({ data, response });
            },
            error: error => {
                reject(error);
            },
        };

        model.update(path, data, parameters);
    });
},
// Table Controller, Update Handler for both scenarios, Model "main" is bound via List Binding to my Table
onUpdate: async function (oEvent) {
    const bContinue = await SubmitDialog.init.call(this);
    if (!bContinue) return;
    const srv = this.getModel("main", true);
    // ... Fetching Table Data / some additional logic
    const aUpdates = [];
    aAssets.forEach(asset => {
        // ... Updating fields setting uri etc.
        aUpdates.push(this.updateEntry(srv, `/EntitySet${uri}`, body, { groupId: "update", refreshAfterChange: false }));
    });
    await Promise.all(aUpdates);
    srv.refresh(true);
},

and finally in the same controller

onAfterRendering: function () {
    this.byId("table").getBinding("items").attachDataReceived(() => {
        console.log("Model after update", this.getModel("main").getProperty("/"));
        this._initUIData(); // some UI changes
    });
},

The Update handler functions exactly as I am expecting it to. I first do a $batch request with all my updates followed by a $batch GET request to get the updated data to then modify my UI accordingly (i.e. displaying counters in an Icon Tab Bar etc.)

However, for one of the scenarios I can see that, although the payload I am receiving after the GET request reflects my changes, the model is simply not updated, so that my logic to update the UI fails. I am assuming that it is somehow related to my dataReceived event handler, but I am honestly not sure why. FWIW in one of the update handlers (the one that is working properly), I am closing a Dialog after the srv.refresh(true) but that's about the only thing that is different.

It may also be worth mentioning that I use Client operation mode for my OData model.

Any hints or ideas are highly appreciated.


Solution

  • I believe I may have found the solution. My second update scenario (the one that hasn't updated the model) is an update, that causes the list entries to no longer appear in my table. I believe in this case the following condition of the v2.ODataModel#refresh is not met, even though I had set the 1st parameter bForceUpdate to true.

    This will reload all data stored in the model. This will check all bindings for updated data and update the controls if data has been changed.

    I have now set the 2nd parameter bRemoveData to true as well and this seems to have fixed my issue.