Post

Replies

Boosts

Views

Activity

How to safely maximize concurrent UI rendering
I'm using Swift 6 and tasks to concurrently process multiple PDF files for rendering, and it's working well. But currently I'm manually limiting the number of simultaneous tasks to 2 out of fear that the system might run many tasks concurrently without having enough RAM to do the PDF processing. Testing on a variety of devices, I've tried increasing the task limit and haven't seen any crashes, but I'm quite concerned about the possibility. Any given device might be using a lot of RAM at any moment, and any given PDF might strain resources more than the average PDF. Is there a recommended technique for handling this kind of scenario? Should I not worry about it and just go ahead and start a high number of tasks, trusting that the system won't run too many concurrently and therefore won't run out of RAM?
2
0
208
1w
Swift 5.5 Task { ... } weak self
When using Task { ... } in Swift 5.5, I'm unclear if retain cycles might be an issue or not. For example: class ViewController: UIViewController { private var rows = [String]() private var tableView: UITableView? func populateRows() { Task { rows = (try? await getRowsFromNetwork()) ?? [] tableView?.reloadData() } } } Will populateRows retain the view controller for the life of the task because it references rows and tableView? If the view controller goes out of scope, I'd like it to have the freedom to deallocate without populateRows retaining it. This syntax compiles, but I don't know if it makes sense: class ViewController: UIViewController { private var rows = [String]() private var tableView: UITableView? func populateRows() { Task { [weak self] in self?.rows = (try? await self?.getRowsFromNetwork()) ?? [] self?.tableView?.reloadData() } } } I don't mind if the task continues to run. (I'm not worried about canceling it.) I just want to understand if there is a way to avoid retaining the view controller in the task, and if that happens automatically or if something like [weak self] is needed.
1
0
6.8k
Oct ’21