0

In the function below, everything works except when I try to get the variable partnerName from point A to point B, i.e. moving the variable in an out of a closure. I am a novice coder so I am hoping that someone can help me discover how to solve this particular issue and point me to a place where I might be able to learn the basics of how to share variables between different parts of a function.

func getAllConversations(handler: @escaping (_ conversationsArray: [Conversation]) -> ()) {
        var partner = [""]
        var title: String = ""
        var partnerName = ""

        var conversationsArray = [Conversation]()
        REF_CONVERSATION.observeSingleEvent(of: .value) { (conversationSnapshot) in
            guard let conversationSnapshot = conversationSnapshot.children.allObjects as? [DataSnapshot] else { return}
            for conversation in conversationSnapshot {
                let memberArray = conversation.childSnapshot(forPath: "members").value as! [String]
                partner = memberArray.filter {$0 != (Auth.auth().currentUser?.uid)!}
                if memberArray.contains((Auth.auth().currentUser?.uid)!) {

                    let newPartner = (String(describing: partner))
                    title = newPartner.replacingOccurrences(of: "[\\[\\]\\^+<>\"]", with: "", options: .regularExpression, range: nil)

                        databaseRef.child("bodhi").child(title).observeSingleEvent(of: .value, with: { (snapshot) in

                            if let bodhiDict = snapshot.value as? [String: AnyObject]
                            {
                                //I realize that the variable is being wrongly redeclared here to make this run.
                                let partnerName = (bodhiDict["Name"] as! String)
                                    print ("partnerName returned from firebase: \(partnerName)")
                                // Point A: This prints "Sandy"
                            }
                        })

                    print ("partnerName: \(partnerName)")
                    // Point B: This prints nothing but if I add partnerName = "Sandy", then the function complete
                    title = partnerName
                    print ("new title: \(title)")
                    let conversation = Conversation(conversationTitle: title, key: conversation.key, conversationMembers: memberArray, conversationMemberCount: memberArray.count)
                    conversationsArray.append(conversation)
                }
            }
            handler(conversationsArray)
        }
    }

    func createGroup(withTitle title: String, andDescription description: String, forUserIds ids: [String], handler: @escaping (_ groupCreated: Bool) -> ()) {
        REF_GROUPS.childByAutoId().updateChildValues(["title": title, "description": description, "members": ids])
        //    need to add code here for slow internet: if successful connection true else error
        handler(true)
    }

    func getAllGroups(handler: @escaping (_ groupsArray: [Group]) -> ()) {
        var groupsArray = [Group]()
        REF_GROUPS.observeSingleEvent(of: .value) { (groupSnapshot) in
            guard let groupSnapshot = groupSnapshot.children.allObjects as? [DataSnapshot] else { return}
            for group in groupSnapshot {
                let memberArray = group.childSnapshot(forPath: "members").value as! [String]
                if memberArray.contains((Auth.auth().currentUser?.uid)!) {
                    let title = group.childSnapshot(forPath: "title").value as! String
                    let description = group.childSnapshot(forPath: "description").value as! String
                    let group = Group(title: title, description: description, key: group.key, members: memberArray, memberCount: memberArray.count)
                    groupsArray.append(group)
                }
            }
            handler(groupsArray)
        }
    }

}
JW Hall
  • 17
  • 1
  • 4
  • Does [this question/answer](https://stackoverflow.com/q/39067196/4916627) help you? – André Kool May 07 '18 at 20:25
  • After reading this, it seems that since I am taking a value from one node on the server and using that to define a request for a second node, I need to create a second completion handler within the existing handler to return the partner name variable to the outer completion handler. Let me know if I am on the write track because, for me, there are hours of work ahead. :) – JW Hall May 08 '18 at 17:58

1 Answers1

0

I recommend you read the documentation on closures:

Closures

https://developer.apple.com/library/content/documentation/Swift/Conceptual/Swift_Programming_Language/Closures.html

Closures: Capturing Values

https://developer.apple.com/library/content/documentation/Swift/Conceptual/Swift_Programming_Language/Closures.html#//apple_ref/doc/uid/TP40014097-CH11-ID103

R.B.
  • 422
  • 4
  • 10
  • After reading this, it seems that since I am taking a value from one node on the server and using that to define a request for a second node, I need to create a second completion handler within the existing handler to return the partner name variable to the outer completion handler. Let me know if I am on the write track because, for me, there are hours of work ahead. :) – JW Hall May 08 '18 at 00:52
  • I did at least get the variable into the completion handler: – JW Hall May 08 '18 at 00:54