简体   繁体   中英

Can't retrieve data from Firebase Database

I my code I can't retrieve data from Firebase.

When I am adding values on Firebase I do see that items added on Firebase database on my console.

Also I had created listener .childAdded and I see that items added on Firebase Database.

But when I call .value it retrieves nil result.

I don't know what's happening there.

Here is my code.

class FirebaseManager {

    var ref: DatabaseReference?
    var database: DatabaseHandle?

    static let shared: FirebaseManager = {
        Database.database().isPersistenceEnabled = true
        return FirebaseManager()
    }()

    private func initFireabase() {
        ref =  Database.database().reference(withPath: AMUtils.getUDID())        
    }

    func addToDB(composition: Composition) {
        initFireabase()
        ref?.child(String(composition.id)).child("id").setValue(composition.id)
        ref?.child(String(composition.id)).child("title").setValue(composition.title)       
    }

    func removeFromDb(composition: Composition) {
        ref?.child(String(composition.id)).removeValue()
    }

    func getCompositonFromDB(onResult: @escaping ([Composition]) -> Void){
        initFireabase()
        var compositions: [Composition] = []
        database = ref?.observe(.value, with: { (snapshot) in
            compositions.removeAll()
            let value = snapshot.value as? JSON
            compositions.append(AudioListParser.parseObject(json: value!))
            self.ref?.removeObserver(withHandle: self.database!)
            onResult(compositions)
        })
    }
}

getCompositonFromDB() I am calling when I am starting the view controller and this is always nil even I have values on database

Could anyone tell me what I did wrong here?

在此处输入图片说明

have you changed the rules for database?? like if you are authenticating you should set not null for both write and read and if you are not authenticating then keep it null for both, because doing exact opposite of above could cause some errors!!

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM