我有一個商業位置的註冊頁面,並且比較緯度和經度與數據庫中現有的任何孩子是否匹配。此代碼有時可以工作,然後停止工作。當它停止工作時,我會按「創建賬戶操作」並且什麼都不會發生。我添加了一個打印(「註冊按鈕點擊Firebase問題」),所以我知道該按鈕實際上正在點燃。爲什麼此代碼有時可以工作,有時甚至不行?什麼是我的問題的解決方案?謝謝。Swift Firebase QueryOrdered
@IBAction func createAccountAction(_ sender: Any) {
let email = self.emailTextField.text!
let password = self.passwordTextField.text!
if firstName.text == "" {
CommonUtils.sharedUtils.showAlert(self, title: "Error", message: "Please Enter Your First Name.")
}
if lastName.text == "" {
CommonUtils.sharedUtils.showAlert(self, title: "Error", message: "Please Enter Your Last Name.")
}
if companyPosition.text == "" {
CommonUtils.sharedUtils.showAlert(self, title: "Error", message: "Please Enter Your Company Position.")
}
if businessName.text == "" {
CommonUtils.sharedUtils.showAlert(self, title: "Error", message: "Please Enter The Business Name.")
}
if businessStreet.text == "" {
CommonUtils.sharedUtils.showAlert(self, title: "Error", message: "Please Enter The Business's Street.")
}
if businessCity.text == "" {
CommonUtils.sharedUtils.showAlert(self, title: "Error", message: "Please Enter The City.")
}
if businessState.text == "" {
CommonUtils.sharedUtils.showAlert(self, title: "Error", message: "Please Enter The State.")
}
if businessZIP.text == "" {
CommonUtils.sharedUtils.showAlert(self, title: "Error", message: "Please Enter The ZIP.")
}
if businessPhone.text == "" {
CommonUtils.sharedUtils.showAlert(self, title: "Error", message: "Please Enter The Business's Phone.")
}
if businessWebsite.text == "" {
CommonUtils.sharedUtils.showAlert(self, title: "Error", message: "Please Enter The Business Website.")
}
if businessLatitude.text == "" {
CommonUtils.sharedUtils.showAlert(self, title: "Error", message: "Please Enter The Business Latitude.")
}
if businessLongitude.text == "" {
CommonUtils.sharedUtils.showAlert(self, title: "Error", message: "Please Enter The Business Longitude.")
}
print("Sign Up Button Tapped Firebase problem")
self.ref.child("Businesses").queryOrdered(byChild: "businessLatitude").queryEqual(toValue: self.businessLatitude.text!).observeSingleEvent(of: .value, with: { snapshot in
if (snapshot.value! is NSNull) {
print("Not Found - GOOD")
if email != "" && password != "" && self.firstName.text != "" && self.lastName.text != "" && self.companyPosition.text != "" && self.businessName.text != "" && self.businessStreet.text != "" && self.businessCity.text != "" && self.businessState.text != "" && self.businessZIP.text! != "" && self.businessPhone.text != "" && self.businessWebsite.text != "" && self.businessLatitude.text != "" && self.businessLongitude.text != "" {
// search for businessLatitude
CommonUtils.sharedUtils.showProgress(self.view, label: "Registering...")
FIRAuth.auth()?.createUser(withEmail: email, password: password, completion: { (user, error) in
if error == nil {
FIREmailPasswordAuthProvider.credential(withEmail: email, password: password)
self.ref.child("Businesses").child(user!.uid).setValue(["firstName":self.firstName.text!,"lastName":self.lastName.text!, "companyPosition":self.companyPosition.text!,"businessName":self.businessName.text!, "businessStreet":self.businessStreet.text!, "businessCity":self.businessCity.text!, "businessState":self.businessState.text!, "businessZIP":self.businessZIP.text!, "businessPhone":self.businessPhone.text!, "businessWebsite":self.businessWebsite.text!,"businessLatitude":self.businessLatitude.text!, "businessLongitude":self.businessLongitude.text!, "approvalStatus":self.isApproved, "email":email ])
CommonUtils.sharedUtils.hideProgress()
let photoViewController = self.storyboard?.instantiateViewController(withIdentifier: "BusinessProfile")
self.navigationController?.pushViewController(photoViewController!, animated: true)
} else {
DispatchQueue.main.async(execute: {() -> Void in
CommonUtils.sharedUtils.hideProgress()
CommonUtils.sharedUtils.showAlert(self, title: "Error", message: (error?.localizedDescription)!)
})
}
})
} else {
let alert = UIAlertController(title: "Error", message: "Enter email & password!", preferredStyle: .alert)
let action = UIAlertAction(title: "OK", style: .default, handler: nil)
alert.addAction(action)
}
} else {
print(snapshot.value!)
if let snapshots = snapshot.children.allObjects as? [FIRDataSnapshot] {
for snap in snapshots {
let thisLongitude = snap.childSnapshot(forPath: "businessLongitude").value as! String
if thisLongitude == self.businessLongitude.text! {
// handle repeated location
if snap.exists() == true {
DispatchQueue.main.async(execute: {() -> Void in
CommonUtils.sharedUtils.hideProgress()
CommonUtils.sharedUtils.showAlert(self, title: "Error", message: "This Place Already Is Registered")
})
}
}
}
}
}
})
}
更多信息
如果我設置這樣的代碼(下面的代碼)並運行它,它的工作原理但它確實的檢查,如果相同的「businessLatitude」存在的用戶註冊和AFTER用戶被保存到數據庫中。那麼,當我將它重新設置爲上述代碼運行時的代碼並執行檢查而無需將用戶註冊到數據庫中(正是我想要的)。爲什麼在我做觸發器後運行?笑
if email != "" && password != "" && self.firstName.text != "" && self.lastName.text != "" && self.companyPosition.text != "" && self.businessName.text != "" && self.businessStreet.text != "" && self.businessCity.text != "" && self.businessState.text != "" && self.businessZIP.text! != "" && self.businessPhone.text != "" && self.businessWebsite.text != "" && self.businessLatitude.text != "" && self.businessLongitude.text != "" {
// search for businessLatitude
CommonUtils.sharedUtils.showProgress(self.view, label: "Registering...")
FIRAuth.auth()?.createUser(withEmail: email, password: password, completion: { (user, error) in
if error == nil {
FIREmailPasswordAuthProvider.credential(withEmail: email, password: password)
self.ref.child("Businesses").child(user!.uid).setValue(["firstName":self.firstName.text!,"lastName":self.lastName.text!, "companyPosition":self.companyPosition.text!,"businessName":self.businessName.text!, "businessStreet":self.businessStreet.text!, "businessCity":self.businessCity.text!, "businessState":self.businessState.text!, "businessZIP":self.businessZIP.text!, "businessPhone":self.businessPhone.text!, "businessWebsite":self.businessWebsite.text!,"businessLatitude":self.businessLatitude.text!, "businessLongitude":self.businessLongitude.text!, "approvalStatus":self.isApproved, "email":email ])
CommonUtils.sharedUtils.hideProgress()
let photoViewController = self.storyboard?.instantiateViewController(withIdentifier: "BusinessProfile")
self.navigationController?.pushViewController(photoViewController!, animated: true)
} else {
DispatchQueue.main.async(execute: {() -> Void in
CommonUtils.sharedUtils.hideProgress()
CommonUtils.sharedUtils.showAlert(self, title: "Error", message: (error?.localizedDescription)!)
})
}
})
} else {
let alert = UIAlertController(title: "Error", message: "Enter email & password!", preferredStyle: .alert)
let action = UIAlertAction(title: "OK", style: .default, handler: nil)
alert.addAction(action)
}
self.ref.child("Businesses").queryOrdered(byChild: "businessLatitude").queryEqual(toValue: self.businessLatitude.text!).observeSingleEvent(of: .value, with: { snapshot in
if (snapshot.value! is NSNull) {
print("Not Found - GOOD")
} else {
print(snapshot.value!)
if let snapshots = snapshot.children.allObjects as? [FIRDataSnapshot] {
for snap in snapshots {
let thisLongitude = snap.childSnapshot(forPath: "businessLongitude").value as! String
if thisLongitude == self.businessLongitude.text! {
// handle repeated location
if snap.exists() == true {
DispatchQueue.main.async(execute: {() -> Void in
CommonUtils.sharedUtils.hideProgress()
CommonUtils.sharedUtils.showAlert(self, title: "Error", message: "This Place Already Is Registered")
})
}
}
}
}
}
})
}
即使更多信息
我只是改變了我的「讀取」 +「寫入」規則火力地堡,以「真」。我一直在閱讀這是一個安全風險?這是迄今爲止的工作,但我正在測試,看看它會像以前一樣停止工作。我會保持這個更新....
{
"rules": {
".read": "true",
".write": "true",
"Businesses": {
"$Businesses_id" : {
".indexOn": "businessLatitude"
}
}
}
}
圖片編譯器的設置斷點時
This is the compiler when the code won't work and I set a breakpoint at the top line
嘗試指定獲取錯誤的代碼的確切部分,使其更具可讀性。 – JoshKopen
您需要在發佈問題之前進行一些故障排除 - 這樣您可能會發現問題,或者至少告訴我們哪一行代碼無法正常工作或崩潰。只需在self.ref行添加一個斷點,運行應用程序,點擊按鈕,然後逐行瀏覽它,直到找到問題。此外,您可能想重新思考所有這些if語句,就好像有多個空字段一樣,它會顯示很多警報,這不是一個好的用戶體驗。 – Jay
我添加了更多信息,如果您需要更多信息,請讓我知道 –