0

This is the firebase node posts and I don't know why it is optional. I think it is the problem.

posts:

Optional("hXCdzourFJNRLCH64GF7yZVzu0I3") -Kj_auPDxdboKTgPV-9J text : hello

  • This the get that sets the node in firebase database

@IBOutlet weak var post: UIButton! @IBOutlet weak var newpost: UITextView!

var databaseref = FIRDatabase.database().reference()
var loggedinuser : AnyObject?

override func viewDidLoad() {
    super.viewDidLoad()
    self.loggedinuser = FIRAuth.auth()?.currentUser
    newpost.textContainerInset = UIEdgeInsetsMake(30, 20, 20, 20)
    newpost.text = "what is new?"
    newpost.textColor = UIColor.lightGray
}

@IBAction func didtapcancel(_ sender: Any) {
    dismiss(animated: true, completion: nil)
}

func didbeganediting(_ textView : UITextView)  {
    if(newpost.textColor == UIColor.lightGray){
        newpost.text = ""
        newpost.textColor = UIColor.black
    }
}
func textFieldShouldReturn(_ textfield: UITextField) -> Bool {
    textfield.resignFirstResponder()
    return false
}

func didtappost(_ sender: Any) {
    if (newpost.text.characters.count>0) {
        let key = databaseref.child("posts").childByAutoId().key
        let childupdates = ["/posts/\(self.loggedinuser!.uid)/\(key)/text ": newpost.text,"/time/\(self.loggedinuser!.uid)/\(key)/text":"\(Date().timeIntervalSince1970)"] as [String : Any]
        self.databaseref.updateChildValues(childupdates)
        dismiss(animated: true, completion: nil)
    }
}

}

This is the tableview code:

@IBOutlet weak var ac: UIActivityIndicatorView!
@IBOutlet weak var feeds: UITableView!

var databaseref = FIRDatabase.database().reference()
var loggedinuser : AnyObject?
var loggedinuserdata : NSDictionary?
var posts = [NSDictionary]()

override func viewDidLoad() {
    super.viewDidLoad()
    self.loggedinuser = FIRAuth.auth()?.currentUser
    self.databaseref.child("users").child(self.loggedinuser!.uid).observeSingleEvent(of: .value) { (snapshot:FIRDataSnapshot) in
        self.loggedinuserdata = snapshot.value as? NSDictionary
        self.databaseref.child("posts").child(self.loggedinuser!.uid).observe(.childAdded, with: { (snapshot: FIRDataSnapshot) in
            self.posts.append(snapshot.value as! NSDictionary)
            self.feeds.insertRows(at: [IndexPath(row : 0 ,section : 0)], with: UITableViewRowAnimation.automatic)
            self.ac.stopAnimating()
        }) {(error) in
            print(error.localizedDescription)
        }
    }
}

func numberOfSections(in tableView: UITableView) -> Int {
    return 1
}

public  func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
    return  self.posts.count
}

public func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
    let cell : viewTableViewCell = tableView.dequeueReusableCell(withIdentifier: "opa", for:indexPath) as! viewTableViewCell
    let post = posts[(self.posts.count-1) - indexPath .row]["text"] as! String

    cell.configure(nil, name:self.loggedinuserdata!["name"]  as! String          
    handle: self.loggedinuserdata!["handle"]  as! String, post: post)

    return cell
}

2 Answers2

0

try adding:

feeds.reloadData()

before self.ac.stopAnimating()

There's no reason for you to call self on:

  • self.loggedinuserdata
  • self.databaseref (first instance)
GKH
  • 179
  • 1
  • 13
  • when i remove self from loogedinuserdata an error occurs says insert self but it works for the first instance for database ref – hannah james May 14 '17 at 09:03
  • self.feeds.reloaddata did not change anything is there an unwrapping problem anywhere causing the firebase to write optional before the node @GlennHerping – hannah james May 14 '17 at 09:05
  • It was only meant to work on the first instance of loggedinuserdata. :) What is the output of `print(posts[Insert index here]["text"])` before you insert the row? – GKH May 14 '17 at 10:42
  • the output is null self.databaseref.child("posts").child(self.loggedinuser!.uid).observe(.childAdded, with: {( snapshot: FIRDataSnapshot) in this line posts return 0 also i think it is due to optional data – hannah james May 14 '17 at 11:09
0

The issue is having an optional logged in user: self.loggedinuser = FIRAuth.auth()?.currentUser. Change it to

self.loggedinuser = FIRAuth.auth()!.currentUser
Peter Tao
  • 1,668
  • 6
  • 18
  • 29