Thuta Learning
ရှာဖွေရန်
AdvancedMobile Developmentintermediate

Networking (URLSession + async/await)

စိတ်လျှော့ပါ။ ဒီခန်းကို စာအုပ်လိုမဟုတ်ဘဲ စကားပြောသလိုပဲ၊ နားလည်လွယ်အောင် ရှင်းပါမယ်။

ဒီခန်းပြီးရင် ဘာတတ်သွားမလဲ

  • Networking (URLSession + async/await) ကို ကြောက်စရာမလိုအောင် နားလည်မယ်
  • ကိုယ်တိုင် Xcode/SwiftUI code ကို run ကြည့်တတ်မယ်
  • Real project ထဲမှာ ဒီ concept ကို ချက်ချင်း အသုံးချတတ်မယ်

ခဏလေး ဒီလိုပဲ စဉ်းစားကြည့်

`URLSession` က iOS ရဲ့ built-in networking API ပါ (Android ရဲ့ Retrofit လို third-party library မလိုအပ်ပါ — Apple ကိုယ်တိုင် ပေးထားပါတယ်)။ Modern Swift (5.5+) မှာ `async`/`await` syntax နဲ့ တွဲသုံးရင် callback-based code (`completion handler`) ထက် ဖတ်ရလွယ်ပါတယ် — `URLSession.shared.data(from: url)` က `async` function ပါ, `try await` နဲ့ ခေါ်ရပါတယ်။ JSON response ကို `Codable` protocol conform လုပ်ထားတဲ့ struct အဖြစ် `JSONDecoder` ကနေ decode လုပ်လို့ရပါတယ်.

လက်တွေ့ scenario နဲ့ ချိတ်ကြည့်မယ်

`struct WeatherResponse: Codable { let city: String; let tempCelsius: Double }` ကို define လုပ်ပြီး, `func fetchWeather(city: String) async throws -> WeatherResponse { let url = URL(string: "https://api.example.com/weather?city=\(city)")!; let (data, _) = try await URLSession.shared.data(from: url); return try JSONDecoder().decode(WeatherResponse.self, from: data) }` လို့ ရေးရင် — `Task { weather = try await fetchWeather(city: "Yangon") }` ခေါ်ရင် API ကနေ weather data ကို fetch ယူပြီး UI ထဲ ပြပေးနိုင်ပါတယ်.

အတူတူ ကြည့်မယ်

swift
struct WeatherResponse: Codable {
    let city: String
    let tempCelsius: Double
}

func fetchWeather(city: String) async throws -> WeatherResponse {
    let url = URL(string: "https://api.example.com/weather?city=\(city)")!
    let (data, _) = try await URLSession.shared.data(from: url)
    return try JSONDecoder().decode(WeatherResponse.self, from: data)
}
You should see
$ let weather = try await fetchWeather(city: "Yangon")
// weather: WeatherResponse(city: "Yangon", tempCelsius: 32.0)

၅ မိနစ် စမ်းကြည့်

`fetchWeather` function ကို ကိုယ်တိုင် define လုပ်ကြည့်ပါ (public free weather API တစ်ခုကို ရွေးချယ်ပြီး) — `Task { }` ထဲက `try await` ခေါ်ပုံစံကို ဒီဇိုင်းရေးကြည့်ပါ။

သတိလေးတစ်ချက်

API key (secret) ကို app code ထဲ hardcode ရေးထားပြီး app bundle ထဲ ပါသွားရင်, decompile လုပ်ပြီး key ကို ထုတ်ယူနိုင်ပါတယ် — sensitive key ကို backend proxy server ကနေတစ်ဆင့် သုံးသင့်ပါတယ်။

ဒီနေရာမှာ လူအများမှားတတ်တယ်

  • `URLSession.shared.data(from:)` ကို `await` မပါဘဲ ခေါ်ကြိုးစားခြင်း — compile error ဖြစ်ပါလိမ့်မယ် (async function ဆိုတော့ await လိုအပ်ပါတယ်)
  • API call ရဲ့ error case (network down, invalid JSON) ကို handle မလုပ်ဘဲ success case ကိုသာ ရေးထားခြင်း — `try`/`catch` ဖြင့် error handling ထည့်ရပါမယ်

အခု ကိုယ်တိုင် စမ်းကြည့်

`fetchWeather` function ကို ကိုယ်တိုင် define လုပ်ကြည့်ပါ (public free weather API တစ်ခုကို ရွေးချယ်ပြီး) — `Task { }` ထဲက `try await` ခေါ်ပုံစံကို ဒီဇိုင်းရေးကြည့်ပါ။

You'll know it worked when: $ let weather = try await fetchWeather(city: "Yangon") // weather: WeatherResponse(city: "Yangon", tempCelsius: 32.0)

Networking (URLSession + async/await) | Thuta Learning