Slide 1

Slide 1 text

async/awaitͷੑೳΛ DartͱSwiftͱͷൺֱͰಡΈղ͘ iOSDC 2021 Tamappe @tamapppe

Slide 2

Slide 2 text

I N T R O D U C T I O N Tamappe ϞόΠϧΞϓϦΤϯδχΞ 2014೥-2019೥3݄·ͰϑϦʔϥϯεͱͯ͠׆ಈ 2019೥4݄ΑΓϨΞδϣϒʹϞόΠϧΞϓϦΤϯδχΞͱͯ͠ೖࣾ झຯ: ϒϩάͷߋ৽ɺσΟζχʔ८Γ(ίϩφʹΑΓࣗॗ) Qiita: https://qiita.com/tamappe Blog: https://tamappe.com Twitter: https://twitter.com/tamapppe 01 גࣜձࣾϨΞδϣϒ

Slide 3

Slide 3 text

ຊ೔ͷςʔϚ async/await SwiftͱDartͷൺֱ

Slide 4

Slide 4 text

͖͔͚ͬ… WWDC 2021Ͱasync/awaitൃද

Slide 5

Slide 5 text

͖͔͚ͬ… ɾfunctionͰ࢖͑Δ ɾError handling Ͱ΋࢖͑Δ ɾfor จ(Async sequences)Ͱ΋࢖͑Δͷʁ ɾgetterͰ΋࢖͑Δ΍Μʁʁ

Slide 6

Slide 6 text

ˎasync/await͸ͲͷݴޠͰ΋ૉ੖Β͍͠Ͱ͢ SwiftͰͷasync/await ΊͬͪΌੌ͍΍Μ ݁࿦...

Slide 7

Slide 7 text

Ajenda 1. async/await ൺֱ 2. SwiftͰͷasync/await 3. SwiftͰͷӨڹൣғ 4. DartͰͷasync/await 5. DartͰͷϝϦοτɾσϝϦοτ 6. ·ͱΊ

Slide 8

Slide 8 text

async/await ൺֱ 02 Swift Dart Function ○ ○ Error handling ○ ○ Property(getter) ○ × Parameter × ○ Async sequences ○ ×

Slide 9

Slide 9 text

async/await is … 03 1.Simple 2.Easy 3.Safe

Slide 10

Slide 10 text

04 SwiftͰͷasync/await

Slide 11

Slide 11 text

SwiftͰͷasync/await 05 Function WWDC 2021 Meet async/await in Swift

Slide 12

Slide 12 text

SwiftͰͷasync/await 06 Function func asyncFetchData() async throws -> Data { let url = URL(string: "https://jsonplaceholder.typicode.com/todos/1")! let request = URLRequest(url: url) let (data, response) = try await URLSession.shared.data(for: request, delegate: nil) guard (response as? HTTPURLResponse)?.statusCode == 200 else { throw NSError(domain: "error", code: -1, userInfo: nil) } return data }

Slide 13

Slide 13 text

SwiftͰͷasync/await 07 Property (getter) WWDC 2021 Meet async/await in Swift

Slide 14

Slide 14 text

SwiftͰͷasync/await 08 Property (getter) extension UIImage { var thumbnail: UIImage? { get async { let size = CGSize(width: 40, height: 40) return await self.byPreparingThumbnail(ofSize: size) } } }

Slide 15

Slide 15 text

SwiftͰͷasync/await 09 Async sequences (for loop) WWDC 2021 Meet async/await in Swift

Slide 16

Slide 16 text

SwiftͰͷasync/await 10 sync to async WWDC 2021 Meet async/await in Swift

Slide 17

Slide 17 text

SwiftͰͷasync/await 11 sync to async func doSomething() { async { let data = try await asyncFetchData() } }

Slide 18

Slide 18 text

12 SwiftͰͷӨڹൣғ

Slide 19

Slide 19 text

SwiftͰͷӨڹൣғ 13 1. ίʔϧόοΫ - ίʔϧόοΫ(ωετ)஍ࠈ͔Βͷղ์ - weakSelf͔Βͷղ์ 2. Thread - actorʹΑΓMainActorͰϝΠϯεϨουͰ࣮ߦ 3. CoreData - contextͰͷ΍ΓͱΓ

Slide 20

Slide 20 text

SwiftͰͷӨڹൣғ (ίʔϧόοΫ) 14 class SampleRequest: NSObject { func fetchSomethingA(id: Int, compeltion: (Int) -> Void) { } func fetchSomethingB(id: Int, compeltion: (Int) -> Void) { } func fetchSomethingC(id: Int, compeltion: (Int) -> Void) { } }

Slide 21

Slide 21 text

SwiftͰͷӨڹൣғ (ίʔϧόοΫ) 15 class SampleRequest: NSObject { // தུ func requestSomething() { fetchSomethingA(id: 1, compeltion: { [weak self] a in // ॲཧΛॻ͘ self?.fetchSomethingB(id: a, compeltion: { [weak self] b in // ॲཧΛॻ͘ self?.fetchSomethingC(id: b, compeltion: { c in // ॲཧΛॻ͘ }) }) }) } }

Slide 22

Slide 22 text

ίʔϧόοΫ is … 16 not Simple not Easy not Safe

Slide 23

Slide 23 text

SwiftͰͷӨڹൣғ (ίʔϧόοΫ) 17 class SampleRequest: NSObject { func fetchSomethingA(id: Int) async -> Int { return 1 } func fetchSomethingB(id: Int) async -> Int { return 2 } func fetchSomethingC(id: Int) async -> Int { return 3 } }

Slide 24

Slide 24 text

SwiftͰͷӨڹൣғ (ίʔϧόοΫ) 18 class SampleRequest: NSObject { // தུ func requestSomething() async { let a = await fetchSomethingA(id: 1) let b = await fetchSomethingB(id: a) let c = await fetchSomethingC(id: b) // ॲཧΛॻ͘ } }

Slide 25

Slide 25 text

SwiftͰͷӨڹൣғ (ίʔϧόοΫ) 19 1.Simple 2.Easy 3.Safe

Slide 26

Slide 26 text

SwiftͰͷӨڹൣғ (Thread) 20 εϨουૢ࡞͸Ͳ͏ͳΔ͔ - UITableView - UICollectionView - Viewͷߋ৽ॲཧ

Slide 27

Slide 27 text

SwiftͰͷӨڹൣғ (Thread) 21 class SampleViewController: UIViewController { @IBOutlet weak var collectionView: UICollectionView! func update() { // APIͰσʔλΛऔಘޙʹUIͷߋ৽ DispatchQueue.main.async { self.collectionView.reloadData() } } }

Slide 28

Slide 28 text

SwiftͰͷӨڹൣғ (Thread) 22 ɾactor ɾMainActor

Slide 29

Slide 29 text

SwiftͰͷӨڹൣғ (Thread) 23 class SampleRequest: NSObject { var number: Int = 0 // தུ func requestSomething() async { let a = await fetchSomethingA(id: 1) let b = await fetchSomethingB(id: a) let c = await fetchSomethingC(id: b) // ॲཧΛॻ͘ Task.detached(priority: nil, operation: { @MainActor in // @MainActor ͰϝΠϯεϨουͰߋ৽Ͱ͖Δʁ self.number = c }) } }

Slide 30

Slide 30 text

SwiftͰͷӨڹൣғ (CoreData) 24 synchronous functions (ಉظॲཧ) asynchronous functions (ඇಉظॲཧ) .performAndWait await perform .perform await perform(.enqueued)

Slide 31

Slide 31 text

SwiftͰͷӨڹൣғ (CoreData) 25 func executePerform() { let persistentContainer = NSPersistentContainer(name: "XXX") let context = persistentContainer.newBackgroundContext() context.perform { // ॲཧ context.perform { // ॲཧ context.perform { // ॲཧ } try? context.save() } } }

Slide 32

Slide 32 text

SwiftͰͷӨڹൣғ (CoreData) 26 func asyncExecutePerform() async { let persistentContainer = NSPersistentContainer(name: "XXX") let context = persistentContainer.newBackgroundContext() await context.perform { // ॲཧ } await context.perform { // ॲཧ try? context.save() } await context.perform { // ॲཧ } }

Slide 33

Slide 33 text

27 DartͰͷasync/await

Slide 34

Slide 34 text

DartͰͷasync/await 28 1. ؔ਺ʹasyncΛએݴ͢Δ 2. ݺͼग़࣌͠ʹawaitΛએݴ 3. ύϥϝʔλʔ͸Futureܕ ˎCombine.frameworkͷFutureͱ͍ۙ

Slide 35

Slide 35 text

DartͰͷasync/await 29 Example: synchronous functions (ಉظॲཧ) String createOrderMessage() { var order = fetchUserOrder(); return 'Your order is: $order'; } Future fetchUserOrder() => Future.delayed(const Duration(seconds: 2), () => 'Large Latte',); void main() { print('Fetching user order...'); print(createOrderMessage()); }

Slide 36

Slide 36 text

DartͰͷasync/await 30 Future createOrderMessage() async { var order = await fetchUserOrder(); return 'Your order is: $order'; } Future fetchUserOrder() => Future.delayed(const Duration(seconds: 2), () => 'Large Latte',); Future main() async { print('Fetching user order...'); print(await createOrderMessage()); } Example: asynchronous functions (ඇಉظॲཧ)

Slide 37

Slide 37 text

DartͰͷasync/await 31 https://dart.dev/codelabs/async-await

Slide 38

Slide 38 text

DartͰͷasync/await (then ݺͼग़͠) 32 Future hello() async { var order = await fetchHello(); return order; } Future fetchHello() => Future.delayed(const Duration(seconds: 2), () => 'Hello world',); main() async { hello() .then((value) { // thenͰܨ͛Δ print(value); }); }

Slide 39

Slide 39 text

DartͰͷasync/await (Error handling) 33 main() async { hello() .then((value) { // thenͰܨ͛Δ print(value); }) .catchError((error) { // catchError Ͱܨ͛Δ print(error); }); }

Slide 40

Slide 40 text

DartͰͷϝϦοτɾσϝϦοτ 34 ϝϦοτ ɾDartͷඪ४Ͱasync/await͕࢖͑Δ ɾFutureܕ͕͋Δ ɾiOS ͷ৚݅ʹറΓ͕ͳ͍

Slide 41

Slide 41 text

DartͰͷϝϦοτɾσϝϦοτ 35 σϝϦοτ ɾจ๏͕JavaϥΠΫͰݹ͍ॻ͖ํ ɾenum͕SwiftΑΓ΋ॊೈʹॻ͚ͳ͍

Slide 42

Slide 42 text

Dartͷenum (͓·͚) 36 Swift Dart Normal (Int Value) ○ ○ Iterating over Enumeration Cases ○ × Associated Values ○ × Implicitly Assigned Raw Values ○ × Recursive Enumerations ○ × https://docs.swift.org/swift-book/LanguageGuide/Enumerations.html

Slide 43

Slide 43 text

Dartͷenum (͓·͚) 37 // Value͸ int enum Type { First, Second, Third, Fourth } // Dartͷ֦ுؔ਺͸ on ͱϥϕϦϯά͕ඞཁ extension TypeExtension on Type { String parseString() { switch (this) { case Type.First: return "First"; case Type.Second: return "Second"; case Type.Third: return "Third"; case Type.Fourth: return "Fourth"; } } }

Slide 44

Slide 44 text

·ͱΊ 38 Swift Dart Function ○ ○ Error handling ○ ○ Property(getter) ○ × Parameter × ○ Async sequences ○ ×

Slide 45

Slide 45 text

Ҿ༻ 1 Asynchronous programming https://dart.dev/codelabs/async-await Futures and error handling https://dart.dev/guides/libraries/futures-error-handling WWDC 2021 Meet async/await in Swift https://developer.apple.com/videos/play/wwdc2021/10132 WWDC 2021 Meet AsyncSequence https://developer.apple.com/videos/play/wwdc2021/10058 WWDC 2021 Use async/await with URLSession https://developer.apple.com/videos/play/wwdc2021/10095 WWDC 2021 Bring CoreData concurrency to Swift and SwiftUI https://developer.apple.com/videos/play/wwdc2021/10017

Slide 46

Slide 46 text

Ҿ༻ 2 Async/Await and the Future of Combine https://benscheirman.com/2021/06/async-await-and-the-future-of-combine/ URLsessionΛ༻͍ͨHTTPϦΫΤετͷํ๏(Swift, Xcode) https://qiita.com/shungo_m/items/64564fd822a7558ac7b1 Core DataΛόοΫάϥ΢ϯυͰ࢖͏ https://www.2nd-walker.com/2020/10/09/swift-using-coredata-in-background/ {JSON} Placeholder https://jsonplaceholder.typicode.com/

Slide 47

Slide 47 text

Thank you!