我是Swift新手。我有一个小型应用程序,可以检查电话号码在同一天是否显示超过1个,然后我的应用程序应该停止并出现错误
我尝试了几种方法,但在我看来,该代码跳过了当天检查电话号码的部分,然后转到下一步。
import UIKit
import Firebase
//import FirebaseAuth
//import FirebaseDatabase
var ref_login: DatabaseReference!
var refTransaction: DatabaseReference!
var refcustomerdetails: DatabaseReference!
var refcountcheckin: DatabaseReference!
var CustomerName = String()
class ViewController: UIViewController {
@IBOutlet weak var nameText: UITextField!
@IBOutlet weak var nameLabel: UILabel!
@IBOutlet weak var phoneText: UITextField!
@IBOutlet weak var errorText: UILabel!
override func viewDidLoad() {
super.viewDidLoad()
ref_login = Database.database().reference()
// Do any additional setup after loading the view, typically from a nib.
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
@IBAction func loginPressed(_ sender: Any) {
if self.phoneText.text == "" {
print("Phone is empty")
self.errorText.text = "Phone is empty."
return
}
if self.phoneText.text == "000000000" {
switchownerScreen()
}
else {
Auth.auth().signIn(withEmail: phoneText.text! + "@gmail.com", password: "fingerjoynails", completion: {(user, error) in
if error != nil {
print(error)
self.errorText.text = "Phone does not exist in system."
return
}
if error == nil {
print("self.phoneText.text!",self.phoneText.text!)
//Start checking duplicate checking in the same day
//Start Date to query
let dformatter = DateFormatter()
dformatter.dateFormat = "yyyy-MM-dd"
let dmyString = dformatter.string(from: Date())
let dyourDate = dformatter.date(from: dmyString)
dformatter.dateFormat = "yyyy-MM-dd HH:mm:ss"
let dmyStringafd = dformatter.string(from: dyourDate!)
print("dmyStringafd:------",dmyStringafd)
refcountcheckin = Database.database().reference().child("Transactions")
let query = refcountcheckin.queryOrdered(byChild: "CHECK_IN_DATE").queryStarting(atValue: dmyStringafd)
query.observe(.value, with: { (snapshot) in
print("snapshot.childrenCount:----",snapshot.childrenCount)
if snapshot.childrenCount>0{
for transactions in snapshot.children.allObjects as![DataSnapshot]{
let transactionObject = transactions.value as? [String: AnyObject]
let transactionPhone = transactionObject?["PHONE"]
let checkPhone = transactionPhone as! String?
print("self.phoneText.text!",self.phoneText.text!)
if checkPhone == self.phoneText.text! {
self.errorText.text = "You already checked in on today."
print("You already checked in on today.")
return
}
}
}
})
//End checking duplicate checking in the same day
let phone = self.phoneText.text!
//let name = self.nameText.text!
let formatter = DateFormatter()
// initially set the format based on your datepicker date / server String
formatter.dateFormat = "yyyy-MM-dd HH:mm:ss"
let myString = formatter.string(from: Date()) // string purpose I add here
// convert your string to date
let yourDate = formatter.date(from: myString)
//then again set the date format whhich type of output you need
formatter.dateFormat = "yyyy-MM-dd HH:mm:ss"
// again convert your date to string
let myStringafd = formatter.string(from: yourDate!)
refcustomerdetails = Database.database().reference().child("Users")
let customerdetailsquery = refcustomerdetails.queryOrdered(byChild: "PHONE").queryEqual(toValue: self.phoneText.text!)
customerdetailsquery.observe(.value, with: { (snapshot) in
if snapshot.childrenCount>0{
for transactions in snapshot.children.allObjects as![DataSnapshot]{
let customerdetailsObject = transactions.value as? [String: AnyObject]
let customerFirstName = customerdetailsObject?["FIRST_NAME"]
let customerLastName = customerdetailsObject?["LAST_NAME"]
let firstname = customerFirstName as! String?
let lastname = customerLastName as! String?
CustomerName = firstname! + " " + lastname!
}
}
})
ref_login?.child("Transactions").childByAutoId().setValue(["PHONE": phone, "NAME": CustomerName,"CHECK_IN_DATE": myStringafd, "GIFT_CARD_NUMBER": "N"])
print("Insert into transaction complete.")
self.switchScreen()
}
})
}
}
func switchScreen() {
let storyboard = UIStoryboard(name: "Main", bundle: nil)
let controller = storyboard.instantiateViewController(withIdentifier: "usersVC")
self.present(controller, animated: true, completion: nil)
}
func switchownerScreen() {
let storyboard = UIStoryboard(name: "Main", bundle: nil)
let controller = storyboard.instantiateViewController(withIdentifier: "ownerVC")
self.present(controller, animated: true, completion: nil)
}
func assignbackground(){
let background = UIImage(named: "login.jpg")
var imageView : UIImageView!
imageView = UIImageView(frame: view.bounds)
imageView.contentMode = UIViewContentMode.scaleAspectFill
imageView.clipsToBounds = true
imageView.image = background
imageView.center = view.center
view.addSubview(imageView)
self.view.sendSubview(toBack: imageView)
}
}
在日志上,我可以看到应用程序插入记录,然后检查现有条件。我的目标是,如果今天电话号码已插入交易表中,则我们不应允许最终用户再次插入电话号码。
日志文件:
2019-01-20 16:02:50.096558-0800 checkinuser[8558:381633] [BoringSSL] nw_protocol_boringssl_get_output_frames(1301) [C4.1:2][0x7ff55fe02870] get output frames failed, state 8196 2019-01-20 16:02:50.096717-0800 checkinuser[8558:381633] [BoringSSL] nw_protocol_boringssl_get_output_frames(1301) [C4.1:2][0x7ff55fe02870] get output frames failed, state 8196 self.phoneText.text! 4088584842 2019-01-20 16:02:50.097999-0800 checkinuser[8558:381633] TIC Read Status [4:0x0]: 1:57 2019-01-20 16:02:50.098397-0800 checkinuser[8558:381633] TIC Read Status [4:0x0]: 1:57 dmyStringafd:------ 2019-01-20 00:00:00 Insert into transaction complete. snapshot.childrenCount:---- 10 self.phoneText.text! 4088584842 You already checked in on today. 2019-01-20 16:02:50.260980-0800 checkinuser[8558:381633] 5.15.0 - [Firebase/Database][I-RDB034028] Using an unspecified index. Your data will be downloaded and filtered on the client. Consider adding ".indexOn": "CHECK_IN_DATE" at /Transactions to your security rules for better performance
答案 0 :(得分:0)
答案 1 :(得分:0)
这是因为您正在运行查询,这需要时间,并且您尝试通过在函数内部运行return
来阻止插入(如果有重复项)。这意味着,如果在方法的其余部分可以执行之前查询没有处理并到达return
语句,则return
语句将没有时间执行。就像其他答案提到的那样,使用完成处理程序可能是正确的选择,但是您也可以将重复检查后的所有逻辑移到else
附带的if checkPhone == self.phoneText.text!
语句中,或者将其移动到一个新函数,然后从那里调用该函数:
if checkPhone == self.phoneText.text! {
self.errorText.text = "You already checked in on today."
print("You already checked in on today.")
}else{
self.yourNewInsertPhoneNumberFucntion()
}