Tuesday, March 24, 2009

How to speed up loading your NotesViews in LotusScript

One of the things that can really slow down your NotesAgents is opening large NotesViews. When coding an agent, it is important to always check if the agent redundantly opens the same NotesView and if access time could be reduced. I do this with a short but handy function that I want to share here:

%REM
Accesses a view and stores it in listView; only opens the view, if the view does not already exist in listView
Input:
- viewname: name of view to be accessed
- listView: list of NotesViews, can be uninitialized
- isRefresh: if true, refreshes the view, otherwise not
Output:
- v: NotesView
- listView: list of NotesViews, returns at least the one accessed
%END REM

Function getView(v As NotesView, viewname As String, listView List As NotesView, isRefresh As Boolean) As Boolean

   On Error Goto errorhandler

   Set v = Nothing

   Dim s As New NotesSession

   Dim db As NotesDatabase
   Set db = s.CurrentDatabase

   If Iselement(listView(viewname)) Then
      Set v = listView(viewname)
   End If

   If v Is Nothing Then
      Set v = db.GetView(viewname)
   End If

   If v Is Nothing Then
      ' errorhandling
   End If

   If isRefresh Then
      Call v.Refresh()
   End If

   Set listView(viewname) = v

   getView = True

e:
   Exit Function
errorhandler:
   ' errorhandling
   Resume e
End Function

I call this function everytime I want to open a NotesView:


Function foo(listView list as NotesView) as Boolean
...
Dim vContacts as NotesView
If Not getView(vContacts, "vContacts", listView, True) Then Exit Function
...
End Function

The getView()-function checks in the list listView, if the view has already been opened. If so, it returns the view that is already in memory. If not (first access), it opens the view, adds it to listView and then returns the view.

Check the comment (eewweee ;-)) for what the parameters of getView() do. Note that the return value of my getView()-function only returns true or false, depending if the function ran into an error or not. I just like to have all my functions like that. The NotesView that I want to access is returned in the first parameter.

I pass listView through all my functions and procedures. This way I only open a view, if I have to.

2 Comments:

Anonymous Bill said...

I would routinely put notesview.autoupdate = false as well and leave the programmer to explicitly refresh under control.

I've seen some server crashes (8.5 as well as older) with NotesViewNavigator.getNext() where the view has refreshed itself.

---* Bill

March 25, 2009 12:11 AM  
Blogger marcusfoerster said...

Good one...

March 25, 2009 12:19 AM  

Post a Comment

Thank you for your comment. It will be published shortly. Keep having a great day!

<< Home