Skip to content

Instantly share code, notes, and snippets.

@pgor
Last active August 29, 2015 14:07
Show Gist options
  • Save pgor/b63e40cd428bdec3994a to your computer and use it in GitHub Desktop.
Save pgor/b63e40cd428bdec3994a to your computer and use it in GitHub Desktop.
Simple category on NSArray to process it in specified batch sizes
@interface NSArray (Batching)
- (void) enumerateObjectsWithBatchSize:(NSUInteger)batchSize
usingBlock:(void (^)(NSArray* batch, NSUInteger startIndex, BOOL *stop))block;
@end
#import "NSArray+Batching.h"
@implementation NSArray (Batching)
- (void) enumerateObjectsWithBatchSize:(NSUInteger)batchSize
usingBlock:(void (^)(NSArray* batch, NSUInteger startIndex, BOOL *stop))block
{
if ( block == nil ) {
return;
}
NSRange range = { .length = batchSize };
BOOL stop = NO;
while ( ! stop && range.location < [self count] ) {
range.length = MIN(batchSize, [self count] - range.location);
NSArray* batch = [self subarrayWithRange:range];
block( batch, range.location, &stop);
if ( ! stop ) {
range.location += range.length;
}
}
}
@end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment