I was reading the topic of using iterator and iterabel Interface - https://developer.salesforce.com/docs/atlas.en-us.apexcode.meta/apexcode/apex_classes_iterable.htm
It says :
If you do not want to use a custom iterator with a list, but instead want to create your own data structure, you can use the Iterable interface to generate the data structure.
The am trying to understand how the below code works with the Batch Class. ( Example given at link - https://developer.salesforce.com/docs/atlas.en-us.apexcode.meta/apexcode/apex_classes_iterable.htm )
The Batch Class:
global class batchClass implements Database.batchable<Account>{
global Iterable<Account> start(Database.batchableContext info){
return new foo();
}
global void execute(Database.batchableContext info, List<Account> scope){
List<Account> accsToUpdate = new List<Account>();
for(Account a : scope){
a.Atextfield__c = 'Abc';
accsToUpdate.add(a);
}
update accsToUpdate;
}
global void finish(Database.batchableContext info){
}
}
The Foo Class which implements Iterable:
global class foo implements iterable<Account>{
global Iterator<Account> Iterator(){
return new CustomIterable();
}
}
Finally the class that implements Iterator:
global class CustomIterable
implements Iterator<Account>{
List<Account> accs {get; set;}
Integer i {get; set;}
public CustomIterable(){
accs =
[SELECT Id, Name,
NumberOfEmployees
FROM Account
WHERE Name Like 'A%'];
i = 0;
}
global boolean hasNext(){
if(i >= accs.size()) {
return false;
} else {
return true;
}
}
global Account next(){
// 8 is an arbitrary
// constant in this example
// that represents the
// maximum size of the list.
if(i == 8){return null;}
i++;
return accs[i-1];
}
}
Questions:
In
Foo
Class we are returning only an object of typereturn new CustomIterable();
, where does thehasNext(
) andNext()
when returning just a new object of typeCustomIterable
.How does the scope gets all the list of Account? From what I understand the
Start()
methodreturn new foo();
which in turnreturn new CustomIterable();
. Instantiation an object ofCustomIterable
Class only invokes it's constructor. How does the scope gets assigned a List of Accounts here?