TL;DR
What's the best way to handle dependency between types of data that is loaded asynchronously from different backend endpoints?
Problem
My app fetches data from a backend, for each entity I have an endpoint to fetch all instances. For example http://ift.tt/1Pz9NaV for User model and http://ift.tt/1mcv12u for Thing model.
This data is parsed and placed into data store objects (e.g. UserDataStore and ThingDataStore) that serve these models to the rest of the app.
Question
What should I do if the data that comes from /things depends on data that comes from /users and the fetch operations are async. In my case /things returns the id of a user that created them. This means that if /things returns before /users, then I won't have enough data to create the Thing model.
Options
-
Have
/thingsreturn also relevant/usersdata nested.This is bad because:
- I'll then have multiple model instances
Userfor the same actual user - one that came from/usersand one that came nested in/things. - Increases the total payload size transferred.
- In a system with some permission policy, data that is returned for
/userscan be different to/things, and then it'll allow partially populated models to be in the app.
- I'll then have multiple model instances
-
Create an operational dependency between the two data stores, so that
ThingsDataStorewill have to wait forUserDataStoreto be populated before it attempts to load its own data.This is also bad because:
- Design-wise this dependency is not welcome.
- Operational-wise, it will very quickly become complicated once you throw in another data stores (e.g. dependency cycles, etc).
What is the best solution for my problem and in general?
Aucun commentaire:
Enregistrer un commentaire