Reputation: 187
I have a problem that I searched the web for and found really one answer and it did not work for me. I have an app where users can post images and a feed of all posted the images loads for them. One problem I keep getting the error of permission_denied in the output. I am quite confused and when I looked up an answer to this, all I got was try ref.removeAllObservers, but my code already has this. Here is my code, check it out:
func fetchPosts () {
let ref = FIRDatabase.database().reference()
ref.child("posts").queryOrderedByKey().observeSingleEvent(of: .value, with: { snapshot in
let postSnap = snapshot.value as! [String: AnyObject]
for (_,post) in postSnap {
let posst = Post()
if let author = post["author"] as? String, let pathToImage = post["pathToImage"] as? String, let postID = post["postID"] as? String {
posst.author = author
posst.pathToImage = pathToImage
posst.postID = postID
self.posts.append(posst)
}
self.postTableView.reloadData()
}
})
ref.removeAllObservers()
}
Please let me know if you have an answer. Also here is the output:
2017-05-03 08:06:31.771 Postflur[21064] [Firebase/Database][I-RDB03812] Listener at /posts failed: permission_denied
Upvotes: 0
Views: 1242
Reputation: 1056
Good to hear that you have solve it (I read it in the comment section) , but I wanted anyway to write this answer because I get a similar problem in the past. So by default firebase database only readable/writeable by authenticated users(if you take a look at rules inside your firebase console you will find the following):
{
"rules": {
".read": "auth != null",
".write": "auth != null"
}
}
So to let a non authenticated user read/ write data you should change the rules to the following
{
"rules": {
".read": true,
".write": true
}
}
and this was the solution for me.
Upvotes: 1