I have a PowerApp that has Project, Task and Subtask Hierarchy.
The way I used to build my Hierarchy was in my OnStart, where I would make a collection and build a Hierarchy for My Projects to have when expanding and retracting.
Code looked like this:
ClearCollect(
colTasks,
AddColumns(Project_Data,
"Show", ThisRecord.TaskLvl = 0,
"Expanded", false,
"TaskHierarchy",If(ThisRecord.TaskLvl= 0, "0", If(ThisRecord.TaskLvl= 1, With({localOuterRecord: ThisRecord}, LookUp(Project_Data, Id=localOuterRecord.ParentId).TaskNo & "." & Text(localOuterRecord.TaskNo, "000")))),
"ProjectCreationDate", If(ThisRecord.TaskLvl= 0, Oprettet, If(ThisRecord.TaskLvl= 1, With({localOuterRecord: ThisRecord}, LookUp(Project_Data, Id=localOuterRecord.ParentId).Created)))
)
);
// Then later in the OnStart I would have some more code
With(
{localColTasks: colTasks},
UpdateIf(
colTasks As ThisTask,
ThisTask.TaskLvl=2,
{
TaskHierarchy: LookUp(localColTasks, Id=ThisTask.ParentId).TaskHierarchy & "." & Text(ThisTask.TaskNo, "000"),
ProjectCreationDate: LookUp(localColTasks, Id=ThisTask.ParentId).ProjectCreationDate
}
)
);
Later on in a Gallery on one of the app screens I would have a gallery that had an expand/retract icon
With({localItem: ThisItem},
UpdateIf(
colTasks,
// Show childTasks of current task
!localItem.Expanded && ParentId = localItem.Id && TaskLvl <> 0,
{Show: true},
// Hide Hierarchy of current task
localItem.Expanded &&
(localItem.ProjectId = ThisRecord.ProjectId) &&
!(ThisRecord.TaskLvl = 0) &&
(localItem.TaskHierarchy in ThisRecord.TaskHierarchy) &&
Id <> localItem.Id,
{Show: false, Expanded: false},
// change Expanded field for this task
Id = localItem.Id,
{Expanded: !localItem.Expanded}));
This worked pretty well until I started having over 1000 items in my SharePoint list.
This started making the app slower and slower. I believe that it took on average 1,5-2 minutes to get in the app from the loading screen, and then it would take additional 20-30 seconds for all the data to get shown proper.
What I ended doing (to speed the app load mostly) is I moved the OnStart functionality to my Screen OnVisible with the same code.
I actually didn't know how to achieve this so I made a timer that runs once when the app is started. This timer then clicks a hidden button that executes the code.
Problem solved... Well kinda...
My app did start loading much, much faster and it only took like 10-30 seconds for everything to get shown inside the app. But the user was inside the app.
So now I am at a crossroad. I can either:
Try to somehow optimize this code so it runs faster where I can somehow incorporate the ClearCollect part with the With part and have it as a single block of code instead of one block running and then the second running.
Is this possible at all?
Something else I tried doing was instead of making an AddColumn where I create my TaskHierarchy I can "hardcode" the TaskHierarchy in my SharePoint list. This way I have no need for the ClearCollect at all. All the code still works fine. The only thing that now is ABSOLUTLY not working is the collapse part of my code:
// Hide Hierarchy of current task
localItem.Expanded &&
(localItem.ProjectId = ThisRecord.ProjectId) &&
!(ThisRecord.TaskLvl = 0) &&
(localItem.TaskHierarchy in ThisRecord.TaskHierarchy) &&
Id <> localItem.Id,
{Show: false, Expanded: false},
// change Expanded field for this task
Id = localItem.Id,
{Expanded: !localItem.Expanded}));
Data gets loaded on average 10 ish seconds faster. I still see al the projects, I can still expand my project tasks and subtasks.
Where this approach fails is when I try collapsing my tasks and subtasks. Nothing happens!!
Is there somebody that can figure out what this last part of the code needs to start working??
Failing that is there a better way to build the Hierarchy with my first method (OnStart) or will this always be a problem down the line?