How to provide User Data in Scan callback function in Java/C#?


#1

Summary

How can I provide User Data in Scan callback function in Java/C#

Resolution

Since the scan callback is a delegate, you can instantiate your user data class and provide the scan callback method there. Please see the following example in C#:

public class MyScan
	{
		public void scan(AerospikeClient client)
		{
			MyData data = new MyData("string1", 8);
			client.ScanAll(null, "test", "test", data.ScanCallback);
		}
	}

public class MyData
	{
		private string a;
		private int b;

		public MyData(string a, int b)
		{
			this.a = a;
			this.b = b;
		}

	public void ScanCallback(Key key, Record record)
		{
			// Have access to a,b at this point
		}
	}

The scan callback could also be put directly in the same class as the scan in some cases.
See AerospikeDemo/ScanParallel.cs which uses the member variable recordCount in it’s scan callback.