Search code examples
synchronizationconsistencyupdatemodel

Update model according to database changes


I have an array filled with objects , queried from a database and this array is displayed as a table to the user.

The user wants to delete 1 item so the server receives the request from the client and updates the database accordingly.

Question : How should I update the user's view ? should I requery the Database again and select everything from it and reasign it to my model or should I do a fast array.remove(element) ?

Which one is the preferred method ?


Solution

  • If you are dealing with a real world application than you should always query the DB for fresh result. Doing this ensures,

    1. You have consistency across the application (this approach will work for Add/update too).
    2. Data Integrity, which will help you to ensure that DB operation was successful.

    So re-query the Database again and select everything from it and reassign it to your model.