1

我有一個商業位置的註冊頁面,並且比較緯度和經度與數據庫中現有的任何孩子是否匹配。此代碼有時可以工作,然後停止工作。當它停止工作時,我會按「創建賬戶操作」並且什麼都不會發生。我添加了一個打印(「註冊按鈕點擊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

This is what the compiler looks like after I do the "Hack" to make it work. What is different is "Provider Data/API Key/ User Id"

+0

嘗試指定獲取錯誤的代碼的確切部分,使其更具可讀性。 – JoshKopen

+0

您需要在發佈問題之前進行一些故障排除 - 這樣您可能會發現問題,或者至少告訴我們哪一行代碼無法正常工作或崩潰。只需在self.ref行添加一個斷點,運行應用程序,點擊按鈕,然後逐行瀏覽它,直到找到問題。此外,您可能想重新思考所有這些if語句,就好像有多個空字段一樣,它會顯示很多警報,這不是一個好的用戶體驗。 – Jay

+0

我添加了更多信息,如果您需要更多信息,請讓我知道 –

回答

0

我不知道有足夠的信息的問題要真正回答具體的問題,但消除一些變量可能會導致解決方案。所以讓我們嘗試通過簡化生成一個答案......

我所做的就是創建一個火力地堡的節點,這樣的測試:

app_name 
    Businesses 
    bus_0 
     businessLatitude: "thing" 
    bus_1 
     businessLatitude: "test" 
    bus_2 
     businessLatitude: "stuff" 

然後我創建了一個全新的火力地堡項目和粘貼下面的代碼進入viewDidLoad函數。這段代碼是從你的問題中複製並粘貼的,消除了封閉中的大部分'東西'。

//self.ref是我的firebase的引用。

override func viewDidLoad() { 

    self.ref.child("Businesses").queryOrdered(byChild: "businessLatitude") 
           .queryEqual(toValue: "test") 
           .observeSingleEvent(of: .value, with: { snapshot in 
     if (snapshot.value! is NSNull) { 
      print("nothing found") 
     } else { 
      print("found it!") 
      print(snapshot) 
     } 
    }) 
} 

然後構建並運行應用程序。輸出結果如下:

found it! 
Snap (Businesses) { 
    "bus_1" =  { 
     businessLatitude = test; 
    }; 
} 

根據這個結果,代碼本身工作正常,所以必須有別的東西影響進程。也許另一種方法正在被調用,或者可能存在異步代碼問題。

我會建議做同樣的事情 - 創建一個新項目並使用上面貼出的代碼,看看它是否有效。如果是這樣的話,我會在你的項目中註釋掉很多代碼,並嘗試儘可能地運行它,試圖減少變量的數量。然後讓它工作,並將代碼添加回來,直到停止工作。

+0

謝謝我試一試,看看我能想出什麼 –

+0

如何在規則中保存當前的「businessLatitude」和「businessLongitude」?我的規則可以影響爲什麼我的查詢沒有被調用? –

+0

@LukasBimba'保存'是什麼意思?規則就是這樣 - 它們爲您的數據提供安全性,並且可以允許或不允許訪問特定的數據節點。它們不會影響您的查詢未被調用的原因 - 但是,它們可以防止讀取/寫入數據。 Firebase始終提供反饋 - 無論是作爲錯誤條件還是控制檯/ Xcode日誌中的響應。例如,如果我們採用規則並將讀取設置爲false,並嘗試使用應用程序進行閱讀,那麼* [Firebase/Database] [I-RDB038012]/Business上的監聽器失敗:permission_denied *被打印到控制檯。 – Jay