What is the best practice to collect data from a datasource into a collection when the application start? I have a ClearCollect(MyCollection, Filter(Datasource, FilterCondition)) in the OnVisible event of the screenObject, and the Gallery on that screen that connects to the collection doesn't show anything. Looking at the Collection itself (content Tab, Collections), it shows the Table headers, but is empty. When I insert a button with the same formula in the "OnSelect" event, it works.
When is the OnVisible event firing? Isn't it when the application starts? (play button in the PowerApps studio).
Is there a better way to initialize a collection with content at "App start time"?
Thank you,
Olivier
Solved! Go to Solution.
OnVisible will trigger when you come upon the Screen. This takes place when you open the app and when you Navigate to the screen. For actions I only want done once, I change OnVisible to:
If(!loadapp, ClearCollect(collection,Filter(datasource,condition)); UpdateContext({loadapp: true}) )
This means that if the variable loadapp is false, which is default, it will make the collection, then set loadapp to true. This is irreversible since nowhere else updates the variable to false again.
If you are editing in PowerApps Studio and you want to see your OnVisible property work, then you need to either reopen the app with your OnVisible properties saved or you need to Navigate away to another Screen, then Navigate back.
EDIT: clarification: pressing the Play button does not trigger the OnVisible property.
OnVisible will trigger when you come upon the Screen. This takes place when you open the app and when you Navigate to the screen. For actions I only want done once, I change OnVisible to:
If(!loadapp, ClearCollect(collection,Filter(datasource,condition)); UpdateContext({loadapp: true}) )
This means that if the variable loadapp is false, which is default, it will make the collection, then set loadapp to true. This is irreversible since nowhere else updates the variable to false again.
If you are editing in PowerApps Studio and you want to see your OnVisible property work, then you need to either reopen the app with your OnVisible properties saved or you need to Navigate away to another Screen, then Navigate back.
EDIT: clarification: pressing the Play button does not trigger the OnVisible property.
Thank you for the detailed explanation, I will let you know how it works as soon as I try it.
Thanks again,
Olivier
Also, you say "since it is false by default" (talking about the context variable loadapp), I thought that a context variable was "inexistant" (null?) before a first assignment, is a context variable false even before it was ever used?
Thanks,
Olivier
Some observations;
When you test a variable value, the container for the variable must exist, even if the variable does not have a value yet.
In other words, if I type in the following for the OnVisible action;
If(!contextvariable, true, false)
It will give me an 'invalid name' error for contextvariable - because nowhere else on the page have I actually created the context container with an UpdateContext formula. It hasn't been defined yet, so I have nothing to test.
Note, defining a container for a variable and setting the variable to something are two separate things.
If I add a button and set the OnSelect property to;
UpdateContext({contextvariable: true})
Two things happen as I type it;
NOTE: This is all before the button is actually pushed - simulating the OnVisible action.
Now, this is where it gets interesting.
Before the variable has been set, the following tests on the container yield the following results;
!contextvariable = true
IsBlank(contextvariable) = true
IsEmpty(contextvariable) = false
While it hasn't been set yet, if you test it to see if it isn't true you will be told that indeed, it is not true. This does not however mean it is false, simply that it is not set to boolean true. If you actually look at the variable value, there's nothing in it. It's blank.
This might seem like a long way of saying "It's not false by default, it's blank", but here's something else;
If you change the Onselect formula to this;
UpdateContext({contextvariable: 10})
We have effectively changed the data TYPE of the container - still without actually pushing the button.
Now the OnVisible tests look like this;
!contextvariable = false
IsBlank(contextvariable) = true
IsEmpty(contextvariable) = false
It's still blank, but now the !contextvariable test comes back false, instead of true.
This could seriously mess with your head if, for some reason, you're using numbers in this variable
Once you set it by pushing the button, when next you run OnVisible the results will be;
!contextvariable = false
IsBlank(contextvariable) = false
IsEmpty(contextvariable) = false
So, long story short, it seems to be blank by default. Testing if it isn't boolean true comes back true simply because it isn't boolean true - but that doesn't mean it's boolean false. It's blank.
It never seemd to be empty at any point.
It's only true for logical negation (!contextvariable which is the same as testing IS NOT TRUE) when the type is not a number. When the type is a number, it returns false. Maybe someone can explain why this is a little better
@Anonymous,
I think the PA team called it "null" when the variable hasn't been determined. It just happens to work like 'false' if it hasn't been defined. The behavior of variables can seem wonky. If you're revising any part of how the variable is defined in UpdateContext, it gets reset to null.
Power Apps User Groups are coming! Make sure you’re among the first to know when user groups go live for public preview.
Did you miss the call?? Check out the Power Apps Community Call here!
User | Count |
---|---|
255 | |
205 | |
76 | |
37 | |
31 |
User | Count |
---|---|
331 | |
214 | |
121 | |
71 | |
55 |