Result of call to 'functionName()' is unused

旧城冷巷雨未停 提交于 2020-01-16 15:53:13

问题


I have an odd issue and I’m not sure what I am doing wrong.

I have the following function that I want called in viewDidLoad to load all documents in a collection that will be used in a tableview.

func functionName() -> [String] {

    var NameArray = [String]()

    db.collection("Names").getDocuments() { (querySnapshot, err) in
        if let err = err {
            print("Error getting documents: \(err)")
        } else {
            for document in querySnapshot!.documents {
                NameArray.append("\(document.documentID)")
            }
        }
    }
    print(NameArray)
    print(NameArray.count)

    return (NameArray)
}

The function has the warning result is being ignored. I don’t want to silence it as I need the value, it should be return an array with values and a non zero count.

I run the below code connected to a button and it returns the array and count as expected. I also would like the keep the variable NameArray as local.

@IBAction func fetchUsersButton(_ sender: UIButton) {
    var NameArray = [String]()

    db.collection("Names").getDocuments() { (querySnapshot, err) in
        if let err = err {
            print("Error getting documents: \(err)")
        } else {
            for document in querySnapshot!.documents {
                NameArray.append("\(document.documentID)")
            }
        }
        print(NameArray)
        print(NameArray.count)
    }
}

回答1:


Instead of returning an array you need to place it in a completion handler.

func functionName(_ completion: @escaping ([String]) -> Void) {

    var NameArray = [String]()

    db.collection("Names").getDocuments() { (querySnapshot, err) in
        if let err = err {
            print("Error getting documents: \(err)")
        } else {
            for document in querySnapshot!.documents {
                NameArray.append("\(document.documentID)")
            }
        }

        completion(NameArray)
    }        
}



回答2:


You can try something like this:

func functionName() -> [String] {

    var nameArray = [String]()

    db.collection("Names").getDocuments() { (querySnapshot, err) in
        if let err = err {
            print("Error getting documents: \(err)")
        } else {
            for document in querySnapshot!.documents {
                NameArray.append("\(document.documentID)")
            }
            print(nameArray)
            return nameArray
        }
    }
    print(nameArray)
    print(nameArray.count)

    return (nameArray)
}

If your code gets to the else statement its because there were no problems and you were able to fetch data; thats when you need to return the array after you loop through every document. Because that firebase function uses a completion handler, thats why if you put the print statements outside the completion, it gets executed automatically. Also you shouldn't be starting variables with a capital letter. In swift it's a good practice to use camelCase. Hope this helps.




回答3:


The reason that you aren't returning anything is because db.collection().getDocuments is an asynchronous function. What this means is that the function gets to "return" before the db.collection().getDocuments code is done executing.

"Return" assumes that the code will execute synchronously which means it will execute line by line in a predictable order. An asynchronous function is one in which we don't know when it will finish executing (which is always true of network code).

What if the network is down? What if it takes a long time to download? Since we can't know, we use a completion handler to "return" what we need once the function has completed. The other suggestions are great, below is another solution. As a note, it assumes that this function is part of class, and you want to assign the result to an outside variable.

class MyClass {

      var myNameArray = [String]()

      func functionName() {

          db.collection("Names").getDocuments() { (querySnapshot, err) in
              if let err = err {
                  print("Error getting documents: \(err)")
              } else {
                  for document in querySnapshot!.documents {
                      myNameArray.append("\(document.documentID)")
                  }
              }
          }
      }

}

Another small thing about naming conventions, variables should utilize camelCase, so nameArray is preferable to NameArray.



来源:https://stackoverflow.com/questions/58193483/result-of-call-to-functionname-is-unused

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!