Hi, I'm trying to use a queue for a calculation intensive for-loop to make my iOS app more responsive. The original for-loop works without errors:
for (NSUInteger i = 0; i < count; i++) { myArray[i] = [self doCalculation: i]; } When count gets too large, there is a delay when the results are shown on the screen. So after reading through Apple's docs on concurrency, I implemented the following replacement: dispatch_queue_t queue = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0); dispatch_apply(count, queue, ^(size_t i) { myArray[i] = [self doCalculation: i]; }); But after a few iterations, my app crashes because of: *** Terminating app due to uncaught exception 'NSRangeException', reason: '*** -[__NSArrayM setObject:atIndex:]: index 5 beyond bounds [0 .. 1]' *** Terminating app due to uncaught exception 'NSRangeException', reason: '*** -[__NSArrayM setObject:atIndex:]: index 3 beyond bounds [0 .. 0]' *** Terminating app due to uncaught exception 'NSRangeException', reason: '*** -[__NSArrayM setObject:atIndex:]: index 2 beyond bounds for empty array' Any thoughts why this happens, and how to fix it? Thanks, - Koen. _______________________________________________ Cocoa-dev mailing list (Cocoa-dev@lists.apple.com) Please do not post admin requests or moderator comments to the list. Contact the moderators at cocoa-dev-admins(at)lists.apple.com Help/Unsubscribe/Update your Subscription: https://lists.apple.com/mailman/options/cocoa-dev/archive%40mail-archive.com This email sent to arch...@mail-archive.com