Is there any way to implement a taskGroup to run in parrarel inside another taskGroup? - swift

I am using Firebase to fetch some data from it with a Continuation. Before resuming the continuation I want to run a group of async tasks inside(fetch other data). This would let me achieve maximum concurrency.
I have tried two methods to do this, both does not work.
This is what I have tried first trying to use inside a continuation a taskGroup.(Code Below). The error is in comment.
First Option
try? await withThrowingTaskGroup(of: Void.self) { group in
for referencePosts in referencePostsDict.keys {
group.addTask {
return try! await self.fetchPost(reference: referencePosts)
}
}
func fetchPost(reference: String) async throws -> Void{
var db_ref = Database.database(url:FIREBASEURL.url)
.reference()
.child("posts")
.child(reference)
typealias postContinuation = CheckedContinuation<Void,Error>
return try await withCheckedThrowingContinuation{
(continuation : postContinuation) in
db_ref.observeSingleEvent(of: .value) { data in
if data.exists(){
var three = data.value as! [String : Any]
withThrowingTaskGroup(of: Void.self) { group in //async' call in a function that does not support concurrency
(three["picturePaths"] as! [String : String]).forEach { key, value in
if key != "O"{
group.addTask {
try? await self.fetchPictureDataL(picRef: key)
}
self.threePictures[key] = value
}
}
}
self.threePosts[reference] = three
continuation.resume(returning: ())
}else{
continuation.resume(returning: ())
}
}
}
}
func fetchPictureDataL(picRef : String) async throws ->Void{
var db =Database
.database(url:FIREBASEURL.url)
.reference()
.child("pictures").child(picRef)
typealias postContinuation = CheckedContinuation<Void,Error>
return try await withCheckedThrowingContinuation{
(continuation : postContinuation) in
db.observeSingleEvent(of: .value) { data in
self.threePicturesFetched[picRef] = three as! [String : Any]
continuation.resume(returning: ())
}
}
}
I commented the code where the compiler reports a problem.
Second Option
fetchCheckIn2 method is modified to return a [String]
This is the second method where I have tried to achieve the same result different, to mention the fetchCheckIn2 method is modified to return a [String].
What I want to achieve is that the fetchPictureDataL will run in parallel inside a Task or group, and will not run awaiting each other to finish.
//fetching all references
try! await withThrowingTaskGroup(of: [String].self) { firstGroup in
for referenceCheckIn in reference_checkInInt_dictionary.keys {
firstGroup.addTask {
return try await self.fetchCheckIn2(reference: referenceCheckIn)
}
}
for try await pictureArray in firstGroup {
if pictureArray.count != 0 {
for pic in pictureArray {
try! await self.fetchPictureDataL(picRef: pic)
}
}
}
}
Trying to achieve
I want to achieve parallelism even with fetchPictureDataL method?
If not clear why I am trying to do this, please read the use of case.
Use of this case:
I have social media app with posts. I have a list of paths in order to fetch the post. Each post contains an array of other paths which are pictures.
I want to fetch posts parrarel and also fetch the pictures in parrarel, in a taskGruop, so all this fetch of post+picture is awaited and can be displayed to User.
Thank you

Related

How to ignore async let throws error when save response in tuple?

i have a code like this:
Task {
async let configsReq = self.interactor.getConfigs()
async let optionsReq = self.interactor.getOptions()
async let updateStateReq = self.interactor.getAppUpdateState()
async let contactsReq = self.interactor.getContactOptions()
var config: Config?
var options: AvailableOptions?
var updateState: UpdateType?
var contacts: ContactOptions?
do {
config = try await configsReq
} catch {
config = nil
}
do {
options = try await optionsReq
} catch {
options = nil
}
do {
updateState = try await updateStateReq
} catch {
updateState = nil
}
do {
contacts = try await contactsReq
} catch {
contacts = nil
}
self.goToNextPage()
}
in this case it does not matter for me that the requests get correct response or throws error. i don't want to block user to get correct response.
And also I want to make sure that all my requests are answered (correct or error response) to take the user to the next page
how can i write these codes cleaner and better with new swift concurrency?
i tried like this (but i could not get match error to each related request):
Task {
async let configs = self.interactor.getConfigs()
async let options = self.interactor.getOptions()
async let updateState = self.interactor.getAppUpdateState()
async let contacts = self.interactor.getContactOptions()
do {
let array = try await [configs, options, updateState, contacts]
} catch {
print(error)
}
}
If I understand the question correctly, you want to:
“match error to each related request”, but that
you want to proceed regardless of success or failure, as it “does not matter for me that the requests get correct response or throws error”.
If that is the pattern you are looking for, I might suggest using Task result:
async let configsReq = Task { try await interactor.getConfigs() }
async let optionsReq = Task { try await interactor.getOptions() }
async let stateReq = Task { try await interactor.getAppUpdateState() }
async let contactsReq = Task { try await interactor.getContactOptions() }
let config = await configsReq.result
let options = await optionsReq.result
let state = await stateReq.result
let contacts = await contactsReq.result
goToNextPage(config: config, options: options, state: state, contacts: contacts)
Or, more concisely:
async let configs = Task { try await interactor.getConfigs() }
async let options = Task { try await interactor.getOptions() }
async let state = Task { try await interactor.getAppUpdateState() }
async let contacts = Task { try await interactor.getContactOptions() }
await goToNextPage(config: configs.result, options: options.result, state: state.result, contacts: contacts.result)
Where goToNextPage might be defined as:
func goToNextPage(
config: Result<Config, Error>,
options: Result<AvailableOptions, Error>,
state: Result<UpdateType, Error>,
contacts: Result<ContactOptions, Error>
) { … }
That way, goToNextPage can look at the .success or .failure for each, to retrieve either the value or error associated with each of the four requests.
Needless to say, you also could have four properties for these four requests, and then goToNextPage could refer to those, rather than taking them as parameters to the method. It’s functionally the same thing, but you have to decide either local vars that are passed to the next method or update properties that are accessed by the next method.
You asked:
… if we don't want to use Result anymore, how can do that?
Yes, we do not use Result very much, anymore, as that was historically a pattern for returning either value or error in traditional asynchronous patterns, and nowadays we try a series of tasks, catch thrown errors, but generally early exit once one of them fails.
But if you really want to capture the success and failure for each of the four concurrent requests, then Result encapsulates that quite well.
I would make a little helper that helps wrap the error into a Result:
extension Result {
init(asyncCatching block: () async throws -> Success) async where Failure == Error {
do {
self = .success(try await block())
} catch {
self = .failure(error)
}
}
}
In case of errors, you even get the Error object for each getXXX method, rather than just a nil. Of course, if you really just want a nil, you can write a helper that returns optionals instead.
// this is essentially like refactoring out the repeated parts of your first code
func asyncCatchWithNil<Result>(function: () async throws -> Result) async -> Result? {
do {
return try await function()
} catch {
return nil
}
}
Then you could do:
Task {
async let configs = Result(asyncCatching: self.interactor.getConfigs)
async let options = Result(asyncCatching: self.interactor.getOptions)
async let updateState = Result(asyncCatching: self.interactor.getAppUpdateState)
async let contacts = Result(asyncCatching: self.interactor.getContactOptions)
/* or
async let configs = asyncCatchWithNil(function: self.interactor.getConfigs)
async let options = asyncCatchWithNil(function: self.interactor.getOptions)
async let updateState = asyncCatchWithNil(function: self.interactor.getAppUpdateState)
async let contacts = asyncCatchWithNil(function: self.interactor.getContactOptions)
*/
let (configsResult, optionsResult, updateStateResult, contactsResult)
= await (configs, options, updateState, contacts)
// you can inspect each result here if you'd like
self.goToNextPage()
}
The idea here is that you get a type that can contain both the response and error at the point of async let, rather than catching the error later.

Publishing changes from background threads is not allowed; make sure to publish values from the main thread (via operators like receive(on:))

I am building an app with Swift and SwiftUI. In MainViewModel I have a function who call Api for fetching JSON from url and deserialize it. this is made under async/await protocol.
the problem is the next, I have received from xcode the next comment : "Publishing changes from background threads is not allowed; make sure to publish values from the main thread (via operators like receive(on:)) on model updates." in this part of de code :
func getCountries() async throws{
countries = try await MainViewModel.countriesApi.fetchCountries() ?? []
}
who calls this one:
func fetchCountries() async throws -> [Country]? {
guard let url = URL(string: CountryUrl.countriesJSON.rawValue ) else {
print("Invalid URL")
return nil
}
let urlRequest = URLRequest(url: url)
do {
let (json, _) = try await URLSession.shared.data(for: urlRequest)
if let decodedResponse = try? JSONDecoder().decode([Country].self, from: json) {
debugPrint("return decodeResponse")
return decodedResponse
}
} catch {
debugPrint("error data")
}
return nil
}
I would like to know if somebody knows how I can fix it
First fetch the data asynchronously and then assign the result to the property on the main thread
func getCountries() async throws{
let fetchedData = try await MainViewModel.countriesApi.fetchCountries()
await MainActor.run {
countries = fetchedData ?? []
}
}
Off topic perhaps but I would change fetchCountries() to return an empty array rather than nil on an error or even better to actually throw the errors since it is declared as throwing.
Something like
func fetchCountries() async throws -> [Country] {
guard let url = URL(string: CountryUrl.countriesJSON.rawValue ) else {
return [] // or throw custom error
}
let urlRequest = URLRequest(url: url)
let (json, _) = try await URLSession.shared.data(for: urlRequest)
return try JSONDecoder().decode([Country].self, from: json)
}
There are two ways to fix this. One, you can add the #MainActor attribute to your functions - this ensures they will run on the main thread. Docs: https://developer.apple.com/documentation/swift/mainactor. However, this could cause delays and freezing as the entire block will run on the main thread. You could also set the variables using DispatchQueue.main.async{} - see this article from Hacking With Swift. Examples here:
#MainActor func getCountries() async throws{
///Set above - this will prevent the error
///This can also cause a lag
countries = try await MainViewModel.countriesApi.fetchCountries() ?? []
}
Second option:
func getCountries() async throws{
DispatchQueue.main.async{
countries = try await MainViewModel.countriesApi.fetchCountries() ?? []
}
}

How to wait for a bunch of async calls to finish to return a result?

I understand the basic usage of async/await but I'm a bit confused of what I should do in this specific example. I have an async function called save(url: URL) which mimics a function that would take a local URL as its parameter, and asynchronously return a String which would be, say the new remote URL of this file:
struct FileSaver {
// In this example I'll simulate a network request
// with a random async time and return the original file URL
static func save(_ url: URL) async throws -> String {
try await Task.sleep(
seconds: Double(arc4random_uniform(10)) / 10
)
return url.absoluteString
}
}
extension Task where Success == Never, Failure == Never {
public static func sleep(
seconds: Double
) async throws {
return try await sleep(
nanoseconds: UInt64(seconds) * 1_000_000_000
)
}
}
Now say I have 4 local files, and I want to save these files in parallel, but only return when they are all done saving. I read the documentation but still I'm a bit confused if I should use an array or a TaskGroup.
I would like to do something like this:
// in FileSaver
static func save(
files: [URL]
) async throws -> [String] {
// how to call save(url) for each file in `files`
// in parallel and only return when every file is saved?
}
Thank you for your help
We use task group to perform the requests in parallel and then await the whole group.
The trick, though, is that they will not finish in the same order that we started them. So, if you want to preserve the order of the results, we can return every result as a tuple of the input (the URL) and the output (the string). We then collect the group result into a dictionary, and the map the results back to the original order:
static func save(files: [URL]) async throws -> [String] {
try await withThrowingTaskGroup(of: (URL, String).self) { group in
for file in files {
group.addTask { (file, try await save(file)) }
}
let dictionary = try await group.reduce(into: [:]) { $0[$1.0] = $1.1 }
return files.compactMap { dictionary[$0] }
}
}
There are other techniques to preserve the order of the results, but hopefully this illustrates the basic idea.
I think withThrowingTaskGroup is what you are looking for:
static func save(
files: [URL]
) async throws -> [String] {
try await withThrowingTaskGroup(of: String.self) { group in
for file in files {
group.addTask { try await save(file) }
}
var strings = [String]()
for try await string in group {
strings.append(string)
}
return strings
}
}

How to use Decodable on the results of a Firestore Query on each document

I have the following code, and I need to use Query so that I can programmatically build the query up before making the call to Firestore, but the document I get back apparently doesn't support Decodable. If I don't use Query, I cannot build up the where clauses programmatically however the documents I get back do support Decodable. How can I get the first case to allow Decodable to work?
public static func query<T: Codable>(queryFields: [String: Any]) async -> [T] {
let db = Firestore.firestore()
var ref: Query = db.collection("myDocuments")
for (key, value) in queryFields {
ref = ref.whereField(key, isEqualTo: value)
}
let snapshot = try? await ref.getDocuments()
if let snapshot = snapshot {
let results = snapshot.documents.compactMap { document in
try? document.data(as: T.self) // this does not compile
}
return results
} else {
return [T]()
}
}

Swift Async let with loop

I want to get data in parallel. I found an example to call API in parallel but I want to store async let variables with loop.
Async let example. However, this example doesn't use a loop.
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)
I want to do something like the following.
let items = photoNames.map({ photo in
async let item = downloadPhoto(named: photo)
return item
})
let photos = await items
show(photos)
You can use a task group. See Tasks and Task Groups section of the The Swift Programming Language: Concurrency (which would appear to be where you got your example).
One can use withTaskGroup(of:returning:body:) to create a task group to run tasks in parallel, but then collate all the results together at the end.
E.g. here is an example that creates child tasks that return a tuple of “name” and ”image”, and the group returns a combined dictionary of those name strings with their associated image values:
func downloadImages(names: [String]) async -> [String: UIImage] {
await withTaskGroup(
of: (String, UIImage).self,
returning: [String: UIImage].self
) { [self] group in
for name in names {
group.addTask { await (name, downloadPhoto(named: name)) }
}
var images: [String: UIImage] = [:]
for await result in group {
images[result.0] = result.1
}
return images
}
}
Or, more concisely:
func downloadImages(names: [String]) async -> [String: UIImage] {
await withTaskGroup(of: (String, UIImage).self) { [self] group in
for name in names {
group.addTask { await (name, downloadPhoto(named: name)) }
}
return await group.reduce(into: [:]) { $0[$1.0] = $1.1 }
}
}
They run in parallel:
But you can extract them from the dictionary of results:
let stooges = ["moe", "larry", "curly"]
let images = await downloadImages(names: stooges)
imageView1.image = images["moe"]
imageView2.image = images["larry"]
imageView3.image = images["curly"]
Or if you want an array sorted in the original order, just build an array from the dictionary:
func downloadImages(names: [String]) async -> [UIImage] {
await withTaskGroup(of: (String, UIImage).self) { [self] group in
for name in names {
group.addTask { await (name, downloadPhoto(named: name)) }
}
let dictionary = await group.reduce(into: [:]) { $0[$1.0] = $1.1 }
return names.compactMap { dictionary[$0] }
}
}
Rob's answer is good. You can use an Array instead of a Dictionary too, to preserve order.
let photos = await photoNames.map(downloadPhoto)
public extension Sequence where Element: Sendable {
func mapWithTaskGroup<Transformed: Sendable>(
priority: TaskPriority? = nil,
_ transform: #escaping #Sendable (Element) async throws -> Transformed
) async rethrows -> [Transformed] {
try await withThrowingTaskGroup(
of: EnumeratedSequence<[Transformed]>.Element.self
) { group in
for (offset, element) in enumerated() {
group.addTask(priority: priority) {
(offset, try await transform(element))
}
}
return try await group.reduce(
into: map { _ in nil } as [Transformed?]
) {
$0[$1.offset] = $1.element
} as! [Transformed]
}
}
}
If the order of result doesn't matter here, use a TaskGroup instead.