You can use ExecuteFetchHints in a selfVM expression when a ViewModel fetch hint must be applied to an object list that your OCL code has produced, especially before processing that list in a loop.
Purpose
The Query Planner normally determines the data needed by a ViewModel automatically. It can have insufficient visibility when the required data is used deep inside a loop or in a complex server-side ViewModel.
ExecuteFetchHints gives the ViewModel an explicit opportunity to fetch that data before the loop continues. It finds ViewModel nestings whose names start with FetchHints and executes the expressions in their columns for the objects in the list you supply. This brings the data required by those expressions into memory up front, rather than fetching it repeatedly while the loop runs.
Syntax
selfVM.ExecuteFetchHints(objectlist)| Part | Meaning |
|---|---|
selfVM
|
The standard variable that refers to the current ViewModel. See Documentation:SelfVM. |
ExecuteFetchHints
|
Executes the fetch-hint expressions defined by nestings whose names start with FetchHints.
|
objectlist
|
The list of objects on which the fetch-hint expressions are evaluated. |
Use it when OCL creates the list
Use ExecuteFetchHints after you have populated an object list in OCL and before later logic reads data that the FetchHints nesting requires.
For example, the following logic builds vSeekerResult from the current user's favorite articles. Calling ExecuteFetchHints after the list is built applies the ViewModel's FetchHints expressions to those result objects.
vSeekerResult->Clear;
Singleton.oclSingleton.User.FavoriteArticle->select(fa|
fa.Filter.SqlLike(vFavFilter+'%') or vFavFilter.IsNullOrEmpty
).FavoriteArticles->collect(a|
let sa = a.SalesArticles->select(sa|sa.MarketingCompany=self)->first in
(
if sa.isnull then
vSeekerResult
else
vSeekerResult.Add(sa)
endif
)
);
selfVM.ExecuteFetchHints(vSeekerResult)In this example, vSeekerResult is the object list passed to the operation. The call does not create the list; the preceding OCL does that. The call makes the ViewModel evaluate the FetchHints columns for the objects already present in that list.
Configure the fetch hints
- In the ViewModel, create or identify a nesting whose name starts with
FetchHints. - Put the expressions that represent the data your later loop or ViewModel logic needs in that nesting's columns.
- Build the relevant object list in OCL.
- Call
selfVM.ExecuteFetchHintswith that list before the code that repeatedly accesses the data.
The name prefix is significant: only nestings with names that start with FetchHints are found by this operation.
When this is useful
Use this operation when both conditions apply:
- The list is produced by your OCL logic rather than by the normal seeker flow.
- The later processing accesses related data repeatedly, and the automatic Query Planner cannot determine all of those requirements before the loop starts.
For the overall approach to reducing repeated fetching and designing FetchHints nestings, see Documentation:Efficient ViewModel fetch.
Related selfVM operations
ExecuteFetchHints is one of the operations available on selfVM. The same variable also provides operations such as Save and ExecuteAction; see the complete list in Documentation:SelfVM.
