简体   繁体   中英

iOS Project Structure for API Calls

I've started writing an iOS app that calls various web service calls to connect to data in a database and authenticate etc.

I'd like to know a good way of structuring the project, specifically the Model area (the project is MVVM) and any calls to the API which provides my data.

Currently I have:

--Models

--Views

--ViewModels

--Services

Any API wrapper code lives in services for example any of the login functionality or API calls to get something from the database.

The model code then uses these wrappers to handle the data.

I'm unsure as to what the best solution is for the structure of my project however. Say for example my API had a web service call that returned to me a list of songs:

Where's a good place to put the wrapper for the API call to the back-end?

What as a rough outline would live in my model for this example? - ie I'm a little unsure of just how much code should be in the model, should it be calling the API instead of the wrapper or should it just literally be a simple object only describing the fields in the model (song_name, artist etc), or finally should it have basic functions to call the wrapper code?

Should I use a singleton for much of the API calls as I shouldn't need to instantiate more than once for what is effectively a "helper"?

Finally if my API pulled back either a list of songs or just one song by filtering, how can this be modelled, would I need two completely different models, one a list of songs one a single song? If I have a simple class for the model that lists only the fields I can see how other code could call my API and create a list of this object but if the model contains functions to get the albums for example I can't do that.

I would expect the model to describe each attribute and have helper methods to parse the data directly to each object.

You could most definitely have a Singleton which handles the API calls and returns the model parsed.

Keep the view controllers very light-weight.

I hope this helps.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM