0
votes

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.

1

1 Answers

0
votes

It will all depend on the UX needed and the quantity of resources but I am generally forced to download and cache images before it is actually needed so that the user is not stuck waiting thus forcing implementation in a viewcontroller.

For images, I usually use https://github.com/rs/SDWebImage

Videos, I find that users are expecting to have it preload for a while so it does not justify wasting space pre-caching them unless offline use is expected.

As for singleton usage, I avoid it. If it is in database it can be retrieved anywhere and if in memory, it can be passed from controller to controller. It is really common (even popular) to use them for this tough, some will also use the app delegate.