-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathNewsLoader.swift
61 lines (46 loc) · 1.52 KB
/
NewsLoader.swift
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
//
// File.swift
// DimaApp4
//
// Created by Dzmitry Miklashevich on 1/26/17.
// Copyright © 2017 Dzmitry Miklashevich. All rights reserved.
//
import Foundation
import UIKit
protocol NewsLoaderDelegate {
func didLoadNews(news: [NewsItem])
}
class NewsLoader {
var isLoading = false
var delegate: NewsLoaderDelegate? = nil
var newsTableViewCell = NewsTableViewCell()
func loadNews(){
if isLoading {
return
}
isLoading = true
let todoEndpoint: String = "http://news.tut.by/rss/index.rss"
guard let url = URL(string: todoEndpoint) else {
print("Error: cannot create URL")
return
}
let urlRequest = URLRequest(url: url)
let config = URLSessionConfiguration.default
let session = URLSession(configuration: config)
let task = session.dataTask(with: urlRequest, completionHandler: { (data, response, error) in
if let data = data {
let parser = Parser()
let newsItems = parser.parseData(data: data)
self.isLoading = false
DispatchQueue.main.async {
self.delegate?.didLoadNews(news: newsItems)
}
if let error = error{
print("Error:\(error) + Description:\(error.localizedDescription)")
return
}
}
})
task.resume()
}
}