Task is new ViewModifier in iOS15. SwiftUI
Tags: ios, swiftui, ios15, task
Published: 2021-08-13 21:24:00

Apple added a new ViewModifier [*task(priority:_:)*](https://developer.apple.com/documentation/swiftui/circle/task(priority:_:)) in iOS 15. This modifier can be added to any SwiftUI [View](https://developer.apple.com/documentation/swiftui/view). 

>  [*task(priority:_:)*](https://developer.apple.com/documentation/swiftui/circle/task(priority:_:)) avalible since iOS15+ macOS 12.0+ Mac Catalyst 15.0+ watchOS 8.0+ tvOS 15.0+

From official apple documentation:

Adds an asynchronous task to perform when this view appears.

```swift
func task(priority: TaskPriority = .userInitiated, _ action: @escaping () async -> Void) -> some View
```
A view that runs the specified action asynchronously when the view appears.

See details about parameters:

 1 ***Priority*** The task priority to use when creating the asynchronous task. The default priority is [userInitiated](https://developer.apple.com/documentation/swift/taskpriority/3851283-userinitiated).

All priorities list:
- [userInitiated](https://developer.apple.com/documentation/swift/taskpriority/3851283-userinitiated)
- [high](https://developer.apple.com/documentation/swift/taskpriority/3851275-high)
- [medium](https://developer.apple.com/documentation/swift/taskpriority/3862710-medium)
- [low](https://developer.apple.com/documentation/swift/taskpriority/3851279-low)
- [utility](https://developer.apple.com/documentation/swift/taskpriority/3851285-utility)
- [background](https://developer.apple.com/documentation/swift/taskpriority/3851272-background)

 2 ***Action*** A closure that SwiftUI calls as an asynchronous task when the view appears. SwiftUI automatically cancels the task if the view disappears before the action completes.

This modifier is a good point to start and to do some asynchronous job related to the view. You don't have to worry about canceling asynchronous task, SwiftUI handles it out of the box.  
SwiftUI cancels the task in case of the task doesn’t finish before SwiftUI removes the view or the view changes identity.


### Example: 

Show the list users of GitHub. Displays the login and avatar image for each user.

``List`` code: 

```swift
@State private var users: [User] = []

var body: some View {
    List(users) { user in
        HStack(spacing: 16) {
            AsyncImage(url: URL(string: user.avatarUrl)) { avatar in
                avatar
                    .resizable()
                    .aspectRatio(contentMode: .fit)
                    .cornerRadius(16)
            } placeholder: {
                ProgressView()
            }
            .frame(width: 50, height: 50)
            
            Text(user.login)
                .font(.title2)
        }.padding(.init(top: 4, leading: 0, bottom: 4, trailing: 0))
    }.task {
        self.users = await loadUsers()
    }
}
```

We are interested in function ``loadUsers()``. The loading of users starts when the ``List`` appears. 
First of all the function gets JSON of users from GitHub. After that, it decodes the JSON into objects and finally sets the result to the ``List``'s data source.

``loadUsers()`` function: 

```swift
func loadUsers() async -> [User] {
    guard let url = URL(string: "https://api.github.com/users") else { return [] }
    do {
        let (data, _) = try await URLSession.shared.data(from: url)
        return try JSONDecoder().decode([User].self, from: data)
    } catch {
        Swift.print(error)
        return []
    }
}
```

Result 
![UsersList](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/uwkgjl1j6wq0txgba04x.png)

``User``model: 

```swift
struct User: Codable, Identifiable {
    let id: Int
    let login: String
    let avatarUrl: String
    
    enum CodingKeys: String, CodingKey {
        case id
        case login
        case avatarUrl = "avatar_url"
    }
}
```
The model implements ``Codable`` protocol for decoding JSON. Enum  ``CodingKeys`` is used to translate from the snake case :snake: to the camel case :camel:. 

[Full source code](https://github.com/Toni77777/GithubUsersSample) 

Thanks for reading! See you soon.