0

The following function is suppose to fetch a list of blocked-users then fetch blocked-by-users merge the two lists and return it as a [String].

The output to the console is the following which tells me that the return blockedList is being called early - how can I force for the two observeSingleEvents functions are finished before passing the array back to the caller method?

1 4 2 3

func getBlockedUsersList() -> [String]{

    var blockedList = [String]()
    var blockedUsers = [String]()
    var blockedByUsers = [String]()
    print("1")

    DataService.run.getBlockedByUsersList { (success, blockedByUsersList) in
        print("2")
        if success {

            blockedByUsers = blockedByUsersList

            DataService.run.getBlockedUsersList(handler: { (success, blockedUsersList) in
                print("3")
                if success {

                    blockedUsers = blockedUsersList

                    blockedList = Array(Set(blockedUsers + blockedByUsers))

                }//end if-success

            })//getBlockedUsersList

        }//end if-success

    }//end getBlockedByUsersList

    print("4")
    return blockedList

}//end func

Function getBlockedByUsersList gets list from Firebase:

func getBlockedByUsersList(handler: @escaping (Bool, [String]) -> ()){

    guard let uid = Auth.auth().currentUser?.uid else { return }

    var blockedUIDs = [String]()

    REF_USERS.child(uid).child("blocked-by-users").observeSingleEvent(of: .value, with: { (snapshot) in

        for child in snapshot.children{

            let itemSnap = child as! DataSnapshot
            blockedUIDs.append(itemSnap.key)

        }//end for

        handler(true, blockedUIDs)


    }) { (error) in
        print("error: \(error.localizedDescription)")
    }

}//end func
Roggie
  • 1,157
  • 3
  • 16
  • 40
  • @rmaddy please help me understand - how is this a duplicate question to the one you've suggested? – Roggie May 09 '19 at 03:21
  • 1
    Why do you think it isn't a duplicate? The better question is, if you know how to properly code an async method for `getBlockedByUsersList`, then why not do the same for `getBlockedUsersList`? – rmaddy May 09 '19 at 03:24
  • apologies but im new to this - so I wouldn't say I know how to code an async method (hence the question). I have read your suggested duplicate question and I am struggling to understand the likeness. In my code im making a nest `observeSingleEvent` nested calls, which I understand are `async` by nature. So my question is simple - how can I ensure that the first nested handler is returned, merged with the second prior to passing it to the caller? – Roggie May 09 '19 at 04:18
  • 1
    You are already doing what you need to do in your `getBlockedByUsersList`. Use that exact same pattern in your `getBlockedUsersList` method (use a completion handler instead of a return value). The duplicate shows how to make that change. But again, you already have one method using the correct pattern. Just make the other one use the same pattern. – rmaddy May 09 '19 at 04:20
  • right that worked! - my initial intention was to try and make my code cleaner and simply just calling `getBlockedUsersList()` as parameter in another function to get the a merged `array`. I appreciate your help. – Roggie May 09 '19 at 04:30

0 Answers0