์ค๋์ ์๊ณ ๋ฆฌ์ฆ ๋ฌธ์ ๋ฅผ ํ๊ณ ( ํ.. ํ๋ฌธ์ .. )
๋ธ๊พธ๋ฅผ ์กฐ๊ธํ๊ณ ( ์๋.. ๋ง์ดํ๊ณ .. )
๊ฐ์๋ฅผ ๋ฃ๊ณ ( ์ ๋ฆฌ๋ ์ํ๊ณ .. )
๋ด์ผ๊น์ง ์ ์ถํด์ผํ๋ Github ๊ณผ์ ๋ฅผ ์ด์ฌํ ํ์ด์!
์๋ฎฌ๋ ์ดํฐ ์ค๋ฅ
ํ .. ์๊น๊น์ง๋ ๋ถ๋ช ํ ๋์๋๋ฐ ๊ฐ์๊ธฐ ์๋ฎฌ๋ ์ดํฐ ์ค๋ฅ๊ฐ ๋ ์
์ด๋ค ํ๋ก์ ํธ์ด๋ ๋ค ์ผ์ง์ง ์๋๋ผ๊ตฌ์!
์๋ก์ด ํ๋ก์ ํธ๋ฅผ ํ์ฑํ๋๋ฐ๋ ์ ๋ ๊ฒ ๋ ์ ์ธํฐ๋ท์๋ ์ณ๋ณด๊ณ , ๊น๋ผ์ 17.4 ๋ ๋ค์ ๊น์๋๋ฐ ํด๊ฒฐํ์ง ๋ชปํ์์ด์!
ํ .. ์ปดํจํฐ๋ฅผ ํ๋ฒ ๊ป๋ค๊ฐ ์ผ๋ด์ผ๊ฒ ๋ค ํ๊ณ ๋ ์ปดํจํฐ๋ฅผ ๊ป๋ค๊ฐ ํค๋ ํด๊ฒฐ๋์์ต๋๋ค~ ^_^
๋ฌด์จ ์ค๋ฅ์ธ์ง๋ ์์ง๋ ๋ชจ๋ฅด๊ฒ ์ด์ใ
URLSession๊ณผ Alamofire
์ค๋ ๊ณผ์ ๋ฅผ ํ๋ฉด์ URLSession๊ณผ Alamofire ๋ณต์ต ๊ฒธ ๋คํธ์ํฌ ํต์ ์ ๋ ๊ฐ์ง ๊ฒฝ์ฐ๋ก ๋ค ๊ตฌํํด๋ณด์์ด์!
Alamofire ๋ผ์ด๋ธ๋ฌ๋ฆฌ ์ฌ์ฉ ๋ฐฉ๋ฒ
ํ๋ก์ ํธ -> Package Depedencies -> Package ์์ ์ํ๋ ๋ผ์ด๋ธ๋ฌ๋ฆฌ์ ๊นํ ์ฃผ์๋ฅผ ์ ๋ ฅํ์ฌ ๋ค์ด๋ฐ์ผ๋ฉด ์๋ฃ๋ฉ๋๋ค!
URLSession
// ์ ์ ํ๋กํ
func fetchUserProfile(userName: String, completion: @escaping (Result<GithubUser, Error>) -> Void) {
guard let url = URL(string: url+"\(userName)") else {
completion(.failure(NSError(domain: "URL ๋ณํ ์คํจ", code: 401)))
return
}
var request = URLRequest(url: url)
URLSession.shared.dataTask(with: request) { data, response, error in
if let error {
completion(.failure(error))
return
}
guard let safeData = data else {
completion(.failure(NSError(domain: "data๊ฐ ์กด์ฌํ์ง ์์", code: 402)))
return
}
do {
let user = try JSONDecoder().decode(GithubUser.self, from: safeData)
completion(.success(user))
} catch {
completion(.failure(error))
}
}.resume()
}
// ๋ ํฌ์งํ ๋ฆฌ
func fetchUserRepository(userName: String, page: Int, completion: @escaping (Result<[GitRepository], Error>) -> Void) {
guard let url = URL(string: url+"\(userName)?page=\(page)") else {
completion(.failure(NSError.self as! Error))
return
}
var request = URLRequest(url: url)
URLSession.shared.dataTask(with: request) { data, response, error in
if let error {
completion(.failure(error))
return
}
guard let safeData = data else {
completion(.failure(NSError.self as! Error))
return
}
do {
let user = try JSONDecoder().decode([GitRepository].self, from: safeData)
completion(.success(user))
} catch {
completion(.failure(error))
}
}.resume()
}
Alamofire
// ์ ์ ํ๋กํ
func fetchUserProfile(userName: String, completion: @escaping (Result<GithubUser, Error>) -> Void) {
let url = url + "\(userName)"
AF.request(url).responseDecodable(of: GithubUser.self) { response in
switch response.result {
case .success(let user):
completion(.success(user))
case .failure(let error):
completion(.failure(error))
}
}
}
// ๋ ํฌ์งํ ๋ฆฌ
func fetchUserRepository(userName: String, page: Int, completion: @escaping (Result<[GitRepository], Error>) -> Void) {
let url = url + "\(userName)?page=\(page)"
AF.request(url).responseDecodable(of: [GitRepository].self) { response in
switch response.result {
case .success(let repositories):
completion(.success(repositories))
case .failure(let error):
completion(.failure(error))
}
}
}
URLSession์ผ๋ก ํต์ ํ๋ค๊ฐ Alamofire๋ก ๋ก์ง์ ๊ตฌํํ๋ ํจ์ฌ ๊ฐ๋จํด์ ์ข์์ต๋๋ค! ์์ง ์ฐ๋ ๋ฐฉ๋ฒ์ด URLSession ๋ณด๋ค๋ ์ต์ํ์ง ์์์ ์ด๋ฒ ์ฃผ์ฐจ์ ๋์จ ๊ณผ์ Alamofire๋ฅผ ์ด์ฉํด์ ๋คํธ์ํฌ ํต์ ์ ์งํํด๋ณผ๊น ํฉ๋๋ค!!