Alamofire + DictionaryCoding = NetworkService

When I wanted to develop my library for network requests for swift, I came across article
 
Why you shouldn’t use delegates in Swift I ran through the article and made my decision.
 
TL; DR
 
 
3r3196. 3r3197. class NetworkService {
Alamofire.request (URL_MOVIE_LIST, method: .get, headers: headers) .responseJSON {response in
switch response.result {
case .success:
let code = response.response? .statusCode
if (code == 200) {
if let result = response.result.value {
let jsonData = result as! NSArray
do {
let decoder = DictionaryDecoder ()
for moviesData in jsonData {
let moviesResp = try decoder.decode (Movie.self, from: moviesData as! NSDictionary)
movies.append (moviesResp)
}
self.onCompleteMovies! (movies)
} catch {
print ("caught: (error)")
self.onerror! (-? "(error)")
}
}
} else {
3r3202.
 
презентере
fetchMovies()
onCompleteMovies = {
onerror = {
func requestMovies(){
self.delegate.showProgress()
networkService.fetchMovies()
networkService.onCompleteMovies = { result in
self.movies = result
self.delegate.hideProgress()
self.delegate.moviesDidSucceed()
}
networkService.onError = { code, error in
self.delegate.moviesDidFailed(code: code, error: error);
}
}

Метод moviesDidSucceed() генерирует событие на обновление данных в таблице UITableViewController
extension MoviesTableVC : MoviesDelegate{
func moviesDidSucceed() {
tableView.reloadData()
}
}

reloadData() — обновляет таблицу. В примере показано простое решение присвоения значений с презентера.
[code]cell.movieId = (self.presenter?.movies[indexPath.row].id)!
let imgUrl = self.presenter? .movies[indexPath.row].poster
3r3202.
 
 
This is a basic solution that can be used for simple projects. For more complex, requiring testing of all parts of the project, VIPER
is probably more suitable.  
 
A complete example can be viewed on GitHub:
 
Github Sample App
+ 0 -

Add comment