Use Async Await with MVVM and URLSession (JSON API Calls ) | Swift Concurrency Hindi 2024 | Xcode 15

preview_player
Показать описание
In this video, you will learn about Swift concurrency feature, Async Await, MainActor, Task and explore how it can be seamlessly integrated with the MVVM architecture and URLSession. Join us as we unlock the full potential of Swift Concurrency and learn how to write efficient, responsive, and scalable code. Stay ahead of the curve and elevate your Swift development skills in 2024!

📕 Github Project:

📹 Youtube Videos:

📱 My Applications:
G1 Driving Test - Ontario 2023
My Music - Play & Share 
BMI Weight Loss Calculator 

🙋‍♂️ Say Hi on Social Media:

If you enjoyed this project and found it useful, please share and recommend it so others can find it 💚💚💚💚💚💚 !!!!

### Enjoy!!! 😀
Рекомендации по теме
Комментарии
Автор

Super Awesome Explaination Yogesh Sir.. 👌🙌👏

gratifirnr
Автор

Very detailed explanation...!! Thanks for sharing...

parthbarot-ncmr
Автор

Thank you so much Yogesh sir I really want to understand the concept of async await. Thanks again

navikaushal
Автор

Nice explanation. Please start swiftui tutorials.

shubhamkshatriya
Автор

Thanks bro, I appreciate your way of explaining and the knowledge you share.

khanshahbaz
Автор

Bro could you please create a video about advanced debugging techniques.

AmitBiswastunebox
Автор

Hello Yosgesh, can you please make a video on more than 2 API calls on any screen using
async/await

Mohsinqureshi
Автор

Try to save api response in core data and realme db or firebase db .

Crazyness.c
Автор

Sir..Can we have a session on video call, video streaming etc?

Sanilsysitco
Автор

Hi Sir,
In this video, View Controller is tightly coupled to viewModel. So basically view controller is depending on viewModel. How can me make it loosely coupled ?

ammarulhaq
Автор

Hello yogesh..do you give online classes to learn iOS development?i want to learn please reply

Yourstruly_suna
Автор

Question: When I run the request function in which thread does it run ? Does it run on main thread or uses a qos like .utility. Because when the we get response from data and setting it directly it is giving the error that it needs to be done in the main thread. So just wanted to know which thread it is used by default by the async functions. And also in non async function what is the thread. So like if i didnt had the async function and then I was setting the data in the fetchUsers I think it wont give me any issue as per my experience.

PS: Nice videos. Wanted to learn concurrency. Very informative.

leonardodavinci
Автор

understanding achi ha but Yogesh hamara do cash ab har Controller me linkhana pary ga so i think achi aproach ni ha ViewController level pay it should be in wrapper please reply.

ShakeelAhmed-jbnu
Автор

Hi Sir,
Does @MainActor blocks main thread ?

ammarulhaq
Автор

How can we send the multiple json object in single post request?

AmitGupta-xcgq
Автор

How we can pass params, headers and api type(get, post) using this method ?

sagarsharma
Автор

Hello Yogesh
Thank you for all content.
When we can see RXSwift with MVVM videos ?? Please plan this as well if possible, thank you

pradeepkas
Автор

import Foundation
import KRProgressHUD
import SwiftyJSON
import Alamofire

class ApiCall: NSObject, RequestInterceptor {
static let share: ApiCall = {
return ApiCall()
}()
let retryLimit = 3

func request(_ apiRouter: URLRequestConvertible, hasProgress: Bool, isShowError: Bool = true, completion: @escaping (JsonResult<JSON, AFError>) -> Void) {
if {
if isShowError {
Toast(text: "Unable to connect to the server, please check your network connection and try again").show()
}
.noCertificatesFound), nil))
return
}
if hasProgress {
KRProgressHUD.show()
}
AF.request(apiRouter, interceptor: self).validate().responseData { response in
if hasProgress {
KRProgressHUD.dismiss()
}
switch response.result {
case .success(let data):
do {
let json = try JSON(data: data)
print("- result ?? ""): \(json)")
completion(.success(json))
} catch {

}
case .failure(let error):

completion(.failure(error,
break
}
}
}

func requestFormData(_ router: URLRequestConvertible, image: ImageModel?, hasProgress: Bool, completion: @escaping (JsonResult<JSON, AFError>) -> Void) {
if {
Toast(text: "Unable to connect to the server, please check your network connection and try again").show()
.noCertificatesFound), nil))
return
}
if hasProgress {
KRProgressHUD.show()
}
var params: [String: Any]?
var url: String?
var headers: HTTPHeaders?
var method: HTTPMethod?

if let rt = router as? APIRouter {
params = rt.parameters
url = Api.me.baseUrl + rt.path
headers = rt.headers
method = rt.method
}
guard let pr = params, let u = url, var h = headers, let m = method else {
return
}
h["Content-type"] = "multipart/form-data"
h["Accept"] = "*/*"
print("requestFormData")
print("url: \(u)")
print("params: \(pr)")
print("method: \(m)")
print("headers: \(h)")
let block = { (multipart: MultipartFormData) in
{ (key, value) in
if let data = value.data(using: .utf8) {
multipart.append(data, withName: key)
}
}
if let image = image, let im = image.id), let data = 1024, height: 1024)) {
multipart.append(data, withName: "image", fileName: "\(Date.init().timeIntervalSince1970).png", mimeType: "image/png")
}
}
AF.upload(multipartFormData: block, to: u, method: m, headers: h)
.responseData { response in
if hasProgress {
KRProgressHUD.dismiss()
}
switch response.result {
case .success(let data):
do {
let json = try JSON(data: data)
print("- result: \(json)")
completion(.success(json))
} catch {

}
case .failure(let error):

completion(.failure(error,
break
}
}
}
}

extension URLEncoding {
public func queryParameters(_ parameters: [String: Any]) -> [(String, String)] {
var components: [(String, String)] = []

for key in parameters.keys.sorted(by: <) {
let value = parameters[key]!
components += queryComponents(fromKey: key, value: value)
}
return components
}
}

enum JsonResult<T, AFError> {
case success(T)
case failure(AFError, Int?)
}

Is it ok for me to implement api call like this?

hunghuynhmanh
Автор

Very detailed explanation...!! Thanks for sharing...

creations_saumya