Skip to content

๐Ÿ”† ๋‚ ์”จ OpenAPI๋ฅผ ์ด์šฉํ•ด์„œ Apple ๊ธฐ๋ณธ ๋‚ ์”จ์•ฑ ๋˜๋Š” Yahoo ๋‚ ์”จ์•ฑ๊ณผ ์œ ์‚ฌํ•œ ๊ธฐ๋Šฅ์…‹์„ ๊ตฌํ˜„ํ•˜๊ธฐ

Notifications You must be signed in to change notification settings

daheenallwhite/WeatherApp

Folders and files

NameName
Last commit message
Last commit date

Latest commit

ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 

Repository files navigation

WeatherApp

๋‚ ์”จ OpenAPI๋ฅผ ์ด์šฉํ•ด์„œ Apple ๊ธฐ๋ณธ ๋‚ ์”จ์•ฑ ๋˜๋Š” Yahoo ๋‚ ์”จ์•ฑ๊ณผ ์œ ์‚ฌํ•œ ๊ธฐ๋Šฅ์…‹์„ ๊ตฌํ˜„ํ•˜๊ธฐ

ย 

Index


ย 


๊ธฐ๋Šฅ

๋‚ ์”จ ์ •๋ณด

์œ„์น˜ ๋ฆฌ์ŠคํŠธ(ํ˜„์žฌ ์œ„์น˜ + ์ €์žฅ๋œ ์œ„์น˜)์˜ ๋‚ ์”จ๋ฅผ ๊ฐ๊ฐ ๊ฐ€๋กœ ์Šคํฌ๋กค๋กœ ์ „ํ™˜ํ•˜์—ฌ ์กฐํšŒ ๊ฐ€๋Šฅ

์žฅ์†Œ ์ €์žฅ ๋ฐ ๋ฆฌ์ŠคํŠธ ์กฐ์ž‘

์ถ”๊ฐ€ํ–ˆ๋˜ ์žฅ์†Œ ๋ฆฌ์ŠคํŠธ๋ฅผ ๋ณผ ์ˆ˜ ์žˆ๊ณ , ์‚ญ์ œํ•  ์ˆ˜ ์žˆ๋‹ค

์žฅ์†Œ ๊ฒ€์ƒ‰ ๋ฐ ์ถ”๊ฐ€

์›ํ•˜๋Š” ์žฅ์†Œ๋ฅผ ๊ฒ€์ƒ‰ํ•˜์—ฌ ์ถ”๊ฐ€ํ•˜๊ณ , ๊ด€๋ จ ๋‚ ์”จ ์ •๋ณด๋ฅผ ๋ณผ ์ˆ˜ ์žˆ๋‹ค

์˜จ๋„ ๋‹จ์œ„ ์„ค์ •

์˜จ๋„ ๋‹จ์œ„๋ฅผ ์„ญ์”จ ํ˜น์€ ํ™”์”จ๋กœ ๋ณ€ํ™˜ํ•˜์—ฌ ๋ณผ ์ˆ˜ ์žˆ๋‹ค

์‚ฌ์šฉ์ž ์„ค์ • ์ €์žฅ

๋‹ค์Œ ์„ค์ •์„ ์ €์žฅํ•˜์—ฌ ์•ฑ์„ ๋‹ค์‹œ ์‹คํ–‰์‹œ, ๊ธฐ์กด์˜ ์„ค์ •๋Œ€๋กœ ์‹คํ–‰๋˜๋„๋ก ํ•œ๋‹ค

์ถ”๊ฐ€ํ•œ ์žฅ์†Œ ์˜จ๋„ ๋‹จ์œ„ ์„ค์ • ๋งˆ์ง€๋ง‰์œผ๋กœ ๋ณธ ํŽ˜์ด์ง€

ย 


์„ค๊ณ„ ๋ฐ ๊ตฌํ˜„

View Controller ๊ตฌ์„ฑ

ย 

๋‚ ์”จ ๋ชจ๋ธ๊ณผ View - MVVM

WeatherViewController - WeatherViewModel

  • ViewModel ์˜ view ๊ด€๋ จ type ์— Observer ๋ฅผ ๋“ฑ๋กํ•  ์ˆ˜ ์žˆ๋Š” ํƒ€์ž…์„ ๊ตฌํ˜„
    • Observable protocol
    • ViewModel - CurrentWeather, HourlyWeatherItem, DailyWetherItem, DetailWeather ๋Œ€์ƒ data ๋ณ€ํ™”์— ๋Œ€ํ•ด observer ๋ฅผ ๋“ฑ๋กํ•  ์ˆ˜ ์žˆ๋‹ค
    • observer handler ์— ๊ด€๋ จ view ๋‚˜ label text ๋ฅผ ๋ณ€๊ฒฝํ•  ์ˆ˜ ์žˆ๋Š” ํ•จ์ˆ˜๋ฅผ ๋“ฑ๋ก
    • view model ๋ณ€๊ฒฝ์‹œ์— ํ•ด๋‹น observer๊ฐ€ ์‹คํ–‰๋˜์–ด view ๋„ ๊ฐ™์ด ๊ทธ์— ๋งž๊ฒŒ ์—…๋ฐ์ดํŠธ ๋œ๋‹ค

ย 

์—ญํ•  ๋ถ„๋ฐฐ

view ๊ด€๋ จ

class / struct ์—ญํ• 
PageViewController LocationManager ์‚ฌ์šฉํ•ด์„œ ๋ฐ›์€ ํ˜„์žฌ์œ„์น˜๋ฅผ ํฌํ•จํ•˜์—ฌ, ์ €์žฅ๋œ ์œ„์น˜์˜ ๋‚ ์”จ๋ฅผ ๋ณด์—ฌ์ค„ WeatherViewController ๋ฅผ ๋ณด์—ฌ์ค€๋‹ค
WeatherViewController Location ๊ฐ์ฒด์— ํ•ด๋‹นํ•˜๋Š” ์œ„์น˜ ์ •๋ณด๋ฅผ ๋ณด์—ฌ์ค€๋‹ค
WeatherViewModel - OpenWeatherMapService ๋ฅผ ์ด์šฉํ•˜์—ฌ ์œ„์น˜์— ๋งž๋Š” ๋‚ ์”จ ์ •๋ณด๋ฅผ ๊ฐ€์ ธ์˜จ๋‹ค
- WeatherBuilder ๊ฐ์ฒด๋ฅผ ํ†ตํ•ด ๊ฐ€์ ธ์˜จ ์ •๋ณด๋ฅผ view ์— ํ•„์š”ํ•œ ๋ฐ์ดํ„ฐ ํƒ€์ž…๋“ค๋กœ ๋งŒ๋“ ๋‹ค
WeatherBuilder ๋„คํŠธ์›Œํ‚น์„ ํ†ตํ•ด ๋ฐ›์•„์˜จ WeatherData ๊ฐ์ฒด๋ฅผ view model ์˜ ๊ฐ ํ•„์š”์— ๋งž๊ฒŒ ๊ฐ€๊ณตํ•œ๋‹ค
LocationListViewController - ์ €์žฅ๋œ ์œ„์น˜๋ฅผ ์‚ฌ์šฉ์ž์—๊ฒŒ ๋ณด์—ฌ์ค€๋‹ค
- ์ €์žฅ๋œ ์œ„์น˜๋ฅผ ์‚ญ์ œํ•œ๋‹ค
- ์˜จ๋„ ๋‹จ์œ„๋ฅผ ๋ฐ”๊พผ๋‹ค
- SearchViewController ๋ฅผ ๋ณด์—ฌ์ค€๋‹ค
SearchViewController - ์‚ฌ์šฉ์ž ๊ฒ€์ƒ‰ ๋ฌธ์ž์—ด์„ ์‚ฌ์šฉํ•˜์—ฌ ์œ„์น˜ ์ž๋™์™„์„ฑ
- ์‚ฌ์šฉ์ž๊ฐ€ ์„ ํƒํ•œ ์œ„์น˜ ์ •๋ณด(์žฅ์†Œ์ด๋ฆ„, ์œ„๋„, ๊ฒฝ๋„) ๋ฅผ LocationViewController delegate ํ†ตํ•ด ๋„˜๊ธด๋‹ค
Observable - ViewModel ์˜ ๊ฐ ๋ฐ์ดํ„ฐ ํƒ€์ž…์— observer ๊ธฐ๋Šฅ์„ ๊ตฌํ˜„ํ•˜๊ธฐ ์œ„ํ•œ generic type
- WeatherViewModel ์—์„œ observer ๋“ฑ๋ก์ด ํ•„์š”ํ•œ ์†์„ฑ์— ์‚ฌ์šฉ๋จ

Utilities

class / struct ์—ญํ• 
LocationManager CLLocationManager ์‚ฌ์šฉํ•˜์—ฌ ํ˜„์žฌ ์œ„์น˜ ๊ฐ€์ ธ์˜ค๊ธฐ
OpenWeatherMapService - ๋„คํŠธ์›Œํ‚น ํ†ตํ•œ ๋‚ ์”จ ์˜ˆ์ธก ์ •๋ณด ๊ฐ€์ ธ์˜ค๊ธฐ
- WeatherData ํƒ€์ž…์œผ๋กœ json decoding ํ•˜๊ธฐ
WeatherBuilder WeatherData โ†’ WeatherViewModel ์ด ๊ฐ€์ง„ ๊ฐ type์œผ๋กœ ๋ฐ์ดํ„ฐ ๊ฐ€๊ณต
WindDirection ๋ฐ”๋žŒ์˜ ๋ฐฉํ–ฅ ๊ฐ’(360๋„ ๋‚ด)์„ compass direction์œผ๋กœ ๋ณ€ํ™˜
DateConverter ์ฃผ์–ด์ง„ timezone ์„ ์‚ฌ์šฉํ•˜์—ฌ ๋ฌธ์ž์—ด๋กœ ๋œ ๋‚ ์งœ๋ฅผ ๋ณ€ํ™˜ํ•ด ์ฃผ๋Š” ์—ญํ• 

ย 

Weather Model Hierarchy

ย 

ViewController ๊ฐ„ Data ์ฃผ๊ณ  ๋ฐ›๊ธฐ - Delegate ์‚ฌ์šฉ

๊ด€๋ จ ํ•™์Šตํ•œ ๋‚ด์šฉ- 1 ๊ด€๋ จ ํ•™์Šตํ•œ ๋‚ด์šฉ- 2

view controller ๊ฐ„ ๋ฐ์ดํ„ฐ๋ฅผ backward ๋กœ ๋ฐ›๊ธฐ ์œ„ํ•ด์„œ, delegate protocol ์„ ๊ตฌํ˜„ํ•˜์—ฌ ์‚ฌ์šฉ

ย 

ํ˜„์žฌ ์œ„์น˜ - CLLocationManager ํ™œ์šฉ

LocationMagnager class ๋กœ ๊ตฌํ˜„

์œ„์น˜ ๊ฐ€์ ธ์˜ค๋Š” ๊ณผ์ •

  1. CLLocationManager ๊ฐ์ฒด ์ƒ์„ฑ
  2. location ๋ฐ์ดํ„ฐ์˜ ์ •ํ™•๋„ ์„ค์ • : desiredAccuracy property ์„ค์ •
  3. ์‚ฌ์šฉ์ž์—๊ฒŒ ์œ„์น˜์ •๋ณด ์‚ฌ์šฉ ํ—ˆ๊ฐ€ ๋ฐ›๊ธฐ : requestWhenInUseAuthorization() method
  4. ์œ„์น˜ ์š”์ฒญ์ด ๊ฐ€๋Šฅํ•œ ํ—ˆ๊ฐ€ ์ƒํƒœ CLAuthorizationStatus : .authorizedWhenInUse / .authorizedAlways
  5. ์œ„์น˜ ์š”์ฒญ: requestLocation()
    • ํ•ด๋‹น method๋Š” ์ฆ‰๊ฐ return ํ•œ๋‹ค
    • ์œ„์น˜ ๊ฐ’์„ ์–ป์€ ํ›„, delegate ์˜ didUpdateLocation method ๋ฅผ ํ˜ธ์ถœํ•œ๋‹ค
  6. Delegate method - didUpdateLocation

ย 

๋‚ ์”จ ์ •๋ณด ๋ฐ›์•„์˜ค๊ธฐ & ํŒŒ์‹ฑํ•˜๊ธฐ - OpenWeather API / URLSession / Codable

5 days / 3 hours forecast api

API JSON ๊ตฌ์กฐ (orange color : ๋ฐฐ์—ด ๊ตฌ์กฐ)

ย 

์žฅ์†Œ ๊ฒ€์ƒ‰ & ์ž๋™์™„์„ฑ - MKLocalSearchCompleter

๋ฌธ์ž์—ด๋กœ ์œ„์น˜๋ฅผ ์ œ๊ณตํ•˜๋ฉด ๊ทธ์— ๋งž๋Š” ์ž๋™์™„์„ฑ๋œ comletion string list ๋ฅผ ์ œ๊ณตํ•˜๋Š” utility ๊ฐ์ฒด

  • ๊ตฌํ˜„ ์›๋ฆฌ

  • results property : MKLocalSearchCompleter ์˜ ์ž๋™์™„์„ฑ ์ฒ˜๋ฆฌ๋œ ๋ฐ์ดํ„ฐ๋ฅผ ์–ป๋Š” ์†์„ฑ

    • MKLocalSearchCompletion type
    • ์ง์ ‘ ์ƒ์„ฑํ•  ์ˆ˜๋Š” ์—†๋‹ค. Completer ์— ์˜ํ•ด์„œ๋งŒ ์ƒ์„ฑ๋˜๋Š” ๊ฐ์ฒด
  • completion ๋  ๋Œ€์ƒ ์ง€์ • ๋ฐฉ๋ฒ•

    • ์œ„์น˜ ๋ฌธ์ž์—ด, ์ง€์—ญ, ํ•„ํ„ฐ ํƒ€์ž… ๋“ฑ์„ ์ง€์ •ํ•  ์ˆ˜ ์žˆ๋‹ค.
    • ๋„์‹œ๋ช… ๊ฒ€์ƒ‰ : queryFragment property ์— ์‚ฌ์šฉ์ž๊ฐ€ ์ž…๋ ฅํ•˜๋Š” ๋ฌธ์ž์—ด ์„ค์ •
    • ํ•„ํ„ฐ ํƒ€์ž… : locationAndQueries / locationsOnly
  • delgate : search completion data ๋ฅผ ๊ฐ€์ ธ์˜ค๊ธฐ ์œ„ํ•œ ๋ฉ”์†Œ๋“œ๊ฐ€ ์ •์˜๋จ

    • MKLocalSearchCompleterDelegate
    • completerDidUpdateResults() ๋ฉ”์†Œ๋“œ : completer ๊ฐ€ ๊ฒ€์ƒ‰ ์™„์„ฑ ๋ฐฐ์—ด์„ ์—…๋ฐ์ดํŠธ ํ•œ ๋’ค ํ˜ธ์ถœํ•˜๋Š” ๋ฉ”์†Œ๋“œ.
    • ์ด ๋ฉ”์†Œ๋“œ ์•ˆ์— search ๊ฒฐ๊ณผ table view ๋ฅผ reload ํ•˜๋„๋ก ๊ตฌํ˜„ํ•จ

ย 

์‚ฌ์šฉ์ž ์„ค์ • ์ €์žฅ - UserDefaults

๊ด€๋ จ ํ•™์Šตํ•œ ๋‚ด์šฉ

์‚ฌ์šฉ์ž ์„ค์ • ํ•ญ๋ชฉ

  • ๋งˆ์ง€๋ง‰์œผ๋กœ ๋ณธ ๋‚ ์”จ์˜ ์œ„์น˜
  • ์‚ฌ์šฉ์ž๊ฐ€ ์ €์žฅํ•œ ์œ„์น˜ ๋ฆฌ์ŠคํŠธ
  • ์˜จ๋„ ๋‹จ์œ„ ์„ ํƒ ์ •๋ณด

UserDefaults ์— ์‚ฌ์šฉ๋  key ๊ด€๋ฆฌํ•˜๋Š” struct DataKeys

ย 

API ๋ฐ์ดํ„ฐ ๊ธฐ๋ฐ˜ ์‹œ๊ฐ„ ๊ตฌํ•˜๊ธฐ

API ์—์„œ ๋ฐ›์•„์˜จ date & time (UTC ํ‘œ์ค€) โ†’ ๊ฐ ๋‚˜๋ผ๋ณ„ ์‹œ๊ฐ„์œผ๋กœ ๋ณ€ํ™˜ํ•˜๊ธฐ

  1. list.dt_text string (utc ๋‹จ์œ„ ์‹œ๊ฐ„) โ†’ Date ๊ฐ์ฒด๋กœ ๋ณ€ํ™˜
  2. city.timezone : ํ•ด๋‹น ๋„์‹œ์˜ ์‹œ๊ฐ„์„ UTC๋กœ๋ถ€ํ„ฐ ๋ณ€ํ™˜ํ•˜๊ธฐ ์œ„ํ•œ ์ฐจ์ด๊ฐ’. ๋‹จ์œ„๋Š” ์ดˆ
  3. ๊ฐ ๋„์‹œ์˜ ์‹œ๊ฐ„ = list.dt_txt ๋ฅผ date๋กœ ๋ณ€ํ™˜ํ•œ ๊ฐ์ฒด + city.timezone
  4. ์ฐจ์ด๊ฐ’ ๋”ํ•ด์ฃผ๊ธฐ : Date - addingTimeInterval() method ์‚ฌ์šฉ

ย 

์˜จ๋„ ๋‹จ์œ„ ์„ค์ •๋Œ€๋กœ ์ •๋ณด ๋ณด์—ฌ์ฃผ๊ธฐ - Singleton ํ™œ์šฉ

  • Singleton ์œผ๋กœ ๊ตฌํ˜„ํ•œ ์ด์œ 

    • view controller ๋ฟ๋งŒ ์•„๋‹ˆ๋ผ ๋‚ ์”จ ๊ด€๋ จ๋œ ๊ฑฐ์˜ ๋ชจ๋“  data model ์—์„œ ์˜จ๋„์™€ ๊ด€๋ จ๋œ ๋ถ€๋ถ„์ด ๋งŽ์Œ
    • Singleton ํ†ตํ•ด์„œ ํ•˜๋‚˜์˜ ์ธ์Šคํ„ด์Šค๋กœ ์‚ฌ์šฉ์ž๊ฐ€ ์„ค์ •ํ•œ ์˜จ๋„ ๋‹จ์œ„๋ฅผ ์ด์šฉํ•˜๋Š”๊ฒŒ ์ ์ ˆํ•˜๋‹ค๊ณ  ํŒ๋‹จ
  • TemperatureUnit ์˜ shared property ๋กœ ๋‹จ์œ„ ์ ‘๊ทผ ๊ฐ€๋Šฅ

ย 


Trouble Shooting

Page View Controller ์—์„œ ๊ณ„์†ํ•ด์„œ WeatherViewController ๋ฅผ ์ƒ์„ฑํ•˜๋Š” ๋ฌธ์ œ

  • ๋ฌธ์ œ์ƒํ™ฉ
    • PageViewController ์—์„œ swipe ์— ๋”ฐ๋ฅธ ์ด์ „/์ดํ›„ ํŽ˜์ด์ง€ ์š”์ฒญ์‹œ๋งˆ๋‹ค ์ƒˆ๋กญ๊ฒŒ view controller instance (WeatherViewController) ๋ฅผ ์ƒ์„ฑ
    • ๋ฉ”๋ชจ๋ฆฌ ๋ถ€ํ•˜๋กœ ์ธํ•ด ๊ฐ‘์ž๊ธฐ ๊บผ์ง€๋Š” ํ˜„์ƒ
  • ํ•ด๊ฒฐ ๋ฐฉ๋ฒ• : View Controller Caching - NSCache
    • NSCache

      • ์บ์‹ฑ์„ ๊ตฌํ˜„ํ•œ ํด๋ž˜์Šค๋กœ mutable dictionary type ์ฒ˜๋Ÿผ ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ๋‹ค - key ๋ฅผ ํ†ตํ•ด ์บ์‹ฑํ•œ ์ธ์Šคํ„ด์Šค ์ ‘๊ทผ ๊ฐ€๋Šฅ
      • ๋””๋ฐ”์ด์Šค๊ฐ€ ๋ฉ”๋ชจ๋ฆฌ ๋ถ€์กฑ์„ ๊ฒช์„ ๋•Œ ์ด ์บ์‹œ์— ์žˆ๋Š” ์ธ์Šคํ„ด์Šค์˜ ๋ฉ”๋ชจ๋ฆฌ ๊ณต๊ฐ„์„ ์ž๋™์œผ๋กœ ํšŒ์ˆ˜ํ•จ
      var cachedWeatherViewControllers = NSCache<NSNumber, WeatherViewController>()
    • view controller๋ฅผ ํ•œ๋ฒˆ ์ƒ์„ฑํ•œ ๋’ค, caching ํ•˜์—ฌ ์ดํ›„์˜ view controller ์˜ ์š”์ฒญ์ด ์žˆ์„ ๋•Œ, ์ด๋ฏธ ์ธ์Šคํ„ด์Šค๊ฐ€ ์žˆ๋‹ค๋ฉด ์ด๋ฅผ ๋ฐ˜ํ™˜ํ•˜๋Š” ๊ธฐ๋Šฅ์„ ๊ตฌํ˜„

    • PageViewController ์—์„œ ์บ์‹ฑํ•œ WeatherViewController ์ธ์Šคํ„ด์Šค ๊ด€๋ฆฌ

      • Key: page index (NSNumber type)
      • Value: WeatherViewController instance

ย 

๋งˆ์ง€๋ง‰์œผ๋กœ ๋ณธ ์œ„์น˜๋ฅผ ๋ฆฌ์ŠคํŠธ์—์„œ ์‚ญ์ œํ•  ๊ฒฝ์šฐ

  • ๋ฌธ์ œ์ƒํ™ฉ
    • ๋งˆ์ง€๋ง‰์œผ๋กœ ๋‚ ์”จ ์ •๋ณด๋ฅผ ๋ณธ ์œ„์น˜๋ฅผ ๋ฆฌ์ŠคํŠธ์—์„œ ์‚ญ์ œํ•  ๊ฒฝ์šฐ, ๋‹ค์‹œ page view ๋กœ ๋Œ์•„๊ฐ”์„ ๋•Œ page index ๊ฐ€ ๋ถˆ์ผ์น˜ ํ•˜๋Š” ํ˜„์ƒ
  • ํ•ด๊ฒฐ ๋ฐฉ๋ฒ•
    • ์‚ญ์ œ์‹œ ์œ„์น˜์ •๋ณด์˜ index ๊ฐ€ ๋งˆ์ง€๋ง‰์ผ ๊ฒฝ์šฐ, ๋งˆ์ง€๋ง‰์œผ๋กœ ๋ณธ list index ๋ฅผ 0์œผ๋กœ ์„ค์ •ํ•œ๋‹ค.

ย 

API ์—์„œ ๋ฐ›์•„์˜จ ๋ฐ์ดํ„ฐ - ์‹œ๊ฐ„๋ณ„ O ์ผ๋ณ„ X

  • ๋ฌธ์ œ์ƒํ™ฉ
    • open weather map ์˜ 5์ผ / 3์‹œ๊ฐ„ api ์—๋Š” 3์‹œ๊ฐ„ ๋งˆ๋‹ค์˜ ๋‚ ์”จ ์˜ˆ์ธก ์ •๋ณด๋Š” ์žˆ์ง€๋งŒ, ์ผ๋ณ„ ์˜ˆ์ธก ์ •๋ณด๋Š” ์—†๋‹ค.
    • ๋”ฐ๋ผ์„œ, 3์‹œ๊ฐ„ ๋งˆ๋‹ค์˜ ๋ฐ์ดํ„ฐ๋ฅผ ์ผ๋ณ„๋กœ ๋ถ„๋ฅ˜ํ•˜๊ณ  ์ด๋ฅผ ๋‹ค์‹œ ๊ฐ€๊ณตํ•˜๋Š” ์ ˆ์ฐจ๊ฐ€ ํ•„์š”
  • ํ•ด๊ฒฐ๋ฐฉ๋ฒ•
    • WeatherData โ†’ WeatherViewModel ์ด ๊ฐ€์ง„ ๊ฐ model ๋ณ„๋กœ ๊ฐ€๊ณตํ•˜๋Š” ์—ญํ• ์„ ๋‹ด๋‹นํ•˜๋Š” ํด๋ž˜์Šค ๊ตฌํ˜„
    • WeatherBuilder
    • 3์‹œ๊ฐ„ ๋ณ„ ๋ฐ์ดํ„ฐ๋ฅผ ์ผ์ž๋ณ„๋กœ ๋ชจ์•„์„œ, ์ผ์ž๋ณ„ ์ตœ๋Œ€/์ตœ์†Œ ์˜จ๋„๋ฅผ ๊ณ„์‚ฐ โ†’ HourlyWeatherItem ์œผ๋กœ ๋งŒ๋“ค๊ธฐ

ย 


๊ด€๋ จ ํ•™์Šต ๋‚ด์šฉ

URL Loading System

ํ‘œ์ค€ ์ธํ„ฐ๋„ท ํ”„๋กœํ† ์ฝœ์„ ์‚ฌ์šฉํ•˜์—ฌ ์„œ๋ฒ„์™€ url ๋กœ ์†Œํ†ตํ•˜๋Š” ๋ฐฉ์‹

URL๋กœ ํ™•์ธํ•  ์ˆ˜ ์žˆ๋Š” ๋ฆฌ์†Œ์Šค์— ์ ‘๊ทผํ•˜๋Š” ๋ฐฉ์‹์„ URL Loading System ์ด๋ผ ํ•œ๋‹ค.

resource loading ์€ asynchronously (๋น„๋™๊ธฐ) ๋กœ ์ˆ˜ํ–‰๋˜๋ฏ€๋กœ, ์œ ์ €์˜ ์ด๋ฒคํŠธ์— ์‘๋‹ตํ•  ์ˆ˜ ์žˆ๊ณ  ๋“ค์–ด์˜ค๋Š” ๋ฐ์ดํ„ฐ๋‚˜ ์—๋Ÿฌ๋ฅผ ์ฒ˜๋ฆฌํ•  ์ˆ˜ ์žˆ๋‹ค.

URLSession

Url ๋กœ request ๋ฅผ ๋ณด๋‚ด๊ฑฐ๋‚˜ ๋ฐ›๋Š” ์ผ์„ ๋‹ด๋‹นํ•˜๋Š” ๊ฐ์ฒด

  • ์„ค์ • : URLSessionConfiguration
    • default
    • ephemeral
    • background
  • URLSession instance ๋Š” URLSessionTask ์ธ์Šคํ„ด์Šค๋ฅผ ํ•œ๊ฐœ ์ด์ƒ ์ƒ์„ฑํ•˜์—ฌ ์‚ฌ์šฉํ•œ๋‹ค.
    • GET request ํ†ตํ•ด ๋ฐ์ดํ„ฐ๋ฅผ ๋ฐ›์•„์˜ค๋Š” ์ผ : URLSessionDataTask
    • POST / PUT request ํ†ตํ•ด ํŒŒ์ผ์„ ์—…๋กœ๋“œ ํ•˜๋Š” ์ผ : URLSessionUploadTask
    • ์›๊ฒฉ ์„œ๋ฒ„์—์„œ ํŒŒ์ผ์„ ๋‹ค์šด๋กœ๋“œ ํ•ด์˜ค๋Š” ์ผ : URLSessionDownloadTask
  • Task ์ƒํƒœ
    • suspend
    • resume
    • cancel
  • URLSession ์ด ๋ฐ์ดํ„ฐ๋ฅผ ๋ฐ˜ํ™˜ํ•˜๋Š” ๋‘๊ฐ€์ง€ ๋ฐฉ๋ฒ• (๋น„๋™๊ธฐ์ ์œผ๋กœ ์ˆ˜ํ–‰๋˜๋ฏ€๋กœ, ๋๋‚จ์„ ์•Œ๋ฆฌ๋Š” ๋ฐฉ๋ฒ•)
    1. completion handler - task ๊ฐ€ ๋๋‚  ๋•Œ ์‹คํ–‰๋จ
    2. delegate ์˜ method ํ˜ธ์ถœ

URLComponents

URL ์„ ๊ตฌ์„ฑํ•˜๋Š” ์š”์†Œ๋“ค์„ ๊ตฌ์กฐ์ฒด๋กœ ๋‚˜ํƒ€๋ƒ„

  • queryItem property : URLQueryItem (name -value ์ง์œผ๋กœ ๊ตฌ์„ฑ๋˜์–ด URL ์˜ query ๋ถ€๋ถ„์„ ๋‹ด๋‹น)
  • url property : ๊ตฌ์„ฑ์š”์†Œ๋“ค๋กœ๋ถ€ํ„ฐ ์ƒ์„ฑ๋œ URL

๋„คํŠธ์›Œํฌ

๋ฐ์ดํ„ฐ๋ฅผ URL ๋กœ๋ถ€ํ„ฐ ๊ฐ€์ ธ์˜ค๋ ค๋ฉด

  • ์–ด๋–ค ๋ฐ์ดํ„ฐ๋ฅผ ์ฃผ์„ธ์š” : request
  • ์‘๋‹ต : reponse

URLSession ํ™œ์šฉํ•œ data GET

func dataTask(with url: URL, 
completionHandler: @escaping (Data?, URLResponse?, Error?) -> Void) -> URLSessionDataTask
  • shared : singleton URLSession instance - ๊ฐ„๋‹จํ•œ request ์šฉ๋„
let task = URLSession.shared.dataTask(with: url) {
	// completion handler
}
  • Data : bytes or nil(error)

  • reponse: reponse ์˜ ๊ตฌํ˜„์ฒด. HTTPURLReponse ๋กœ ํƒ€์ž… ์บ์ŠคํŒ… ๊ฐ€๋Šฅ

  • error: error ๋ฐœ์ƒ์‹œ ๊ฐ’ ์žˆ์Œ. Nil ์ด๋ฉด ์„ฑ๊ณต

  • ๋„คํŠธ์›Œํฌ ์ข…๋ฃŒ์‹œ resume()

GCD (Grand Central Dispatch)

๋น„๋™๊ธฐ ์ˆ˜ํ–‰์„ ์›ํ•  ๋•Œ, main queue(main flow) ๋ง๊ณ  ๋‹ค๋ฅธ ์ˆ˜ํ–‰ queue ๋กœ ์ž‘์—…์„ ๋ณด๋‚ด๊ณ  ์‹ถ์„ ๋•Œ ์‚ฌ์šฉ

main queue : system ์ด ์ œ๊ณตํ•˜๋Š” queue๋กœ ๋ชจ๋“  UI code ๊ฐ€ ์ˆ˜ํ–‰๋˜์–ด์•ผ ํ•˜๋Š” ๊ณณ

var items: [Item]?

DispatchQueue.main.async {
    items = findItems(matching: "News")
}
// async ํด๋กœ์ ธ๊ฐ€ ์‹คํ–‰ ์™„๋ฃŒ ๋  ๋•Œ, item ์— ๊ฐ’์ด ํ• ๋‹น ๋œ๋‹ค.
// ๋‹จ์ˆœํžˆ ์ € ์„ ์–ธ๋ฌธ ์ง€๋‚˜๊ฐ”๋‹ค๊ณ  ์‹คํ–‰ ์™„๋ฃŒ๋œ๊ฒŒ ์•„๋‹˜

network request ๊ฐ™์€ ๋ฌด๊ฑฐ์šด ์ž‘์—…์„ ํ•  ๋•Œ๋Š” background queue ์—์„œ ์‹คํ–‰๋˜๋Š”๊ฒŒ ์•ฑ์˜ main ์—์„œ ์‹คํ–‰๋˜๋Š”๊ฒƒ๋ณด๋‹ค ๊ถŒ์žฅ๋จ. - UI ๋Š” ๋‹ค๋ฅธ ์ผ์„ ํ•˜๋Š” ๋™์•ˆ ๋ฌด๊ฑฐ์šด ์ž‘์—…๋“ค์„ ๋’ค์—์„œ ํ•  ์ˆ˜ ์žˆ์œผ๋ฏ€๋กœ

View Controller ์–‘๋ฐฉํ–ฅ ๋ฐ์ดํ„ฐ ์ „๋‹ฌ

  • view controller present ๊ด€๊ณ„
    • presenting view controller : ๋‚˜(view controller) ๋ฅผ ๋„์›Œ์ค€, ๋ณด์—ฌ์ค€ view controller
    • presented view controller : ๋‚ด๊ฐ€ (view controller) present ํ•˜๋Š”, ๋„์šด, ๋ณด์—ฌ์ค€ view controller

  • view controller ๊ฐ„ ๋ฐ์ดํ„ฐ ์ „๋‹ฌ
    • ๋ณด์—ฌ์ค„ view controller ์ธ์Šคํ„ด์Šค๋ฅผ ์ƒ์„ฑํ•˜์—ฌ present() method
    • ๋‹ค์‹œ ํ˜„์žฌ์˜ view controller ๋ฅผ ๋ณด์—ฌ์ค€ ์ด์ „์˜ view controller ๋กœ ๋ฐ์ดํ„ฐ๋ฅผ ๋ณด๋‚ด๋ ค๋ฉด? delegate ํ†ตํ•ด ์ „๋‹ฌ~์ „๋‹ฌ
      • ์‰ฝ๊ฒŒ ๋งํ•ด์„œ ์ƒˆ๋กœ์šด view controller ๋ฅผ instantiate ํ•ด์„œ present ํ•˜๊ธฐ ์ „์— ๋‚ด๊ฐ€ ๋„ ๋ณด๋ƒˆ๋‹ค๋Š” ์—ฐ๊ฒฐ์„ delegate ๋ฅผ ํ†ตํ•ด ํ•˜๋Š” ๊ฒƒ์ด๋‹ค
      • ๊ทธ ์—ฐ๊ฒฐ ๋ฐฉ์‹์€ protocol ์„ ํ†ตํ•ด์„œ ํ•ด๋‹น protocol์„ ๊ตฌํ˜„ํ•œ ํƒ€์ž…์ด๋ผ๋ฉด ๋ˆ„๊ตฌ๋‚˜ delegate ๋กœ ์†Œํ†ตํ•  ์ˆ˜ ์žˆ๋‹ค๋Š” ๋œป์ด๋‹ค.

UserDefaults

์•ฑ์˜ data ๋ฅผ ๋ฐฑ๊ทธ๋ผ์šด๋“œ ์ƒํƒœ ํ˜น์€ ์ข…๋ฃŒ์‹œ์—๋„ ์—†์–ด์ง€์ง€ ์•Š๊ณ  persistent(์˜๊ตฌ) ๋ณด์กดํ•  ์ˆ˜ ์žˆ๋„๋ก ํ•ด์ฃผ๋Š” user default database

  • key-value ํ˜•ํƒœ๋กœ ์ €์žฅ๋œ๋‹ค. Key ๋Š” String ๋งŒ ๊ฐ€๋Šฅ
  • ์ €์žฅ ๊ฐ€๋Šฅํ•œ Value ํ˜•ํƒœ : NSData, NSString, NSNumber, NSArray, NSDictionary
  • ํŠน์„ฑ
    • UserDefaults ํ†ตํ•ด ๊ฐ€์ ธ์˜จ ๋ฐ์ดํ„ฐ๋Š” immutable
    • plist extension ์œผ๋กœ ์ €์žฅ๋จ
    • app launch ๋  ๋•Œ, memory ์— ์˜ฌ๋ผ์˜จ๋‹ค.
  • UserDefaults ๋ณ€๊ฒฝ์— ์•Œ๋ฆผ์„ ๋ฐ›๊ณ  ์‹ถ๋‹ค๋ฉด
    • didChangeNotification ์— observer ๋ฅผ ๋“ฑ๋กํ•˜๋ฉด ๋œ๋‹ค.

About

๐Ÿ”† ๋‚ ์”จ OpenAPI๋ฅผ ์ด์šฉํ•ด์„œ Apple ๊ธฐ๋ณธ ๋‚ ์”จ์•ฑ ๋˜๋Š” Yahoo ๋‚ ์”จ์•ฑ๊ณผ ์œ ์‚ฌํ•œ ๊ธฐ๋Šฅ์…‹์„ ๊ตฌํ˜„ํ•˜๊ธฐ

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages