Skip to content

Instantly share code, notes, and snippets.

View alfianlosari's full-sized avatar
📱
Xcoding with Alfian | https://alfianlosari.com

Alfian Losari alfianlosari

📱
Xcoding with Alfian | https://alfianlosari.com
View GitHub Profile
@alfianlosari
alfianlosari / docc_static_hostling_cli_command
Created February 17, 2022 08:46
Docc command for generating static site hosting with Xcode > 13.3
// 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'
@alfianlosari
alfianlosari / FetchAPITask.swift
Created May 31, 2021 07:48
Update to fetch data with URL Session Async Function
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 {
@alfianlosari
alfianlosari / FetchAPITask.swift
Created May 31, 2021 07:47
Fetch and decode using URLSession async function
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
}
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
@alfianlosari
alfianlosari / main.swift
Created May 31, 2021 07:42
Async Main with GroupTask
// 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)
@alfianlosari
alfianlosari / FetchAPITask.swift
Created May 31, 2021 07:39
Fetch API GroupTask
// 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
@alfianlosari
alfianlosari / main.swift
Created May 31, 2021 07:37
Detach on sync main
static func main() {
detach {
do {
let ipifyResponse: IpifyResponse = try await fetchAPI(url: IpifyResponse.url)
//...
} catch {
print(error.localizedDescription)
}
}
RunLoop.main.run(until: Date.distantFuture)
@alfianlosari
alfianlosari / main.swift
Created May 31, 2021 07:35
Main Single Task Async
// 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))
@alfianlosari
alfianlosari / FetchAPITask.swift
Created May 31, 2021 07:31
Creating Single Async Task
// 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
@alfianlosari
alfianlosari / Task.swift
Created May 31, 2021 07:30
Public Task Interface
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)
}