1. If you want to create a new list or map then use B4XCollections.CreateList or the built-in CreateMap keyword.
The purpose of GetEmptyList / GetEmptyMap is for cases where you need an empty collection with the expectation that it will remain empty.
For example, calling a method that expects a collection as a parameter which isn't needed in some cases. Instead of creating a new collection each time you can pass an empty collection. Same is true for methods that return a collection.
2. v1.15 is the latest version. I've updated the index.
Full example of using CopyOnWriteList:
Sub AppStart (Args() As String)
start
StartMessageLoop
End Sub
Private Sub start
Dim CopyList As CopyOnWriteList
CopyList.Initialize(Array(1, 2, 3))
DoSomethingWithList("a", CopyList.GetList)
CopyList.Add(4)
DoSomethingWithList("b", CopyList.GetList)
End Sub
Private Sub DoSomethingWithList (Name As String, l1 As List)
For i = 1 To 3
Log(Name & ": " & l1)
Sleep(1000)
Next
End Sub
Output:
a: (ArrayList) [1, 2, 3]
b: (ArrayList) [1, 2, 3, 4]
a: (ArrayList) [1, 2, 3]
b: (ArrayList) [1, 2, 3, 4]
a: (ArrayList) [1, 2, 3]
b: (ArrayList) [1, 2, 3, 4]
Whenever the CopyOnWriteList is modified, a new internal list is created. So if you passed the internal list to a method, the data for this method will not be unexpectedly modified.
Same example with a regular list:
Private Sub start
Dim l As List = B4XCollections.CreateList(Array(1, 2, 3))
DoSomethingWithList("a", l)
l.Add(4)
DoSomethingWithList("b", l)
End Sub
Private Sub DoSomethingWithList (Name As String, l1 As List)
Dim len As Int = l1.Size
For i = 1 To 3
Log($"Expecting to log ${len} items: "$ & Name & ": " & l1)
Sleep(1000)
Next
End Sub
Output:
Expecting to log 3 items: a: (ArrayList) [1, 2, 3]
Expecting to log 4 items: b: (ArrayList) [1, 2, 3, 4]
Expecting to log 3 items: a: (ArrayList) [1, 2, 3, 4]
Expecting to log 4 items: b: (ArrayList) [1, 2, 3, 4]
Expecting to log 3 items: a: (ArrayList) [1, 2, 3, 4]
Expecting to log 4 items: b: (ArrayList) [1, 2, 3, 4]