Skip to content

Instantly share code, notes, and snippets.

@paulw11
Created May 24, 2017 03:23
Show Gist options
  • Save paulw11/127b5361c5c3daf03bca41bdae67fed8 to your computer and use it in GitHub Desktop.
Save paulw11/127b5361c5c3daf03bca41bdae67fed8 to your computer and use it in GitHub Desktop.
//
// ViewController.m
// ArrayCrash
//
// Created by Paul Wilkinson on 24/5/17.
// Copyright © 2017 Paul Wilkinson. All rights reserved.
//
#import "ViewController.h"
@interface ViewController ()
@property(nonatomic, strong) NSMutableArray *arr;
@end
@implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
self.arr = [NSMutableArray new];
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
-(void)viewDidAppear:(BOOL)animated{
[super viewDidAppear:animated];
__weak typeof(self) weakSelf = self;
dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_LOW, 0), ^{
for (int i = 0; i < 20000; i++) {
NSUInteger randomIndex = arc4random_uniform([weakSelf.arr count]);
[weakSelf.arr insertObject:[NSNumber numberWithInt:i] atIndex:randomIndex];
// [weakSelf.arr addObject:[NSNumber numberWithInt:i]];
NSLog(@"Added %@", weakSelf.arr[randomIndex]);
}
NSLog(@"Final count %ld", [self.arr count]);
});
[self performSelector:@selector(removeObjects) withObject:nil afterDelay:0.1];
}
-(void)removeObjects{
__weak typeof(self) weakSelf = self;
dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_HIGH, 0), ^{
for (int i = 0; i < 1000; i++) {
if (weakSelf.arr.count > 1) {
NSUInteger randomIndex = arc4random_uniform([weakSelf.arr count]);
[weakSelf.arr removeObjectAtIndex:randomIndex];
}
NSLog(@"Remove object");
}
});
}
@end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment