Downloading and managing model resources in a mobile application is a fairly common task. Model resources typically take the form of image or video data that's stored in the cloud. The general flow of an application would be to fetch a list of models, each with a url or link to a resource, and then download the resource, cache it on the device, and then play/display it.
While doing this, I've come up with some questions after running into obstacles.
First, where in the application architecture should this downloading be performed? The model class clearly isn't the place because while the user navigates through the application, you can't guarantee that model objects will stay instantiated which will cause the download request to be cancelled unexpectedly. If you're using an MVVM architecture, the place to download would seem to be the view model but that again faces the same issue as view models are attached to view controller instances and those requests are tied to the view model. When the view model deallocates, the requests will return but the original caller won't exist.
Second, is the singleton design pattern an appropriate use case here? Generally speaking, the singleton pattern should be avoided for reasons stated in this post. However I don't see a way to ensure that requests return and are cached successfully outside the view navigation without using a singleton.
Has anyone successfully built a model manager that can do all this? What I envision is some sort of singleton object combined with a protocol that model classes conform to that provides the necessary information to perform the request.
Edit:
Another possibility I was playing with was a service based architecture, something similar to feathersjs.