A
A
artem-Kolyadin2018-06-25 13:46:01
iOS
artem-Kolyadin, 2018-06-25 13:46:01

How to properly organize interaction with the server API in relation to the VIPER architecture?

Good day to all!
I am writing an application in Swift, in which there will be a lot of requests to the server through the API. Please tell me how to properly place and organize all this in relation to the VIPER architecture. More specifically: In theory, the interactor calls the service with a request for some data? How to properly organize this service class? Singleton or just a collection of methods? Do I need to write a protocol for this service, as for other elements of the VIPER module? What are the bad / good practices for working with the server API in Swift through Alamofire (yes, in theory, it doesn’t matter, at least native)?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
S
Statusgman, 2018-07-05
@artem-Kolyadin

It is better to close the service with a protocol - this will allow you to replace its implementation, if necessary (make a mock service, for example). It is better not to get carried away with singletons: if you use the dependency manager, this will not be needed.
A service may contain the following dependencies:
- Transport (RequestManager) - an entity that makes requests to the API. Can work with Alamofire for example.
- Parser - an entity that parses JSON into PONSO/NSManagedObject
- Cache - data caching in the database, for example, in CoreData.
It turns out that the service is the logic of the operation of the combination of transport, parser and cache. If the objects received from the API are the same, you can make a generic service to receive different entities.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question