ObservSingleEvent (from: with :) getting old data

Background:

I have an application in which new users fill out a registration form (consisting of a username, email and password) to register for an account. When a new user submits a form, the application checks its Firebase database to see if the username has already been executed. The problem here is that observeSingleEvent(of:with:) does not receive the latest data. When I update data directly from the Firebase console, the changes are not reflected in the observeSingleEvent(of:with:) returned results. Even between application reloads, new data changes are not returned.

The only thing I saw in this question is here . The user says that when using observeSingleEvent(of:with:) persistence is not supported. But my perseverance is disabled. Also, I set keepSynced() in the FIRDatabase instance to false (and I also tried true). No luck with the setup.

Here is an example of what I'm trying:

 let usersDatabaseReference = FIRDatabase.database().reference().child("users") usersDatabaseReference.keepSynced(false) // Query the database to see if the username is available // The user with the username "mark" was removed from the database via the Firebase console let username = "mark" // This user used to exist in the database (and was previously added through the app). usersDatabaseReference.child(username).observeSingleEvent(of: .value, with: { (snapshot) in ... } ) 

The above block of code should return the latest data (one where the user mark "should not exist").

I am wondering if someone else ran into this problem or if anyone has any possible solutions.

Note. Development using iOS 10.1 and quick 3.

+5
source share
3 answers

So, my problem had nothing to do with caching or getting old results. Some of my code logic depended on checking the value parameter of the snapshot parameter, which was returned to the completion handler for observeSingleEvent(of:with:) . I mistakenly assumed that I could check the null database value by comparing it with nil ; this led to a misinterpretation of the results.

The best way to check the selection of a null database is to call the exists() method on the FIRDataSnapshot instance. exists() returns true if snapshot contains a non- null value.

0
source

observeSingleEvent(of:with:) returns a value only once

try the following:

 refHandle = postRef.observe(FIRDataEventType.value, with: { (snapshot) in let postDict = snapshot.value as? [String : AnyObject] ?? [:] // ... }) 

this will add a listener to the link database event

+1
source

I recommend that you reorganize your JSON data model using the username as the key. In doing so, you should receive an error message when you try to duplicate a user in the database.

0
source

Source: https://habr.com/ru/post/1266176/


All Articles