Retrieving data from firebase sometimes works, and sometimes doesn't

I have searched this issue to try and figure this problem out. I’ve seen multiple ways to get data from Firebase, but I can’t seem to get anything working right. These blocks that I have now work sometimes, and sometimes they don’t. I tried adjusting the wait times to give things more time to load as I read in a previous forum post. However, that does not work either. I am trying to upload guest names to firebase with the parking spot they are in, the time they arrive, and special instructions if they have any. It’s basically an app to check in guest and know whos arrived first. After I get the tag list under “GuestList” I put in into a list within the app called “fireBaseList”. Then I do a loop for each tag (Parking Spot Number) I have and retrieve/combine the guest names, parking spot numbers, times, and special instructions under one list called “sortedList”. After that, it puts those data items in a new list but ascending order so that the guest whose been here the longest will be at the top of the list. Then I show it on “guestListView” (Which is just a List_view) and pray that it works. It’s supposed to update every time we delete a guest (they leave) and every time we add a guest (they arrive). It might not work because I loop through and ask for to much information at once? If that’s the case, any Ideas on a better method? Any help would be great. Thanks!

OG 2

1 Like

Welcome to the Thunkable Community @josh.pilot538.

I’ve spotted some issues here, I’ll try and run you through them :slight_smile:

  • When working with components that have a then do function, you want to keep the blocks inside of that space, even if they’re parented multiple times. This ensures the app doesn’t jump ahead and goes in order of the blocks. See here for an example of what to do and what not to do: https://i.snipboard.io/iaGcnD.jpg. In your code, you are making a request to Firebase and waiting for a reply, meanwhile your app has moved onto the next step before waiting for the reply. This is why you are getting it working sometimes and sometimes not (request-receive time with Firebase fluctuates).

  • Another potential issue I have noticed but maybe it’s okay, is that you are not initialising your list upon screen starting. Your code makes you wait for the listener to activate to fill the list viewer with data. This is only my interpretation from looking at your blocks. Maybe it’s fine.

Let me know how you get on and make sure to come back if you have any further issues.

3 Likes

Thank you so much, I’m going to change blocks so that most of them, if not all of them will be in the “then do” blocks! I’ll let you know if it works. I’m a little confused about the initializing the list’s on screen starting. I’ve got all the variables listed at the top with create new list for each list I use. But do I need to do something else to initialize them when the screen starts?

1 Like

Hi

Let me know how you get on with changing the block order.

Apologies for not being clear about the initialise list bit I was on about. When you first open up the screen, does the screen immediately display a list of all guests, or must you wait for data to be changed in Firebase DB?

2 Likes

Great suggestions!

1 Like

No you’re good. Sorry I’ve been busy these past few days and didn’t have a chance to go back and change anything. When I open the screen, the list displays immediately each time I open it. I ended up changing the code a little since I wasn’t able to keep most of it within the purple “do” spaces. I made it to where it combines the name, with the parking spot number and time before I send it to the data base that way the phone doesn’t have to combine them into one line of text while it’s retrieving the data. That seemed to help alot as far as the speed and glitches go. However, now if there is only one list item left on the screen, it’ll delete it in the data base, but it won’t take it off the screen. I’m trying to figure out if maybe the old value is just stuck in memory somewhere or maybe the “data changed” block just doesn’t update if there’s only one value? Here are the new blocks that I have.

Oh no, it’s saying access denied when I try to upload the pictures of my blocks :pensive:



Hi,

I’ve read your message and just had a look over your blocks. Does the list only add some values? Image OG 4.PNG has a Firebase Get block in a while loop. I’ve done this before myself, and I’ve had issues. It is inefficient to make lots of requests to Firebase like this. Your best option is when value is changed, retrieve all data from Firebase to a variable. You can do this by setting the key as “/”. Since you are only storing “text” in Firebase, it won’t make a noticeable delay in retrieving data if you get all values.

Your entire Firebase DB is now a variable, which is easier worked with. You then want to add all the items again to the guestList & reset the GuestListView items like you were initially doing in image OG 4.PNG.

For the last item on screen, it should be fixed with adding the IF statement.

Should look something like this, I tried to use the same naming scheme to help you understand it:

Hopefully this helps, let us know how you get on.

2 Likes

I really appreciate your time in trying to help me solve this! So I tried exactly that. It seemed like it should have worked, but instead it only gives me the parking spot number instead of the actual text that is stored after the parking spot number. In the picture I’m showing the results I get. This Is also how I’m saving the data to firebase as well as how it looks saved in firebase.

** edit Oh wait! do you mean instead of saving the line of text under the parking spot number, just save the text alone? I can do that, It’s just right now I’m using the parking spot number value to be able to delete it when the guest leave. So I’m unsure of how to delete just the text without the parking value in front. Unless maybe I search for a text equal to the one I want to delete? Maybe that’s a way to do it. I’ll try that.

OG 3

Hi,

Would you be able to show me a larger snippet of your Firebase DB, so I can replicate this in a test environment and look into it. Apologies if my example didn’t work, I’ll try and fix this for you.

Also if you’re comfortable with it, you can send me the project link either here or via community message.

Yah sure! here is the project link. https://x.thunkable.com/projects/5ea2aaed6472b7a8687763af/9bb65ce9-4b0a-4a07-a67c-effa7b09280f/blocks

Those were the only values I entered into the data base.

1 Like

I am pretty sure I know what’s happening here but let me just double check it so I can confirm it with you :slight_smile:

1 Like

Hi @josh.pilot538,

Here’s what seems to work for me:

The remove last from list block is there because it adds an empty space. If you find this causing list viewer issues try removing it, though it works fine on my end :slight_smile:

Let me know how you get on.

It looks like there was supposed to be an image on your post, but i’m not able to see it :frowning:

Sorry about that. Checked further into it. Your example you sent me from Firebase shows the tags as “1”,“2”,“3”. The example above works fine when they are in ascending order but I am guessing these numbers represent parking spaces and thus change/won’t be in order?

1 Like

No, you’re good! Yeah, the parking spaces won’t be in order. But I’m trying out just saving the text alone instead of the parking spaces. The only problem I’m having here is that it leaves the last item on the screen even though it’s been deleted from the data base.

Ah so you have the items adding to the list okay?

I know you’re probably in the late stages of developing this app, but could I recommend using Airtable instead. It is designed to be used like this and works a charm with lists.

(edit)

This would also open up the advantage of using the new Data Viewer. Let me know what you think and I could help you incorporate Airtable instead :slight_smile:

I finally figured it out! It was a little annoying but I set a label to change it’s background color for every step in the “data changed block” to see what the exact process was. The color’s were separated by 1 sec “wait blocks”. I found out that the “get block” for firebase wouldn’t run if there were no values, any blocks under the “get block” would run if there were no values. So I just set the list to empty after the “get block”. Thank you for all your help! I really appreciate it!

2 Likes