Skip to content
Norman Basham edited this page Feb 17, 2020 · 1 revision

URL

Source


URLs are extended to GET, POST, PUT, and DELETE. You can call these methods directly but using RestHelper will take care of the model encoding and decoding. The easiest way to make REST calls is to extend DataService as shown above.

Get an image from URL (call back on Main Thread).

url.getImage { image in
    if let image = image {
        imageView.image = image
    } else {
        imageView.image = UIImage(named: "ImageNotFound")
    }
}

Get a Codable model from URL.

url.getModel(type: Pet.self) { pet in
    print(pet)
}

Get N models from from a list of URLs in parallel, return when all model requests are completed.

let url1 = "http://localhost:3000/pets/100"
let url2 = "http://localhost:3000/pets/200"
let url3 = "http://localhost:3000/pets/300"
[url1, url2, url3].get { data in
    let pets: [Pet] = data.compactMap { $0.decode() }
    print(pets)
}
Clone this wiki locally