A few things to take in account
Missleading names
Then I'd make a function called
User.update()
that just pulls userdata from the server and populates the User class properties. This is the only function that would be allowed to write over the User properties.
Whoever is familiar with the Active Record design pattern would be fooled by the method's name.
While we may expect to be pushing changes to the server, in reality -according with the question- we would be doing right the opposite!!!
If we are pulling the latest User
representation, then we should consider changing the method name to something like retrieve , refresh or reload. If we are really synchronizing, sync would be more precise than update.
Too many requests
I would run the
User.update()
function during times such as:
- App launch
- App re-opened from sleep
- Profile update
- After in-app-purchase
- etc.
If we need to retrive the User
state so often, it probably means that we shouldn't hold its state. Or -at least- not completely.1
Ideally, we should request for the latest data when there's any evidence of changes and we want/need to be aware of the them. For instance, after specific business operations.
Otherwise, we will come to realise that any event/action/moment is as good as any other. Such randomness, eventually, lead us to retrieve half DB with no consistent reasons.
If App re-opened from sleep
is a good moment for a User
refresh, why should not be good for any other entity?
Keep also in mind that connections consume resources and they also have an impact on the user's data plan. So, I would constrain the calls to the server to the essential.
Consider holding the URIs instead and retrieve them as soon as you need'em, but don't hold the representantations if you find yourself synchronizing them often.
For example, we could retrieve and hold data that is unlikely to change
GET /user/1
HTTP 200 OK
{
"fullname":"Cheewacka"
}
And just hold the URIs of those resources sensible to be retrieved often. For instance, /user/1/profile
or /user/1/score
.
Retrieve these resources only when the business need them, and not randomly after random application events. Be consistent.
Finally, consider who else is going to be changing the User
beside the user itself. If there's no concurrency, probably the client application always has the most up-to-date representation. Or part of it.
1: Keep in mind that, what we do with the local storage is caching. If we have to expire the cache often, it probably means either we don't need cache at all or we are caching the wrong data.