1

How can I access my array objects in another function, array objects from arr and from emails. I can so far only access my array objects inside of the else statement when I call auth.auth() function. I want to find out how I can do this.

let store = CNContactStore()
            
            store.requestAccess(for: .contacts) { (granted, err) in
                if let err = err{
                    print(err.localizedDescription)
                    return
                }
                if granted{
                    print("Access granted")
                    let keys = [CNContactGivenNameKey, CNContactFamilyNameKey, CNContactEmailAddressesKey]
                    let req = CNContactFetchRequest(keysToFetch: keys as [CNKeyDescriptor])
                    do {
                        
                        try store.enumerateContacts(with: req) { (contact, stop) in
                            print(contact.emailAddresses.first?.value as Any)
                            if let em = contact.emailAddresses.first?.value{
                                //print("cool")
                                Auth.auth().fetchProviders(forEmail: em as String, completion: {
                                    (providers, error) in
    
                                    if error != nil {
                                        print("wierd")
                                    }else{
                                        if providers == nil{
                                            print("No active account")
                                        }else{
                                            self.emails.append(em as String)
                                            self.arr.append(contact.givenName + " " + contact.familyName)
                                            print("Active Account")
                                            print(self.arr)
                                            print(self.emails)
                                        }
                                    }
                                })
                            
                            }else{
                                //print("wow")
                            }
                        }
                        
                    }catch let err{
                        print(err.localizedDescription)
                    }
                }else{
                    print("Access denied")
                }
            }
            print("Arr")
            print(self.arr)
            print(self.emails)
            print("Emails")

I want to find out how I can access my array elements in "arr" and in "emails" in another function, because my ultimate goal is to put out the array info in a tableView.

1 Answers1

1

Data is loaded from Firebase asynchronously. While you may store it in variables that can be accessed from anywhere, the timing matters.

In your code, when the print(self.arr) runs, the self.arr.append(...) hasn't run yet. You can most easily see this by placing breakpoints on these lines and running the code in a debugger, or by placing some logging code and checking its output.

For this reason, all code that needs data from the database needs to be inside the completion handler or be called from there.

For some more on this, and examples of solutions, see:

Frank van Puffelen
  • 565,676
  • 79
  • 828
  • 807