Meet AsyncSequence

RSS for tag

Discuss the WWDC21 session Meet AsyncSequence.

Posts under wwdc21-10058 tag

5 Posts

Post

Replies

Boosts

Views

Activity

Async @objc didPullToRefresh selector crashes app swift 5.5
I have a table to which I've added a refreshControl and when I pull down the table to refresh the data, I reset the array that feeds the table with data and then immediately request new data through an API call. Until now, I have used completion handlers and protocols to get the data into the table view but I want to move the logic to async/await because of the complexity needed by the network calls and the pyramid of nested closures. Populating the view in viewDidLoad works fine but with pullToRefresh selector I get an error: Thread 1: EXC_BAD_ACCESS (code=1, address=0xbcf917df8160) override func viewDidLoad() {     super.viewDidLoad()     setupView()     setupTableView()     setupTableRefreshControl()     Task {       await getBalances() //async network call       myTable.reloadData()     }   }    func setupTableRefreshControl() {     myTable.refreshControl = UIRefreshControl()     myTable.refreshControl?.addTarget(self, action: #selector(didPullToRefresh), for: .valueChanged)   } Code that crashes app:    @objc func didPullToRefresh() async {     balance.reset() // reset array to []     Task {       await getBalances() //async network call       myTable.reloadData()     }   }
1
0
969
Feb ’22
How do I use async/await with NotificationCenter?
In the Meet AsyncSequence talk, there's a very cool use case that's shown in one of the slides - the new notifications property on NotificationCenter is an async sequence and the code sample does something like: let notification = await center.notifications(named: ....).first { ... } This seems really intriguing and useful to me but I had a few questions about the details of how this works: What is the type of notification in this snippet? A Task? Where would I store this value? What context should this be invoked in, especially if I want to have a long-running notification filter running that will remain active for the lifetime of the app? Basically, I'm curious to see an example of the code surrounding this snippet.
3
0
7.5k
Nov ’21
Async Sequence
How to iterate over a custom object given below with async sequence enum FetchError: Error { case badImage case badRequest case invalidImageURL case noURL case failedToFetchImage } struct Photo: Codable { let albumId: Int let id: Int let title: String let urlPath: String let thumbnailUrl: String } What i have tried :- func fetchAsyncImage(request:URLRequest) async throws -> [UIImage] { let (data, response) = try await URLSession.shared.data(for: request) guard (response as? HTTPURLResponse)?.statusCode == 200 else { throw FetchError.badRequest } let photos = try JSONDecoder().decode([Photo].self, from: data) guard let imagePath = photos.first?.urlPath, let imageURL = URL.init(string: imagePath) else { throw FetchError.noURL } var imageArr:[UIImage] = [] // getting error in following 2 lines // Error: For-in loop requires '[Photo]' to conform to 'AsyncSequence' for await photo in photos { // Error: Type of expression is ambiguous without more context guard let imagePath = photo.urlPath, let imageURL = URL.init(string: imagePath) else { throw FetchError.noURL } do { let (imageData, imageResponse) = try await URLSession.shared.data(from: imageURL) guard (imageResponse as? HTTPURLResponse)?.statusCode == 200 else { throw FetchError.invalidImageURL } guard let image = UIImage(data: imageData) else { throw FetchError.badImage } imageArr.append(image) } catch { throw FetchError.failedToFetchImage } } return imageArr } I tried implementing AsyncSequenc and AsyncIteratorProtocol on Photo struct as follows:- struct Photo: Codable, AsyncSequence {   typealias Element = URL       let albumId: Int   let id: Int   let title: String   let urlPath: String   let thumbnailUrl: String       struct AsyncIterator: AsyncIteratorProtocol {     let urlPath: String     mutating func next() async throws -> URL? {       do {         guard let imageURL = URL.init(string: urlPath) else { throw FetchError.noURL }         return imageURL       } catch {         throw FetchError.invalidImageURL       }     }   }       func makeAsyncIterator() -> AsyncIterator {     AsyncIterator(urlPath: urlPath)   } }
3
0
1.8k
Jul ’21
Async @objc didPullToRefresh selector crashes app swift 5.5
I have a table to which I've added a refreshControl and when I pull down the table to refresh the data, I reset the array that feeds the table with data and then immediately request new data through an API call. Until now, I have used completion handlers and protocols to get the data into the table view but I want to move the logic to async/await because of the complexity needed by the network calls and the pyramid of nested closures. Populating the view in viewDidLoad works fine but with pullToRefresh selector I get an error: Thread 1: EXC_BAD_ACCESS (code=1, address=0xbcf917df8160) override func viewDidLoad() {     super.viewDidLoad()     setupView()     setupTableView()     setupTableRefreshControl()     Task {       await getBalances() //async network call       myTable.reloadData()     }   }    func setupTableRefreshControl() {     myTable.refreshControl = UIRefreshControl()     myTable.refreshControl?.addTarget(self, action: #selector(didPullToRefresh), for: .valueChanged)   } Code that crashes app:    @objc func didPullToRefresh() async {     balance.reset() // reset array to []     Task {       await getBalances() //async network call       myTable.reloadData()     }   }
Replies
1
Boosts
0
Views
969
Activity
Feb ’22
How do I use async/await with NotificationCenter?
In the Meet AsyncSequence talk, there's a very cool use case that's shown in one of the slides - the new notifications property on NotificationCenter is an async sequence and the code sample does something like: let notification = await center.notifications(named: ....).first { ... } This seems really intriguing and useful to me but I had a few questions about the details of how this works: What is the type of notification in this snippet? A Task? Where would I store this value? What context should this be invoked in, especially if I want to have a long-running notification filter running that will remain active for the lifetime of the app? Basically, I'm curious to see an example of the code surrounding this snippet.
Replies
3
Boosts
0
Views
7.5k
Activity
Nov ’21
Async await backword support issue
Can we use async await on under iOS 15?
Replies
1
Boosts
0
Views
1.5k
Activity
Nov ’21
Swift Package and AsyncSequence
Hi, I'd like to start playing around with the new Swift Concurrency framework in a Swift Package, but I cannot use AsyncSequence. AsyncSequence is not available despite I'm using swift-tools-version:5.5 in my package manifest.
Replies
0
Boosts
0
Views
614
Activity
Oct ’21
Async Sequence
How to iterate over a custom object given below with async sequence enum FetchError: Error { case badImage case badRequest case invalidImageURL case noURL case failedToFetchImage } struct Photo: Codable { let albumId: Int let id: Int let title: String let urlPath: String let thumbnailUrl: String } What i have tried :- func fetchAsyncImage(request:URLRequest) async throws -> [UIImage] { let (data, response) = try await URLSession.shared.data(for: request) guard (response as? HTTPURLResponse)?.statusCode == 200 else { throw FetchError.badRequest } let photos = try JSONDecoder().decode([Photo].self, from: data) guard let imagePath = photos.first?.urlPath, let imageURL = URL.init(string: imagePath) else { throw FetchError.noURL } var imageArr:[UIImage] = [] // getting error in following 2 lines // Error: For-in loop requires '[Photo]' to conform to 'AsyncSequence' for await photo in photos { // Error: Type of expression is ambiguous without more context guard let imagePath = photo.urlPath, let imageURL = URL.init(string: imagePath) else { throw FetchError.noURL } do { let (imageData, imageResponse) = try await URLSession.shared.data(from: imageURL) guard (imageResponse as? HTTPURLResponse)?.statusCode == 200 else { throw FetchError.invalidImageURL } guard let image = UIImage(data: imageData) else { throw FetchError.badImage } imageArr.append(image) } catch { throw FetchError.failedToFetchImage } } return imageArr } I tried implementing AsyncSequenc and AsyncIteratorProtocol on Photo struct as follows:- struct Photo: Codable, AsyncSequence {   typealias Element = URL       let albumId: Int   let id: Int   let title: String   let urlPath: String   let thumbnailUrl: String       struct AsyncIterator: AsyncIteratorProtocol {     let urlPath: String     mutating func next() async throws -> URL? {       do {         guard let imageURL = URL.init(string: urlPath) else { throw FetchError.noURL }         return imageURL       } catch {         throw FetchError.invalidImageURL       }     }   }       func makeAsyncIterator() -> AsyncIterator {     AsyncIterator(urlPath: urlPath)   } }
Replies
3
Boosts
0
Views
1.8k
Activity
Jul ’21