This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Generate docarhive file using xcodebuild to custom derived data path | |
xcodebuild docbuild \ | |
-scheme YOUR_TARGET_NAME \ | |
-derivedDataPath PATH_TO_SAVE_DERIVED_DATA_FOLDER \ | |
-destination 'platform=iOS Simulator,name=iPhone 13' | |
// Find docarchive file in the build derived data path | |
find PATH_TO_SAVE_DERIVED_DATA_FOLDER -type d -name '*.doccarchive' |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
func fetchAPI<D: Decodable>(url: URL) async throws -> D { | |
let task = Task { () -> D in | |
try await fetchAndDecode(url: url) | |
} | |
//... | |
} | |
func fetchAPIGroup<D: Decodable>(urls: [URL]) async throws -> [D] { | |
try await withThrowingTaskGroup(of: D.self) { (group) in | |
for url in urls { |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
func fetchAndDecode<D: Decodable>(url: URL) async throws -> D { | |
let data = try await URLSession.shared.data(with: url) | |
let decodedData = try JSONDecoder().decode(D.self, from: data) | |
return decodedData | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import Foundation | |
extension URLSession { | |
// 1 | |
func data(with url: URL) async throws -> Data { | |
// 2 | |
try await withCheckedThrowingContinuation { continuation in | |
// 3 | |
dataTask(with: url) { data, _, error in |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 1 | |
static func main() async { | |
do { | |
// 2 | |
let revengeOfSith: SWAPIResponse<Film> = try await fetchAPI(url: Film.url(id: "6")) | |
print("Resp: \(revengeOfSith.response)") | |
// 3 | |
let urlsToFetch = Array(revengeOfSith.response.characterURLs.prefix(upTo: 3)) | |
let revengeOfSithCharacters: [SWAPIResponse<People>] = try await fetchAPIGroup(urls: urlsToFetch) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 1 | |
func fetchAPIGroup<D: Decodable>(urls: [URL]) async throws -> [D] { | |
// 2 | |
try await withThrowingTaskGroup(of: D.self) { (group) in | |
// 3 | |
for url in urls { | |
group.async { | |
let data = try Data(contentsOf: url) | |
let decodedData = try JSONDecoder().decode(D.self, from: data) | |
return decodedData |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
static func main() { | |
detach { | |
do { | |
let ipifyResponse: IpifyResponse = try await fetchAPI(url: IpifyResponse.url) | |
//... | |
} catch { | |
print(error.localizedDescription) | |
} | |
} | |
RunLoop.main.run(until: Date.distantFuture) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 1 | |
static func main() async { | |
// 2 | |
do { | |
// 3 | |
let ipifyResponse: IpifyResponse = try await fetchAPI(url: IpifyResponse.url) | |
print("Resp: \(ipifyResponse)") | |
// 4 | |
let freeGeoIpResponse: FreeGeoIPResponse = try await fetchAPI(url: FreeGeoIPResponse.url(ipAddress: ipifyResponse.ip)) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 1 | |
func fetchAPI<D: Decodable>(url: URL) async throws -> D { | |
// 2 | |
let task = Task { () -> D in | |
// 3 | |
let data = try Data(contentsOf: url) | |
let decodedData = try JSONDecoder().decode(D.self, from: data) | |
return decodedData | |
} | |
// 4 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
extension Task where Failure == Never { | |
public init(priority: TaskPriority? = nil, operation: @escaping @Sendable () async -> Success) | |
public init(priority: TaskPriority? = nil, operation: @escaping @Sendable () async throws -> Success) | |
} |
NewerOlder