Upgrade to Pro — share decks privately, control downloads, hide ads and more …

Meet AsyncSequence

Meet AsyncSequence

AsyncSequenceってなに?

Mobile Act ONLINE #5 で AsyncSequence の話をしました #MobileAct - usami-kの日記
https://usami-k.hatenablog.com/entry/2021/07/01/083448

Mobile Act ONLINE #5 〜 WWDC21 を振り返る 〜 - connpass
https://mobileact.connpass.com/event/216108/

USAMI Kosuke

June 30, 2021
Tweet

More Decks by USAMI Kosuke

Other Decks in Programming

Transcript

  1. BTZODBXBJUΛ࢖Θͳ͍৔߹ func fetchThumbnail(completion: (UIImage?, Error?) -> Void) { let task

    = URLSession.shared.dataTask(with: request) { ... if let error = error { completion(nil, error) } else { // ... data / response ͷॲཧ ... completion(thumbnail, nil) } } task.resume() } 4/16
  2. BTZODBXBJUΛ࢖͏৔߹ func fetchThumbnail() async throws -> UIImage { let (data,

    response) = try await URLSession.shared.data(for: request) // ... data / response ͷॲཧ ... return thumbnail } w await鱳Օॴ鱬鲭鲱鳒鳫鳁ɺॲཧ鱑ऴ鲔鱨鱤鲎෮ؼ 5/16
  3. 63-4FTTJPOͷϝιου // async / await ඇରԠ൛ func dataTask(with request: URLRequest,

    completionHandler: (Data?, URLResponse?, Error?) -> Void) -> URLSessionDataTask // async / await ରԠ൛ func data(for request: URLRequest) async throws -> (Data, URLResponse) 6/16
  4. 4FRVFODFΛΠςϨʔλͰݟΔ for quake in quakes { // ... quake ͷॲཧ

    ... } var iterator = quakes.makeIterator() while let quake = iterator.next() { // ... quake ͷॲཧ ... } 10/16
  5. "TZOD4FRVFODFΛΠςϨʔλͰݟΔ for await quake in quakes { // ... quake

    ͷॲཧ ... } var iterator = quakes.makeAsyncIterator() while let quake = await iterator.next() { // ... quake ͷॲཧ ... } 11/16
  6. "TZOD4FRVFODFΦϖϨʔλ let strings = quakes.map { (quake) -> String in

    // ... quake Λ String ʹม׵ ... } for await string in strings { // ... string ͷॲཧ ... } 12/16
  7. "TZOD4FRVFODFͷฒྻॲཧ let task1 = async { for await quake in

    quakes { // ... quake ͷॲཧ ... } } let task2 = async { for await string in strings { // ... string ͷॲཧ ... } } // task1.cancel() 13/16