I got this sigabrt error : Thread 1:EXC_BAD_INSTRUCTION (code=EXC_I386_INVOP, subcode=0x0) as shown in the view did load
The code is responsible for following an unfollowing users The sigabrt error occurs during segue from the search View to the user profile view where i can follow or unfollow that user
var loggedinuser : FIRUser?
var otheruser : NSDictionary?
var loggedinuserdata : NSDictionary?
var databaseref : FIRDatabaseReference!
@IBOutlet weak var name: UILabel!
@IBOutlet weak var followorunfollow: UIButton!
override func viewDidLoad() {
super.viewDidLoad()
self.databaseref = FIRDatabase.database().reference()
databaseref?.child("users").child(self.loggedinuser!.uid).observe(.value, with: { (snapshot) in :Thread 1:EXC_BAD_INSTRUCTION (code=EXC_I386_INVOP, subcode=0x0)
self.loggedinuserdata?.setValue(self.loggedinuser!.uid, forKey: "uid" )
}) { (error) in
print(error.localizedDescription)
}
databaseref?.child("users").child(self.otheruser?["uid"] as! String).observe(.value, with: { (snapshot) in
let uid = self.otheruser?["uid"] as! String
self.otheruser = snapshot.value as? NSDictionary
self.otheruser?.setValue(uid, forKey: "uid")
}) { (error) in
print(error.localizedDescription)
}
databaseref?.child("following").child(self.loggedinuser!.uid).child(self.otheruser?["uid"] as! String).observe(.value, with: { (snapshot) in
if(snapshot.exists())
{
self.followorunfollow.setTitle("follow", for : UIControlState())
}
else
{
self.followorunfollow.setTitle("unfollow", for : UIControlState())
}
}) { (error) in
print(error.localizedDescription)
}
self.name.text = self.otheruser?["name"] as? String
}
@IBAction func didtapfollow(_ sender: Any) {
let followerref = "followers/\(self.otheruser?["uid"] as! String)/\(self.loggedinuserdata?["uid"] as! String)"
let followingref = "following/" + (self.loggedinuserdata?["uid"] as! String) + "/" + (self.otheruser?["uid"] as! String )
if(self.followorunfollow.titleLabel?.text == "follow")
{
let followerdata = ["name" : self.loggedinuserdata?["name"] as! String, "pc " :" \(self.loggedinuserdata?["pc"])"]
let followingdata = ["name" : self.otheruser?["name"] as! String, "pc ":" \(self.otheruser?["pc"])"]
let childupdates = [followerref: followerdata,
followingref:followingdata]
databaseref?.updateChildValues(childupdates)
let followerscount:Int?
let followingcount:Int?
if(self.otheruser?["followerscount"] == nil )
{
followerscount = 1
}
else {
followerscount = self.otheruser?["followerscount"] as! Int + 1
}
if(self.loggedinuserdata?["followingcount"] == nil)
{
followingcount = 1
}
else {
followingcount = self.loggedinuserdata?["followingcount"] as! Int - 1
}
databaseref?.child("users").child(self.loggedinuserdata?["uid"] as! String).child("followingcount").setValue(followingcount)
databaseref?.child("users").child(self.otheruser?["uid"] as! String).child("followerscount").setValue(followerscount)
}
else
{
databaseref?.child("users").child(self.loggedinuserdata?["uid"] as! String).child("followingcount").setValue(self.loggedinuserdata!["followingcount"] as! Int - 1)
databaseref?.child("users").child(self.otheruser?["uid"] as! String).child("followerscount").setValue(self.otheruser!["followerscount"] as! Int - 1 )
_ = "followers/\(self.otheruser?["uid"] as! String)/\(self.loggedinuserdata?["uid"] as! String )"
let followingref = "following/" + (self.loggedinuserdata?["uid"] as! String) + "/" + (self.otheruser?["uid"] as! String )
let childupdates = [followingref: NSNull(),
followerref:NSNull()]
databaseref?.updateChildValues(childupdates)
}
}
}