Last active
August 29, 2015 14:07
-
-
Save pgor/b63e40cd428bdec3994a to your computer and use it in GitHub Desktop.
Simple category on NSArray to process it in specified batch sizes
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
@interface NSArray (Batching) | |
- (void) enumerateObjectsWithBatchSize:(NSUInteger)batchSize | |
usingBlock:(void (^)(NSArray* batch, NSUInteger startIndex, BOOL *stop))block; | |
@end | |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#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