In PHP I would use array_chunk
to split up an array then process each chunk. In objective C this doesn't seem so straight forward, is there a cleaner way than something like this?
- (void)processTransaction:(NSArray *)transactions
{
NSInteger batchCount = (transactions.count - 1) / self.batchSize + 1;
for (NSInteger batch = 0; batch < batchCount; batch ++) {
for (NSInteger batchIndex = 0; batchIndex < self.batchSize; batchIndex++) {
NSInteger index = batch * self.batchSize + batchIndex;
if (index >= transactions.count) {
return;
}
Transaction *transaction = [transactions objectAtIndex:index];
// Process
}
// Save
}
// Done
}
If // Save
isn't too complicated I would do
- (void)processTransaction:(NSArray *)transactions
{
NSInteger batchIndex = 0;
for (Transaction *transaction in transactions) {
// Process
batchIndex++;
if (batchIndex >= self.batchSize) {
// Save
batchIndex = 0;
}
}
if (batchIndex > 0) {
// Save
}
// Done
}