Learn Courses My Dashboard

Noob questions about API and optionals management

Hi CodeCrew!

After completing the 90-day fondation course, I’m currently working on a demo app and I’m using some API from themoviedatabase that has TONS of optional values.

Example : https://developers.themoviedb.org/3/tv/get-tv-details

I’m constructed a MVVM structure with some Models, a ModelView, and Views but I’m currently wondering where I should put all the “optional management”.

As an example, if I want to load an image from the website, I need three values :

(Those are not the real names, but you’ll get the idea.)

  1. base_url
  2. picture_size
    3 picture_name

So if I want to load an image, I need to combine those three values, but each of those values could be nil.

Where should I check if those values are nil? I’ve thought of different options.

Option A) I check that everything is not nil every time I load an image in a View. That option seems redundant but maybe it’s how it should be done.

Option B) I create functions in the ModelView like getImage() that will do all the checking each time.

Option C) You need to verify all that in some way right after the api call so you don’t have to worry about nil stuff later?

Option D) Some great solution I didn’t think of.

So there ya go. I need your help and knowledge! :slight_smile: Oh and if there’s a great course or module that explains it (maybe I just don’t remember), I’d appreciate to know which one.

Thanks a lot!

I don’t think this is a noob question! It’s just learning how to do something new! We all have to start somewhere

The point at where you’re loading the image is where I would handle this, because you’re first hitting the API (fetching data) to get those values, and then you have to make another request, to get the image

btw there isn’t a “model view” in MVVM

  • Model
  • View
  • View Model

So I’m gonna guess you mean viewModel is where this code should go. Wherever you’re going to fetch the image if you end up having a value that’s nil, you’ll instead return a default image, if everything has a value, you’ll make the network request and fetch the real image


Thank you for your answer! And yes, I did mean View Model, ahah! :slight_smile: