Concurrency

Defining and Calling Asynchronous Functions

We use async keyword in place of before return arrow in function definition to make a function as a Asynchronour Function.

Just like throwing functions, and wa wanna a function is both asynchronous and throwing, we write async before throwing.

func listPhotos(inGallery name: String) async -> [String] {
    let result = // ... some asynchronous networking code ...
    return result
}

func listPhotos2(inGallery name: String) async throws -> [String] {
    let result = // ... some asynchronous networking code ...
    return result
}

And we use await keyword to describe we need to wait a return value whthin a function untill it returns.

let photoNames = await listPhotos(inGallery: "Summer Vacation")
let sortedNames = photoNames.sorted()
let name = sortedNames[0]
let photo = await downloadPhoto(named: name)
show(photo)

await will suspend the code that is excuting.

Calling Asynchronous Functions in Parallel

If we want to call a function in parallel, just like dispatch.async, we don’t want a function to suspend the code, but we need the async characteristic, we can use async keyword to decorate a value.

async let firstPhoto = downloadPhoto(named: photoNames[0])
async let secondPhoto = downloadPhoto(named: photoNames[1])
async let thirdPhoto = downloadPhoto(named: photoNames[2])

let photos = await [firstPhoto, secondPhoto, thirdPhoto]
show(photos)

The example above shows a circumstance just like using dispatch,group.

Tasks and Task Groups

There are interesting thing that is the task in swift.

We can incoporate Task and async there two characteristics to do a asynchronous task surprisingly easy.

We can handle a group of tasks even.

await withTaskGroup(of: Data.self) { taskGroup in
    let photoNames = await listPhotos(inGallery: "Summer Vacation")
    for name in photoNames {
        taskGroup.async { await downloadPhoto(named: name) }
    }
}
let newPhoto = // ... some photo data ...
let handle = Task {
    return await add(newPhoto, toGalleryNamed: "Spring Adventures")
}
let result = await handle.value

And we can control each task by change its status.

Actors

Actor is just like Class, we can define a new actor with properties and functions by using actor keyword

actor TemperatureLogger {
    let label: String
    var measurements: [Int]
    private(set) var max: Int

    init(label: String, measurement: Int) {
        self.label = label
        self.measurements = [measurement]
        self.max = measurement
    }
}

But actor is much safer than class in asynchronous function since it only agree one operation on its state at a time.

And for its private property, we must use await to access it, which is called as actor isolation.

Let’s think!

©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • Process for JVM to Run a Program JVM(Java Virtual Machine...
    Lyudmilalala阅读 228评论 0 0
  • Java is a multi threaded programming language. A multi-th...
    Hackjutsu阅读 731评论 0 1
  • 我是黑夜里大雨纷飞的人啊 1 “又到一年六月,有人笑有人哭,有人欢乐有人忧愁,有人惊喜有人失落,有的觉得收获满满有...
    陌忘宇阅读 8,613评论 28 53
  • 人工智能是什么?什么是人工智能?人工智能是未来发展的必然趋势吗?以后人工智能技术真的能达到电影里机器人的智能水平吗...
    ZLLZ阅读 3,943评论 0 5
  • 首先介绍下自己的背景: 我11年左右入市到现在,也差不多有4年时间,看过一些关于股票投资的书籍,对于巴菲特等股神的...
    瞎投资阅读 5,793评论 3 8