2017-08-17 120 views
0

我一直在試圖找到一個解決方案,但沒有運氣:/我想調用我的firebase數據字符串,並將它們用作UIAlertView中的「標題」和「消息」。我正在使用此UIAlertView作爲地理圍欄信息。如果我只是將UIAlertView設置爲輸入消息的基本窗口,但是我需要它調用他們爲其他用戶閱讀的消息,則地理圍欄工作正常。到目前爲止,這個設置只彈出「確定」按鈕,沒有別的東西進入一個區域。Swift Firebase UIAlertView與Firebase數據字符串

func locationManager(_ manager: CLLocationManager, didDetermineState state: CLRegionState, for region: CLRegion) { 
    showAlert(withTitle: name, message: message) 
    //showAlert(withTitle: "Enter \(region.identifier)", message: "Geofence Message") 
    print(state) 
    print("region :\(region.identifier)") 

} 

func locationManager(_ manager: CLLocationManager, didEnterRegion region: CLRegion) { 
    showAlert() 
    //showAlert(withTitle: "Enter \(region.identifier)", message: "Geofence Message") 
    print("DID ENTER REGION") 
} 

func locationManager(_ manager: CLLocationManager, didExitRegion region: CLRegion) { 
    //showAlert(withTitle: "Exit \(region.identifier)", message: "Message Exit") 
    //TODO: stop local sequence 
    print("DID EXIT REGION") 

} 

func showAlert(withTitle title: String?, message: String?) { 
    FIRDatabase.database().reference().child("Businesses").observeSingleEvent(of: .value, with: { snapshot in 
     if let dictionary = snapshot.value as? [String: AnyObject] { 
      self.name = dictionary["businessName"] as? String 
      self.message = dictionary["geofenceMessage"] as? String 
     } 
     let alert = UIAlertController(title: self.name, message: self.message, preferredStyle: .alert) 
     let action = UIAlertAction(title: "Ok", style: .cancel, handler: nil) 
     alert.addAction(action) 
     self.present(alert, animated: true, completion: nil) 
    }) 
} 

更多信息

// Populate Map With Firebase Businesses 
func loadPlaces(){ 
    if CLLocationManager.isMonitoringAvailable(for: CLCircularRegion.self) { 
    FIRDatabase.database().reference().child("Businesses").observe(.value, with: { snapshot in 
     self.locationData = snapshot.value as? NSDictionary 
     if let data = self.locationData{ 
      for (key,obj) in data{ 
       let value = obj as? NSDictionary 
       let locationValue = value as! [String: Any] 
       let lat = Double(locationValue["businessLatitude"] as! String) 
       let long = Double(locationValue["businessLongitude"] as! String) 
       let businessTitle = String(locationValue["businessName"] as! String) 
       let center = CLLocationCoordinate2D(latitude: lat!, longitude: long!) 

       let radius = CLLocationDistance(500.0) 
       let geoRegion = CLCircularRegion(center: center, radius: radius, identifier: businessTitle!) 
       self.geofences.append(geoRegion) 
       self.locationManager.startMonitoring(for: geoRegion) 
       let overlay = MKCircle(center: center, radius: radius) 
       self.mapView.add(overlay) 
       geoRegion.notifyOnEntry = true 
       geoRegion.notifyOnExit = true 

       let annotation = MKPointAnnotation() 
       annotation.coordinate = geoRegion.center 
       annotation.title = businessTitle 
       self.mapView.addAnnotation(annotation) 

       self.nameKeyDict[(value?.value(forKey: "businessName") as? String)!] = key as? String 
      } 
     } 
    }) 
    } else { 
     print("No Bueno") 
    } 
} 

火力地堡數據結構

FireBase Data Structure

回答

0

這裏是有興趣的人,很簡單的查詢,檢查,火力答案。而不是其他人在這裏發佈的瘋狂。請記住將您的「region.identifier」設置爲與「queryOrderded」相同的內容,以便Firebase提取正確的「用戶」數據。然後在Firebase調用中使用「.childAdded」而不是「.value」來檢查Firebase數據庫中的「用戶」,而不是其值。

func locationManager(_ manager: CLLocationManager, didEnterRegion region: CLRegion) { 
    FIRDatabase.database().reference().child("Businesses").queryOrdered(byChild: "businessName").queryEqual(toValue: region.identifier).observe(.childAdded, with: { snapshot in 
     if snapshot.exists() { 
      if let dictionary = snapshot.value as? [String:Any] { 
       self.name = dictionary["businessName"] as? String 
       self.message = dictionary["geofenceMessage"] as? String 
      } 
      let alert = UIAlertController(title: self.name, message: self.message, preferredStyle: .alert) 
      let action = UIAlertAction(title: "Ok", style: .cancel, handler: nil) 
      alert.addAction(action) 
      self.present(alert, animated: true, completion: nil) 
     } else { 
      print("Snap doesnt exist") 
     } 
    }) 
print("DID ENTER REGION") 
} 
+0

只因爲你不明白答案,不要讓他們「瘋狂」。這對你花時間去幫助你的人來說會很謙虛。 – matiastofteby

0

因爲你的nametitle變量之前創建的警報會出現此問題是SE噸。嘗試將名稱和標題的初始化更改爲主隊列。然後使用Firebase observeSingleEvent,它具有完成功能塊,並在完成內部創建警報,以確保從Firebase獲取的值完整。

+0

會給它一個鏡頭 –

+0

希望你的數據結構是正確的。嘗試打印這些名稱值以檢查您是否真的從Firebase獲得響應。 –

+0

我的數據結構是正確的,它一切正常。只是我的格式是關閉 –

0

這是因爲觀察者事件是異步調用的,並且在獲取值之前調用alertcontroller。

用一個函數來完成處理程序取回您的火力值,就像這樣:

func retrieveGeofenceMessage(with region: CLRegion, completionHandler: @escaping (Bool) -> Void){ 
FIRDatabase.database().reference().child("Businesses").child(region.identifier).observeSingleEvent(of: .value, with: { snapshot in 
    if let dictionary = snapshot.value as? [String: AnyObject] { 
     self.name = dictionary["businessName"] as? String 
     self.message = dictionary["geofenceMessage"] as? String 

     } 
     completionHandler(true) 
    }) 
} 
從那裏你叫你寫的函數

然後:當觀察事件已被執行

func locationManager(_ manager: CLLocationManager, didEnterRegion region: CLRegion) { 
    retrieveGeofenceMessage(with: region){ 
      if $0 { 
       // Create Alert controller here 
       let alert = UIAlertController(title: self.name, message: self.message, preferredStyle: .alert) 
       let action = UIAlertAction(title: "Ok", style: .cancel, handler: nil) 
       alert.addAction(action) 
       self.present(alert, animated: true, completion: nil) 

      } 

     } 

     print("DID ENTER REGION") 
} 

您現在得到通知並確保您不會使用任何尚未設置的變量。

+0

不知道我會放在哪裏你的「YourFirebaseClassObject」 –

+0

這是因爲我認爲你已經創建了一個類,你所有的firebase-調用都是由你創建的,因此你可以從這個類的外部調用這個函數。如果你在課堂內調用它,你只需要修改函數名稱或者「自我」。 – matiastofteby

+0

我只是使用FIRDatabase.database()來調用我的firebase調用,每次都引用 –

0

嘗試使用這種方式希望這將有助於

FIRDatabase.database().reference().child("Businesses").observeSingleEvent(of: .value, with: { snapshot in 
     if let dictionary = snapshot.value as? [String: AnyObject] { 
      self.name = dictionary["businessName"] as? String 
      self.message = dictionary["geofenceMessage"] as? String 
      self.alertPopup(name: self.name, message: self.message) 
     } 

    }) 

} 

func alertPopup (name: NSString, message: NSString){ 
    let alert = UIAlertController(title: name as String, message: message as String, preferredStyle: .alert) 
    let action = UIAlertAction(title: "Ok", style: .cancel, handler: nil) 
    alert.addAction(action) 
    self.present(alert, animated: true, completion: nil) 
}