Wednesday, August 8, 2007

Caching of display methods

Caching of display methods has been introduced to improve the performance of display and edit functions if they are calculated on the AOS, and to improve the performance when records are transferred from the server to the client.

Signing up methods for caching

Only methods that are explicitly added to the cache are affected by the new caching mechanism. To sign up a method for caching, the method cacheAddMethod on the form datasource should be called after super() in the init method of the datasource.

The call to cacheAddMethod also determines how often the cached display method value is updated. The value is filled in on fetching data from the back-end, and it is refreshed when reread is called on the datasource. Furthermore, by default the display method values are also updated when the record is written to the database, but that can be changed using the _updateOnWrite parameter in the cacheAddMethod.

Only display methods that are of display type can be cached, i.e. edit-methods cannot be cached



The cacheAddMethod signs up the specified display method for caching. The cached methods are calculated on fetching data, and the calculated values are then passed to the client together with the data. The cached values are refreshed on reread, and by default, on write and create.

Syntax

public boolean cacheAddMethod(str _methodName, boolean _updateOnWrite)

Arguments

Returns: TRUE if the method was signed up successfully, otherwise FALSE

_updateOnWrite: Determines whether the cached value is updated automatically when the record is written. Default value is TRUE.

This method should be called after initialization of the datasource, but before any data is fetched. Hence the call to this method should be placed after the call to super() in the init method.

The _updateOnWrite parameter also determines if the display method value should be calculated and cached when a new record is created. To manually update the cached value for the display method, call the cacheCalculateMethod method.

Only methods with the display keyword can be cached. Furthermore, only table methods can be cached, i.e. methods written on the form or the form datasource cannot be cached. Use the tableMethodStr function to get a compile check as to whether the method exists.

Do not sign up display methods that are not used on the form - they will be calculated for each record even though the values are never shown.

Example 1

public void Init()

{

super();

this.cacheAddMethod(tablemethodstr(custtable,freeValueCur));

}

On a form having custtable as a datasource, you might find this in the init method of the datasource

how to make look up

The super() call in Lookup checks whether the control is bound to a field, or to an Extended Data Type and calls either

performDBLookup(FieldId,FileId,Company);

or

performTypeLookup(ExtendedDataType,arrayIndex,Company);

which are both member functions (methods) on the Formcontrolname class (FormStringControl, FormRealControl, FormIntControl, FormDateControl or FormTimeControl).

You can override the super() call, and call one or the other method yourself, and give the necessary parameters.

One possibility is to call performDBLookup with only FieldId, in which case you can perform a lookup on another field within the same data source.

void Lookup()

{

FormStringControl FEL;

FEL = element.design().control(control::Country);

//12 is the Id for the field to be shown on the Country control

FEL.performDBLookup(12);

}

Both methods may also be called with an alternative Company to achieve a lookup in another data file. Default company is the one that the current data source belongs to.



Sample Code

public void lookup()
{
Query query = new Query();

QueryBuildDataSource queryBuildDataSource;

QueryBuildRange queryBuildRange;



// Create an instance of SysTableLookup where 'this' the current Form control.



SysTableLookup sysTableLookup = SysTableLookup::newParameters(tableNum(LedgerJournalName), this);

;



// The field to be shown in the lookup form.



sysTableLookup.addLookupField(fieldNum(LedgerJournalName, JournalName));





// Limit and arrange data selection.



queryBuildDataSource = query.addDataSource(tableNum(LedgerJournalName));

queryBuildRange = queryBuildDataSource.addRange(fieldNum(LedgerJournalName, JournalName));



sysTableLookup.parmQuery(query);



// Perform lookup



sysTableLookup.performFormLookup();


}