Update model according to database changes

78 Views Asked by At

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 ?

2

There are 2 best solutions below

0
MechanicalCoder On BEST ANSWER

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.

0
driconmax On

You should wait to see if the db query (the delete) ran correctly and then remove the item from the array.

The only reason to do another select, is if you want to check for any modifications in the db.