2017-08-17 121 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) 
}