0

I'm an android developer learning to write my app for iOS using swift. I'm unsure how to run multiple firebase (firestore and realtime database) write tasks asynchronous and return a completion handler. In android I use the Tasks API and have it written as follows:

Task<Void> firestoreTask = firestoreDB.getDocument("users/someId").update("name", "some name");

Task<Void> realtimeTask = realtimeDB.child("users/someId/name/").setValue("some name");

Tasks.whenAll(firestoreTask, realtimeTask).addOnSuccessListener(new OnSuccessListener<Void>() {
            @Override
            public void onSuccess(Void aVoid) {
                // if both tasks are successful
            }
        })
        .addOnFailureListener(new OnFailureListener() {
            @Override
            public void onFailure(@NonNull Exception e) {
                // if at least one of the two task fails
            }
        });

I can't seem to figure out how to write such a task in swift. Can someone please point me in the right direction? Thank you.

Frank van Puffelen
  • 565,676
  • 79
  • 828
  • 807
TrackDave
  • 279
  • 2
  • 14
  • I don't know Android well, but I assume what you're trying to do is perform two different asynchronous requests and have a callback when they're finished? – Eilon Feb 17 '20 at 12:31
  • Correct. The tasks cannot run independently, because I can't have one task fail and the other succeed based on the database security rules. If one task fails then the other should as well. – TrackDave Feb 17 '20 at 13:14
  • A quick [search](https://www.google.com/search?q=swift+wait+for+multiple+callbacks) leads to [Synchronise Multiple Async Tasks With Dispatch Groups](https://dispatchswift.com/introduction-to-dispatch-group-a5cf9d61ff4f) and https://stackoverflow.com/questions/35906568/wait-until-swift-for-loop-with-asynchronous-network-requests-finishes-executing – Frank van Puffelen Feb 17 '20 at 14:13

2 Answers2

2

Following code will help you to solve this problem

import Foundation

// Create a dispatch group object and add a didset observer to catch error in operation

var dispatchGroup:DispatchGroup? = DispatchGroup(){
    didSet{
        if dispatchGroup == nil{
            // here you can do whetever after at least one of the two task fails
            print("error in FirestoreTask or realtimeTask")
        }
    }
}

// do your firestoreTask in this method
func dofirestoreTask (){
    // Implement your firestore task here //

    let taskResponse = true // Remove this variable with your firestore task response status
    if taskResponse == true{
        dispatchGroup?.leave()
    }else{
        dispatchGroup = nil
    }
}

func dorealtimeTask(){
    // Implement your realTimeTask here task here //
    let taskResponse = true // Remove this variable with your realTime task response status
    if taskResponse == true{
        dispatchGroup?.leave()
    }else{
        dispatchGroup = nil
    }
}

dispatchGroup?.enter()
dofirestoreTask()
dispatchGroup?.enter()
dorealtimeTask()

dispatchGroup?.notify(queue: .main) {
    // here you can do whetever after both tasks are successful
    print("Both Success")
}
neeraj joshi
  • 73
  • 1
  • 5
0

I think in iOS you have only closures.

mkowal87
  • 596
  • 4
  • 19