Skip to content

Instantly share code, notes, and snippets.

View SebastianBoldt's full-sized avatar
:octocat:

Sebastian Boldt SebastianBoldt

:octocat:
View GitHub Profile
struct ContentView: View {
var body: some View {
VStack {
Button("Async") {
Task {
await asyncFunction()
}
}
}
}
struct ContentView: View {
@State var taskTrigger: Bool = false
var body: some View {
Button("Execute Task", action: {
taskTrigger.toggle()
}).task(id: taskTrigger) {
do {
let newText = try await getText()
print(newText)
} catch {
@main
struct MainApp {
static func main() async {
await asyncFunction()
}
}
func getUserData(for id: Int) async throws -> Family {
let user = try await getUser(id: id)
let parents = try await getParents(of: user)
return createFamily(user, parent)
}
@SebastianBoldt
SebastianBoldt / AsyncAwait.swift
Last active February 3, 2022 08:36
AsyncAwait.swift
struct SomeView {
var data: Data? {
get async throws {
return await fetchData()
}
}
func fetchData() async throws -> Data? {
// ....
}
import UIKit
enum Section {
case section1
case section2
}
struct Item: Hashable {
let color: UIColor
}
extension ViewController {
func addItems(items: [Item], to section: Section) {
var snapshot = dataSource.snapshot()
snapshot.appendSections([section])
snapshot.appendItems(items, toSection: section)
dataSource.apply(snapshot)
}
}
class ViewController: UIViewController {
@IBOutlet var collectionview: UICollectionView!
lazy var dataSource = UICollectionViewDiffableDataSource<Section, Item>(collectionView: self.collectionview,
cellProvider: self.cellProvider)
lazy var cellProvider: (UICollectionView, IndexPath, Item) -> UICollectionViewCell? = { collectionView, indexPath, item in
let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "Cell", for: indexPath)
cell.backgroundColor = item.color
return cell
}
class ViewController: UIViewController {
@IBOutlet var collectionview: UICollectionView!
override func viewDidLoad() {
super.viewDidLoad()
}
}
enum Section {
case section1
case section2
}
struct Item: Hashable {
let name: String
}