2017-03-20 6 views
2

반환 기능에서 Firebase 값을 사용하는 데 어려움을 겪고 있습니다. 이것은 나를위한 지속적인 문제로 보인다. 나는 내 문제의 기본 예를 썼다. 어떻게해야합니까?Swift Firebase 기능 Return

func getChartIndexValues(completion:@escaping (Double) ->()) { 

    //Firebase Initialization 
    var ref: FIRDatabaseReference! 
    ref = FIRDatabase.database().reference() 

    ref.child("general_room_index").observeSingleEvent(of: .value, with: {(snapshot) in 

     let snapDict = snapshot.value as? NSDictionary 
     var zero = snapDict?["0"] as! Double 

     completion(zero) 
     }) 

} 


    returnFunction() -> (Double) { 

     getChartIndexValues() { (zero) ->() in 

      let testValue = zero 
     } 


    return //THis is my problem 
} 

답변

1

귀하는 문제를 암시 해 왔지만 명시 적으로 언급하지 않았습니다. 거래는 비동기 함수의 결과를 함수 결과로 리턴 할 수 없다는 것입니다. 함수가 완료 될 때 실행되는 완료 핸들러를 전달해야하며 해당 코드는 결과에 액세스 할 수있는 코드입니다.

returnFunction() -> (Double) { 

    getChartIndexValues() { (zero) ->() in 

    //Your code to process the results belongs here 
    self.someInstanceVar = zero 
    someLabel.text = "Your result is \(zero)" 
    } 

} 
//You CAN'T put your code to process results here. 
+0

그렇다면 함수에서 값을 반환하기 위해 블록을 우연히 무시할 수 있습니까? 라벨에 할당해야합니까, 아니면 할당하고 싶습니다. – codechicksrock