iOS

[ iOS ] URL๋กœ ์ด๋ฏธ์ง€๋ฅผ ๋ฐ›์•„ ์˜ฌ ๊ฒฝ์šฐ Data(contentsOf:)๋ฅผ ์‚ฌ์šฉํ•˜๋ฉด ์•ˆ ๋˜๋Š” ์ด์œ 

Forest Yun 2022. 6. 7. 16:47
728x90

๊ณต๋ถ€ํ•œ ๋‚ด์šฉ์„ ์ •๋ฆฌํ•œ ๊ธ€์ž…๋‹ˆ๋‹ค.

 

 

๋ฌธ์ œ

๋„คํŠธ์›Œํฌ ๊ธฐ๋ฐ˜์˜ URL ์ด๋ฏธ์ง€๋ฅผ ๋ฐ›์•„์˜ฌ ๋•Œ Data(contentsOf:)๋ฅผ ์‚ฌ์šฉํ•ด๋„ ๋ ๊นŒ?

 

 

 

 

 

 

 


 

 

 

 

 

 

 

 

1. Data(contentsOf:)

NSData(constentsOf:)๋Š” ์ฃผ์–ด์ง„ URL๋กœ๋ถ€ํ„ฐ ๋ฐ์ดํ„ฐ๋ฅผ ๋ฐ์ดํ„ฐ ๊ฐ์ฒด๋กœ ์ดˆ๊ธฐํ™”ํ•˜๋Š” ์ž‘์—…์ด๋‹ค. ๋”ฐ๋ผ์„œ ์ด ๋ฉ”์„œ๋“œ๋ฅผ ์‚ฌ์šฉํ•ด URL๋กœ๋ถ€ํ„ฐ ์ด๋ฏธ์ง€๋ฅผ ๋ฐ›์•„์˜จ๋‹ค๋ฉด ์ž˜ ์ž‘๋™ํ•œ๋‹ค. ํ•˜์ง€๋งŒ ํ•ด๋‹น ๋ฉ”์„œ๋“œ์˜ ๊ณต์‹๋ฌธ์„œ์—๋Š” ๋„คํŠธ์›Œํฌ ๊ธฐ๋ฐ˜์˜ URL์˜ ๊ฒฝ์šฐ ํ•ด๋‹น ๋ฉ”์„œ๋“œ๋ฅผ ์‚ฌ์šฉํ•˜์ง€ ๋ง๋ผ๊ณ  ๋ช…์‹œ๋˜์–ด์žˆ๋‹ค.

 

 

[ iOS ] NSData(contentsOf:)

init(contentsOf:) ์ฃผ์–ด์ง„ URL๋กœ๋ถ€ํ„ฐ ๋ฐ์ดํ„ฐ๋ฅผ ๋ฐ์ดํ„ฐ ๊ฐ์ฒด๋ฅผ ์ดˆ๊ธฐํ™”ํ•œ๋‹ค. Technology Foundation Declaration init?(contentsOf url: URL) Parameters aURL ๋ฐ์ดํ„ฐ๋ฅผ ์ฝ์„ URL Discussion data://URLs ๋ฅผ NSData..

88yhtserof.tistory.com

Important
๋„คํŠธ์›Œํฌ ๊ธฐ๋ฐ˜์˜ URL์„ ์š”์ฒญํ•˜๊ธฐ ์œ„ํ•ด ๋™๊ธฐ์ ์ธ ์ดˆ๊ธฐํ™” ์ž‘์—…์ธ init(contentsOf:)๋ฅผ ์‚ฌ์šฉํ•˜์ง€ ๋งˆ๋ผ. ๋Š๋ฆฐ ๋„คํŠธ์›Œํฌ ๊ฒฝ์šฐ ๋„คํŠธ์›Œํฌ ๊ธฐ๋ฐ˜์˜ URL์— ๋Œ€ํ•ด ์ด ๋ฉ”์„œ๋“œ๋Š” ์ˆ˜์‹ญ ์ดˆ ๋™์•ˆ  ํ˜„์žฌ ์Šค๋ ˆ๋“œ๋ฅผ ์ฐจ๋‹จ(block)ํ•  ์ˆ˜ ์žˆ์œผ๋ฉฐ, ์ด๋Š” ์‚ฌ์šฉ์ž ๊ฒฝํ—˜์„ ์ €ํ•˜์‹œํ‚จ๋‹ค. ๊ทธ๋ฆฌ๊ณ  iOS์—์„œ๋Š” ์•ฑ์ด ์ค‘๋‹จ๋  ์ˆ˜๋„ ์žˆ๋‹ค.
๋Œ€์‹ , ํŒŒ์ผ์ด ์—†๋Š” URL์— ๋Œ€ํ•ด URLSessionํด๋ž˜์Šค์˜ dataTask(with:completionHandler:) ๋ฉ”์„œ๋“œ๋ฅผ ์‚ฌ์šฉํ•˜๋Š” ๊ฒƒ์ด ์ข‹๋‹ค. Fetching Website Data into Memory์—์„œ ์˜ˆ์‹œ๋ฅผ ๋ณผ ์ˆ˜ ์žˆ๋‹ค.

 

์ฆ‰ NSData(constentsOf:)๋Š” ๋™๊ธฐ์ ์ธ ์ž‘์—…์ด๋ฏ€๋กœ ๋งŒ์•ฝ ๋„คํŠธ์›Œํฌ๊ฐ€ ๋Š๋ฆด ๊ฒฝ์šฐ main ์Šค๋ ˆ๋“œ๊ฐ€ ์ฐจ๋‹จ๋˜์–ด ์‚ฌ์šฉ์ž์™€ ๊ฐ€์žฅ ๊ฐ€๊นŒ์šด UI๊ฐ€ ๋ฉˆ์ถ˜๋‹ค๋Š” ์˜๋ฏธ์ด๋‹ค. ๋ฉˆ์ถ”์ง€ ์•Š๋”๋ผ๋„ ์ด๋ฏธ์ง€๊ฐ€ ๋กœ๋“œ๋˜๋Š” ๋™์•ˆ์€ ๋‹ค๋ฅธ UI View๋“ค์ด ์ƒ์„ฑ๋˜์ง€ ๋ชปํ•˜๋ฏ€๋กœ ์‚ฌ์šฉ์ž ๊ฒฝํ—˜(UX)์ด ์ข‹์ง€ ์•Š๋‹ค. 

๊ทธ๋ ‡๋‹ค๋ฉด DispaychQueue๋ฅผ ์‚ฌ์šฉํ•ด global์—์„œ ์ด๋ฏธ์ง€ ๋กœ๋“œ๋ฅผ ํ•˜๋ฉด ๋˜์ง€ ์•Š์„๊นŒ?

 

 

 

 

 

2. DispatchQueue.global(). async

 

    private func confidureImagefromURL(_ url: String) {
        guard let url = URL(string: url) else {return}
        
        DispatchQueue.global().async { [weak self] in
            guard let data = try? Data(contentsOf: url) else {return}
            
            DispatchQueue.main.async {
                self?.contentImage.image = UIImage(data: data)
            }
        }
    }

์œ„ ์ฝ”๋“œ์ฒ˜๋Ÿผ DispatchQueue.global์—์„œ URL๋กœ๋ถ€ํ„ฐ ๋ฐ์ดํ„ฐ ๊ฐ์ฒด๋ฅผ ์ƒ์„ฑํ•˜๊ณ  UI์— ํ•ด๋‹น ์ด๋ฏธ์ง€๋ฅผ ํ• ๋‹นํ•˜๊ธฐ ์œ„ํ•ด DispatchQueue.main์„ ์‚ฌ์šฉํ•ด๋ณด์ž. ์ด๋ ‡๊ฒŒ ๊ตฌํ˜„ํ•˜๋ฉด ์•ฑ์— ์ด๋ฏธ์ง€๊ฐ€ ์ž˜ ๋กœ๋“œ๋˜๋ฉฐ, ์ด๋ฏธ์ง€๊ฐ€ ์•„์ง ๋กœ๋“œ๋˜์ง€ ์•Š์•˜๋”๋ผ๋„ ๋‹ค๋ฅธ UI View๋“ค์ด ์ž˜ ์ƒ์„ฑ๋œ๋‹ค. ํ•˜์ง€๋งŒ ์Šค๋ ˆ๋“œ๊ฐ€ ์ฐจ๋‹จ๋  ์œ„ํ—˜์€ ์•„์ง ์กด์žฌํ•œ๋‹ค. 

 

DispatchQueue๋ฅผ ์‚ฌ์šฉํ•ด main ์Šค๋ ˆ๋“œ๊ฐ€ block ๋˜๋Š” ๊ฑธ ๋ฐฉ์ง€ํ•  ์ˆ˜๋Š” ์žˆ์ง€๋งŒ, ์–ด์จŒ๋“  ์ œํ•œ๋œ ์ˆซ์ž์˜ GCD์˜ ์Šค๋ ˆ๋“œ ์ค‘ ํ•œ ์Šค๋ ˆ๋“œ๊ฐ€ ์ค‘๋‹จ๋˜์–ด๋ฒ„๋ฆฐ ๊ฒƒ์ด๋‹ค. ๋งŒ์•ฝ ๊ทธ๋Ÿฌํ•œ ์ƒํ™ฉ์—์„œ ๊ฐœ๋ฐœ์ž๊ฐ€ ์Šค๋ ˆ๋“œ ํ’€์˜ ๋ชจ๋“  ์Šค๋ ˆ๋“œ๋ฅผ ์‚ฌ์šฉํ–ˆ๋‹ค๋ฉด GCD๋Š” ์•„๋ฌด๊ฒƒ๋„ ํ•  ์ˆ˜ ์—†๋Š” ์ƒํƒœ๊ฐ€ ๋˜์–ด๋ฒ„๋ฆฐ๋‹ค. 

๊ทธ๋Ÿผ ๋„คํŠธ์›Œํฌ ๊ธฐ๋ฐ˜์˜ URL์—์„œ ์ด๋ฏธ์ง€๋ฅผ ๋ฐ›์•„์˜ค๋ ค๋ฉด ์–ด๋–ป๊ฒŒ ํ•ด์•ผ ํ• ๊นŒ?

 

 

 

 

 

 

3. URLSession

๊ณต์‹๋ฌธ์„œ์—์„œ๋Š” URLSession์˜ dataTask(with:completionHandler:) ์‚ฌ์šฉ์„ ์ถ”์ฒœํ•œ๋‹ค.

URLSession์„ ํ†ตํ•ด ์ด๋ฏธ์ง€๋ฅผ ๋ฐ›์•„์˜ค๋ฉด Data(contentsOf:)์„ ํ†ตํ•ด ๋ฐ›์•„์˜ค๋Š” ๊ฒƒ๊ณผ๋Š” ๋‹ค๋ฅด๊ฒŒ, ์˜ค๋ฅ˜ ๋ฐœ์ƒ ์‹œ ์˜๋ฏธ ์žˆ๋Š” ์ง„๋‹จ์„ ํ•  ์ˆ˜ ์žˆ์œผ๋ฉฐ ์Šค๋ ˆ๋“œ๊ฐ€ block ๋˜๋Š” ๊ฒƒ์„ ๋ฐฉ์ง€ํ•  ์ˆ˜ ์žˆ๋‹ค.

 

Apple์ด ์ œ๊ณตํ•˜๋Š” Article ์ค‘ Fetching Website Data into Memory์—์„œ๋Š” ์•„๋ž˜์™€ ๊ฐ™์€ ์ฝ”๋“œ๋ฅผ ์˜ˆ์‹œ๋กœ ๋ณด์—ฌ์ค€๋‹ค.

func startLoad() {
    let url = URL(string: "https://www.example.com/")!
    let task = URLSession.shared.dataTask(with: url) { data, response, error in
        if let error = error {
            self.handleClientError(error)
            return
        }
        guard let httpResponse = response as? HTTPURLResponse,
            (200...299).contains(httpResponse.statusCode) else {
            self.handleServerError(response)
            return
        }
        if let mimeType = httpResponse.mimeType, mimeType == "text/html",
            let data = data,
            let string = String(data: data, encoding: .utf8) {
            DispatchQueue.main.async {
                self.webView.loadHTMLString(string, baseURL: url)
            }
        }
    }
    task.resume()
}

 

 

 

 

 

 

4. Alamofire

๋„คํŠธ์›Œํฌ ์—ฐ๊ฒฐ ์‹œ ๋งŽ์ด ์‚ฌ์šฉํ•˜๋Š” Alamofire๋Š” Apple์—์„œ ์ œ๊ณตํ•˜๋Š” ๋„คํŠธ์›Œํฌ API์ธ URLSession์— ๊ธฐ๋ฐ˜ํ•˜์—ฌ ๋งŒ๋“ค์–ด์ง„ ๋ผ์ด๋ธŒ๋Ÿฌ๋ฆฌ์ด๋‹ค. ๋”ฐ๋ผ์„œ  Alamofire ๋˜ํ•œ ๋„คํŠธ์›Œํฌ ๊ธฐ๋ฐ˜ URL๋กœ๋ถ€ํ„ฐ ๋ฐ์ดํ„ฐ๋ฅผ ๋ฐ›์•„์˜ค๋Š” ๋ฐ ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ๋‹ค.

 

 

[ iOS ] URL ์ด๋ฏธ์ง€ ๋ฐ›์•„์˜ค๊ธฐ + Alamofire

๋ฌธ์ œ Alamofire๋ฅผ ์‚ฌ์šฉํ•ด URL์—์„œ ์ด๋ฏธ์ง€ ๋ฐ›์•„์˜ค๊ธฐ Unsplash์™€ ๊ฐ™์€ Open API๋‚˜ ์„œ๋ฒ„์—์„œ ์‚ฌ์ง„์„ ๋ฐ›์„ ๊ฒฝ์šฐ URL์„ UIImage๋กœ ๋ณ€๊ฒฝํ•˜๋Š” ์ž‘์—…์„ ์ฒ˜๋ฆฌํ•ด์•ผํ•œ๋‹ค. ๋ณดํ†ต Data(constentsOf:)๋ฅผ ์‚ฌ์šฉํ•˜์ง€๋งŒ ๊ณต์‹๋ฌธ์„œ์—์„œ

88yhtserof.tistory.com

 

 

 

 

 

 

 

 

 

 


 

 

 

 

 

 

 

 

 

 

 

Never load data from a remote URL with synchronous API Data(contentsOf)
init(contentsOf:)
Fetching Website Data into Memory
GCD
728x90