在iOS应用程序中使用Firebase登录时,我的表视图遇到了一个问题。该表从一个子提要加载内容。当我第一次登录时,表会正常加载,但是如果我注销并再次登录,表会重新加载所有数据,并将原始数据添加到表的内容中。我尝试在重新加载之前检查表是否为空,但没有效果。
关闭应用程序没有问题。只有当用户使用帐户视图控制器中的signet按钮退出应用程序并重新登录时,才会发生问题。我想弄清楚是什么原因使表向其中添加相同的数据,或者当用户注销时可能清空表,但其中任何一个都不能确定。
这是加载函数的代码。我将此代码放在viewWillAppear或viewDidLoad中,但没有区别。
if tableView.visibleCells.isEmpty {
loadTableData()
} else {
//do something
}
这是我的加载数据函数
func loadTableData() {
let url = "https://JSONurl.json"
let headers: HTTPHeaders = [
"X-API-Key": api.key,
"Accept": "application/json"
]
Alamofire.request(url, headers: headers).validate().responseJSON { response in
switch response.result {
case .success:
if let value = response.result.value {
let json = JSON(value)
self.parseJSON(json: json)
self.tableView.reloadData()
}
case .failure(let error):
let alert = UIAlertController(title: "title", message: "message", preferredStyle: UIAlertControllerStyle.alert)
let okAction = UIAlertAction(title: "title", style: UIAlertActionStyle.default, handler: nil)
alert.addAction(okAction)
self.present(alert, animated: true, completion: nil)
print(error)
}
}
}
在我的签名视图控制器中,我使用以下代码对用户进行身份验证
func authenticateUser() {
guard let userEmailAddress = userEmailAddressTextField.text, !userEmailAddress.isEmpty else {
// alert error message
return
}
guard let userPassword = userPasswordTextField.text, !userPassword.isEmpty else {
// alert error message
return
}
Auth.auth().signIn(withEmail: userEmailAddress, password: userPassword) {
(user, error) in
if let error = error {
self.commonFunctions.showAlertMessage(viewController: self, alertTitle: AppConstants.AlertMessages.firebaseAuthErrorTitle, messageToDisplay: error.localizedDescription)
return
}
if user != nil {
let mainPage = self.storyboard?.instantiateViewController(withIdentifier: "viewControllerWithTable") as! UITabBarController
self.present(mainPage, animated: true, completion: nil)
}
}
}
这是我在didFinishLaunchingWithOptions函数内的AppDelegate中的代码。这使得用户在关闭应用程序时不必再次登录。
if Auth.auth().currentUser != nil && Auth.auth().currentUser!.isEmailVerified {
let mainStoryBoard: UIStoryboard = UIStoryboard(name:"Main", bundle:nil)
let nextView: UITabBarController = mainStoryBoard.instantiateViewController(withIdentifier: "viewControllerWithTable") as! UITabBarController
self.window?.rootViewController = nextView
}
我的parseJSON如下:
func parseJSON(json: JSON) {
for item in json["results"][0]["members"].arrayValue {
item.id = (item["id"].stringValue)
// omitted extra data
let eachItem = ItemInit(id: item.id!)
items.group.append(eachItem)
}
sortData()
utils.hideActivityIndicator()
}
我的注销是在用户点击按钮时在IBAction中进行的。我将用户发送回WelcomeViewController,用户可以选择如何再次登录(电子邮件、社交媒体)。
do {
try Auth.auth().signOut()
let nextView = self.storyboard?.instantiateViewController(withIdentifier: "WelcomeViewController") as! WelcomeViewController
let appDelegate = UIApplication.shared.delegate
appDelegate?.window??.rootViewController = nextView
} catch{
// alert user
}
发布于 2018-08-28 05:45:50
在添加任何内容之前,请尝试清理dataSource。
func parseJSON(json: JSON) {
items.group.removeAll()
for item in json["results"][0]["members"].arrayValue {
item.id = (item["id"].stringValue)
// omitted extra data
let eachItem = ItemInit(id: item.id!)
items.group.append(eachItem)
}
sortData()
utils.hideActivityIndicator()
}
发布于 2018-08-28 10:04:28
好吧,我不知道为什么上面提到的其他选项都不起作用,但这就是我需要做的。我使用了NotificationCenter,我在注销视图控制器下面添加了这个
NotificationCenter.default.post(name: Notification.Name("clearTable"), object: nil)
在带有表的视图控制器上,我在ViewDidLoad中添加了以下内容:
NotificationCenter.default.addObserver(self, selector: #selector(clearTable), name: Notification.Name("clearTable"), object: nil)
并增加了如下函数:
@objc func clearTable() {
items.group.removeAll()
}
我仍然不明白为什么在加载表之前放置items.group.removeAll()不起作用。看起来几乎是一样的。
https://stackoverflow.com/questions/52040530
复制相似问题