In an Android app I work on, models are stored globally and activities are notified about changes like created, updated and deleted models. The according callbacks are called after receiving the server response.
There is a main activity that lists the models and another activity to create new ones. From there, the user can fill out a couple of fields and click on save. This triggers a server request to create the new model. After getting the response, the validated model from the server is added to the global collection and the notification callbacks are executed. The problem is what to do while waiting for the response.
- We could block the user interface an show a spinner. But that's not very responsive and results in a poor user experience.
- We could add a local preliminary model and add that to the global collection and trigger notification callbacks. This would allows the main activity to add it to the list. After the response, we update or delete the preliminary model, depending on the success or failure of the request. However, the preliminary model doesn't have its id yet, so it would be hard to find in the collection for updating or removal.
- Building on this approach, we could assign a random preliminary id to the preliminary model. However, if the user already interacts with the new model, this could create ill-defined requests that use the wrong preliminary id.
How can we handle preliminary models in multiple activities while waiting for server responses in a clear and responsive way?