I have a Read Only List object that is used by multiple objects in my application. I want all objects to use the one instance of the list so I use the following shared function to access it:
Private Shared mList As ReferenceList
Public Shared Function GetList() As ReferenceList
If mList Is Nothing Then
mList = DataPortal.Fetch(Of ReferenceList)(New Criteria())
End If
Return mList
End Function
Basic stuff, and this works fine. However, I want to add a Refresh method to my list object that will cause the list to be refetched from the database. When this method is called, ALL the objects referencing the shared list need to see that it has been updated.
I tried this:
Public Sub Refresh()
Dim NewList As ReferenceList
mList = Nothing
NewList = ReferenceList.GetList()
End Sub
This does cause the list to be refetched from the database, but I now have two instances of my list in memory with all the existing objects continuing to reference the old instance.
Is there some obvious way to do this, or do I have to resort to manually reconstructing the existing list object?
Thanks for the quick reply.
mInstance would need to be declared 'Shared', but I still don't think this would solve the problem.
My understanding is that because the data portal returns a new object, you need to somehow get all existing references to point to the new object. Accessing the list through a property doesn’t really change this, unless the existing objects periodically update their reference by calling this property.
I tried clearing all the items from my old list and then adding all the items from the new list as follows:
Public Sub Refresh
Dim OldList As ReferenceList = Me
mList = Nothing
Dim NewList As ReferenceList = GetList()
OldList.IsReadOnly = False
For Each r As ReferenceListInfo In NewList
OldList.Add(r)
Next
OldList.IsReadOnly = True
End Sub
This worked and all the visible grids refreshed themselves, but it seems a bit of an arduous approach.
Copyright (c) Marimer LLC