gpt4 book ai didi

ios - Swift Firebase QueryOrdered

转载 作者:可可西里 更新时间:2023-11-01 02:03:29 25 4
gpt4 key购买 nike

我有一个营业地点的注册页面,我正在比较纬度和经度是否与数据库中任何现有的 child 相匹配。此代码有时工作然后停止工作。当它停止工作时,我会按下“创建帐户操作”,但什么也不会发生。我添加了一个 print("Sign Up Button Tapped Firebase Problem") 所以我知道该按钮实际上正在触发。为什么这段代码有时有效有时无效?我的问题的解决方案是什么?谢谢。

@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”。然后,当我将其设置回上面的代码时,上面的代码将运行并进行检查,而无需将用户注册到数据库中(这正是我想要的)。为什么它在我做触发器之后运行?哈哈

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")
})
}
}
}
}
}
})
}

更多信息

我刚刚将 Firebase 中的“读取”+“写入”规则更改为“True”。我一直在阅读这是一个安全风险?到目前为止这是有效的,但我正在测试它是否会像以前那样停止工作。我会保持更新....

{
"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"

最佳答案

我不确定问题中是否有足够的信息来实际具体回答,但消除一些变量可能会导致解决方案。因此,让我们尝试通过简化...来生成答案。

我所做的是在 Firebase 中创建一个节点来进行如下测试:

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

然后我创建了一个全新的 Firebase 项目并将以下代码粘贴到 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;
};
}

根据这个结果,代码本身可以正常工作,所以一定有其他因素影响了这个过程。可能正在调用另一个方法,或者可能存在异步代码问题。

我建议做同样的事情 - 创建一个新项目并使用上面发布的代码,然后看看它是否有效。如果是这样,我会在你的项目中注释掉很多代码,并尝试尽可能简单地运行它,试图减少变量的数量。然后让它工作并一次一点地添加该代码,直到它停止工作。

关于ios - Swift Firebase QueryOrdered,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44727308/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com