Using CacheAddMethod

When working in forms and using display methods, sometimes they have a tendency to slow down the form to the degree that makes you wonder if it’s worth it at all. This is when cacheAddMethod comes in handy.

When using cacheAddMethod we place the method in memory, the effect of this is that the method only runs once when the form loads and then every time we switch between records. This instead of display methods normal behavior, which means it runs every time all the time.

For the cacheAddMethod to do what it’s supposed to we need it to run when the form starts up. If we have created the display method “OurTestMethod” on table SalesTable we want to place the call to cacheAddMethod in the init method on the form datasource SalesTable.

     salesTable_ds.cacheAddMethod(
     tablemethodstr(SalesTable, OurTestMethod));

Now the form SalesTable runs much smoother, but it has some effects that we might not want. Let´s say that our method OurTestMethod returns a real and we want OurTestMethod to return 0.00 if a checkbox that is placed under the same tab as the field that uses our dislaymethod, has the value “No”.

This makes it a bit trickier, with the previous code nothing happens when the checkbox is unchecked. This is because the already calculated value is in memory and is not recalculated unless we switch between records so the data, and the our method, is reloaded. We need to trigger our cached method to be recalculated.

This is done like this:

1
2
3
4
5
6
7
  public void modified()
  {
      super();
 
      SalesTable_ds.cacheCalculateMethod(
      tablemethodstr(SalesTable, OurTestMethod));
  }

We add a modified method on the checkbox that says that when someone clicks it, our display method OurTestMethod is recalculated but still in memory. This is done with the datasource method cacheCalculateMethod.

Last 5 posts in Development

2 thoughts on “Using CacheAddMethod

Leave a Reply

Your email address will not be published. Required fields are marked *