- android - RelativeLayout 背景可绘制重叠内容
- android - 如何链接 cpufeatures lib 以获取 native android 库?
- java - OnItemClickListener 不起作用,但 OnLongItemClickListener 在自定义 ListView 中起作用
- java - Android 文件转字符串
我有一个营业地点的注册页面,我正在比较纬度和经度是否与数据库中任何现有的 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
最佳答案
我不确定问题中是否有足够的信息来实际具体回答,但消除一些变量可能会导致解决方案。因此,让我们尝试通过简化...来生成答案。
我所做的是在 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/
我有一个营业地点的注册页面,我正在比较纬度和经度是否与数据库中任何现有的 child 相匹配。此代码有时工作然后停止工作。当它停止工作时,我会按下“创建帐户操作”,但什么也不会发生。我添加了一个 pr
我现在正在尝试根据时间戳对通知页面上的数据从新到旧进行排序 - 当我运行它时,有时它的顺序是正确的,但有时它是随机且不正确的。如果我可以添加任何内容以确保它始终顺利运行,请告诉我,提前谢谢您:) 我的
我正在尝试获取仅由 adminUserUID 保存的媒体。问题是返回的数据不是 adminUserUID 发布的数据。 我在我的代码中没有看到任何错误,但我不明白为什么我得到了错误的数据。 我的数据结
"users":{ "steve_rodriquez":{ "ch1":{ "timestamp":2 }, "ch2
我需要按日期 (unix) 排列我的 firebase 数据。我认为 queryOrdered(byChild: "date") 可以解决问题。完成了 search并找到this这是有道理的: But
我想从我的 firebase 表中选择一个特定的名称(看图片)。打印的结果总是空的。所选名称应为“manni”。我的代码有什么问题?非常感谢。 @IBAction func BTSelect(_
在 Firebase 中搜索用户名(或任何与此相关的搜索)时有什么区别.queryOrdered().queryEqual(toValue: 和 child().observeSingleEvent(
使用 UISearchController 如果我想搜索用户最喜欢的书,书名是《哈利·波特》,我会执行以下操作来获取它的快照: func updateSearchResults(for searchC
root | @--reviews | | @--postABC // postId |
当运行下面的代码时,我得到一个空的响应,即使相应的数据在那里: self.ref?.child("play-data/calories/GC5g4RUmy0WTTL5w3jSobefa9Ft2").
我是一名优秀的程序员,十分优秀!