在过去的几个小时里,有很多关于这个主题的谷歌搜索,并没有找到真正的解决方案。
我有一个应用程序,我希望用户数据显示在表格中。但是一旦我们登录数据没有显示在表视图中,我的控制台就会打印出0行返回。
这是UsersViewController:
import UIKit
import Firebase
class UsersViewController: UIViewController, UITableViewDelegate, UITableViewDataSource {
@IBOutlet weak var tableview: UITableView!
var user = [User]()
override func viewDidLoad() {
super.viewDidLoad()
retrieveUsers()
}
func retrieveUsers() {
let ref = FIRDatabase.database().reference()
ref.child("users").queryOrderedByKey().observeSingleEvent(of: .value, with: { snapshot in
guard let users = snapshot.value as? [String: AnyObject] else {return}
self.user.removeAll()
for (_, val) in users{
if let uid = val.value(forKey: "uid") as? String {
if uid != FIRAuth.auth()!.currentUser!.uid{
let userToShow = User()
if let fullName = val.value(forKey: "full name") as? String, let imagePath = val.value(forKey: "urlToImage") as? String {
userToShow.fullName = fullName
userToShow.imagePath = imagePath
userToShow.userID = uid
self.user.append(userToShow)
}
}
}
}
DispatchQueue.main.async {
self.tableview.reloadData()
}
})
ref.removeAllObservers()
}
func numberOfSections(in tableView: UITableView) -> Int {
return 1
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableview.dequeueReusableCell(withIdentifier: "USERCell", for: indexPath) as! USERCell
cell.nameLabel.text = self.user[indexPath.row].fullName
cell.userID = self.user[indexPath.row].userID
cell.userImage.downloadImage(from: self.user[indexPath.row].imagePath!)
return cell
}
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
print("\(user.count)")
return user.count
}
func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
let uid = FIRAuth.auth()!.currentUser!.uid
let ref = FIRDatabase.database().reference()
let key = ref.child("users").childByAutoId().key
var isFollower = false
ref.child("users").child(uid).child("following").queryOrderedByKey().observeSingleEvent(of: .value, with: { snapshot in
if let following = snapshot.value as? [String : AnyObject] {
for (ke, value) in following {
if value as! String == self.user[indexPath.row].userID {
isFollower = true
ref.child("users").child(uid).child("following/\(ke)").removeValue()
ref.child("users").child(self.user[indexPath.row].userID).child("followers/\(ke)").removeValue()
self.tableview.cellForRow(at: indexPath)?.accessoryType = .none
}
}
}
if !isFollower {
let following = ["following/\(key)" : self.user[indexPath.row].userID]
let followers = ["followers/\(key)" : uid]
ref.child("users").child(uid).updateChildValues(following)
ref.child("users").child(self.user[indexPath.row].userID).updateChildValues(followers)
self.tableview.cellForRow(at: indexPath)?.accessoryType = .checkmark
}
})
ref.removeAllObservers()
}
func checkFollowing(indexPath: IndexPath) {
let uid = FIRAuth.auth()!.currentUser!.uid
let ref = FIRDatabase.database().reference()
ref.child("users").child(uid).child("following").queryOrderedByKey().observeSingleEvent(of: .value, with: { snapshot in
if let following = snapshot.value as? [String : AnyObject] {
for (_, value) in following {
if value as! String == self.user[indexPath.row].userID {
self.tableview.cellForRow(at: indexPath)?.accessoryType = .checkmark
}
}
}
})
ref.removeAllObservers()
}
@IBAction func composePressed(_ sender: Any) {
}
@IBAction func menuPressed(_ sender: Any) {
}
}
extension UIImageView {
func downloadImage(from imgURL: String!){
let url = URLRequest(url: URL(string: imgURL)!)
let task = URLSession.shared.dataTask(with: url) {
(data, response, error) in
if error != nil {
print(error!)
return
}
DispatchQueue.main.async {
self.image = UIImage(data: data!)
}
}
task.resume()
}
}
我也有手机信息:
import UIKit
class USERCell: UITableViewCell {
@IBOutlet weak var userImage: UIImageView!
@IBOutlet weak var nameLabel: UILabel!
var userID: String!
}
用户对象:
import UIKit
class User: NSObject {
var userID: String!
var fullName: String!
var imagePath: String!
}
让我知道。顺便说一句,我已正确设置了我的身份,并将所有出口连接起来等等。
编辑:日志 - 看起来我的变量没有取任何值:
> > objc[14259]: Class PLBuildVersion is implemented in both
> > /Applications/Xcode.app/Contents/Developer/Platforms/iPhoneSimulator.platform/Developer/SDKs/iPhoneSimulator.sdk/System/Library/PrivateFrameworks/AssetsLibraryServices.framework/AssetsLibraryServices
> > (0x11a4d6998) and
> > /Applications/Xcode.app/Contents/Developer/Platforms/iPhoneSimulator.platform/Developer/SDKs/iPhoneSimulator.sdk/System/Library/PrivateFrameworks/PhotoLibraryServices.framework/PhotoLibraryServices
> > (0x11a255880). One of the two will be used. Which one is undefined.
> > 2017-03-12 21:51:10.301 avenir-design-app[14259:575161] Firebase automatic screen reporting is enabled. Call +[FIRAnalytics
> > setScreenName:setScreenClass:] to set the screen name or override the
> > default screen class name. To disable automatic screen reporting, set
> > the flag FirebaseAutomaticScreenReportingEnabled to NO in the
> > Info.plist
> > 2017-03-12 21:51:15.526 avenir-design-app[14259:] <FIRAnalytics/INFO> Firebase Analytics v.3600000 started
> > 2017-03-12 21:51:15.526 avenir-design-app[14259:] <FIRAnalytics/INFO> To enable debug logging set the following
> > application argument: -FIRAnalyticsDebugEnabled (see
> > )
> > 2017-03-12 21:51:15.529 avenir-design-app[14259:]
>
> <FIRAnalytics/INFO> Successfully created Firebase Analytics App
> > Delegate Proxy automatically. To disable the proxy, set the flag
> > FirebaseAppDelegateProxyEnabled to NO in the Info.plist
> > 2017-03-12 21:51:16.240 avenir-design-app[14259] <Notice> [Firebase/Crash][I-CRA000004] Successfully initialized
> > 2017-03-12 21:51:16.457: <FIRInstanceID/WARNING> FIRInstanceID AppDelegate proxy enabled, will swizzle app delegate remote
> > notification handlers. To disable add
> > "FirebaseAppDelegateProxyEnabled" to your Info.plist and set it to NO
> > 2017-03-12 21:51:16.458: <FIRInstanceID/WARNING> Failed to fetch APNS token Error Domain=com.firebase.iid Code=1001 "(null)"
> > 2017-03-12 21:51:17.332: <FIRMessaging/INFO> FIRMessaging library version 1.2.0
> > 2017-03-12 21:51:17.965: <FIRMessaging/WARNING> FIRMessaging AppDelegate proxy enabled, will swizzle app delegate remote
> > notification receiver handlers. Add "FirebaseAppDelegateProxyEnabled"
> > to your Info.plist and set it to NO
> > 2017-03-12 21:51:30.608 avenir-design-app[14259:] <FIRAnalytics/INFO> Firebase Analytics enabled
> > 2017-03-12 21:51:33.153296 avenir-design-app[14259:575161] [MC] System group container for systemgroup.com.apple.configurationprofiles
> > path is
> > /Users/IAMPRAgency/Library/Developer/CoreSimulator/Devices/473CBD98-C9EA-411C-A167-A507936E8443/data/Containers/Shared/SystemGroup/systemgroup.com.apple.configurationprofiles
> > 2017-03-12 21:51:33.311040 avenir-design-app[14259:575161] [MC] Reading from private effective user settings.
> > 2017-03-12 21:51:36.119894 avenir-design-app[14259:575161] [Common] _BSMachError: port 8f07; (os/kern) invalid capability (0x14)
> > "Unable to insert COPY_SEND"
> > 2017-03-12 21:51:36.120561 avenir-design-app[14259:575161] [Common] _BSMachError: port 8f07; (os/kern) invalid name (0xf) "Unable
> > to deallocate send right"
> > 0
> > 0
> > 0
> > 0