Differences between ios multi-thread queue and ios multi-thread queue
# Import "ViewController. h "# import" View2Controller. h "@ interface ViewController () {UIImageView * img;} @ end @ implementation ViewController-(void) viewDidLoad {[super viewDidLoad]; self. view. backgroundColor = [UIColor redColor]; img = [[UIImageView alloc] initWithFrame: CGRectMake (0, 20,200,200)]; img. backgroundColor = [UIColor greenColor]; [self. view addSubview: img]; UIButton * btn = [UIButton buttonWithType: UIButtonTypeRoundedRect]; btn. frame = CGRectMake (30,300,100, 50); [btn setTitle: @ "Download" forState: UIControlStateNormal]; [btn addTarget: self action: @ selector (start) forControlEvents: UIControlEventTouchUpInside]; btn. backgroundColor = [UIColor cyanColor]; [self. view addSubview: btn]; UIButton * btn2 = [UIButton buttonWithType: bytes]; btn2.frame = CGRectMake (150,300,100, 50); [btn2 setTitle: @ "" forState: UIControlStateNormal] [btn2 addTarget: self action: @ selector (jumpTo) forControlEvents: UIControlEventTouchUpInside]; btn2.backgroundColor = [UIColor cyanColor]; [self. view addSubview: btn2]; // [self testcopy];}/* Experiment Result: async + globel image downloaded successfully, if there is a delay in the display, set the image in main to normal async + main Normal sync + globel image download is successful, there is a delay in the display, and the setting in main is normal sync + main image cannot be downloaded, thread stuck */-(void) start {img. image = nil; NSLog (@ "start"); dispatch_async (dispatch_get_main_queue (), ^ {[self downloadImg] ;}); NSLog (@ "end ");} -(void) downloadImg {NSString * urlStr = @ "http://cdn2.raywenderlich.com/wp-content/uploads/2012/09/iOS_6_feast.png"; NSURL * url = [NSURL URLWithString: urlStr]; NSError * error = nil; NSData * imgData = [[NSData alloc] initWithContentsOfURL: url options: NSDataReadingUncached error: & error]; NSLog (@ "img downloaded % d", imgData. length); // dispatch_sync (dispatch_get_main_queue (), ^ {// img. image = [UIImage imageWithData: imgData]; //}); img. image = [UIImage imageWithData: imgData];}-(void) testcopy {NSArray * a = [NSArray array]; NSMutableArray * B = [NSMutableArray array]; NSMutableArray * c = [a mutableCopy]; NSMutableArray * d = [B copy]; // [c addObject: @ "1"]; // [d addObject: @ "2"];}
Async + globel: The image is successfully downloaded. If there is a delay in the image set in main, async + main is normal sync + globel is successfully downloaded. If there is a delay, the sync + main image cannot be downloaded only when it is set in main, and the thread is stuck.
Copy and mutablecopy
Copy produces immutable objects.
Mutablecopy generates a mutable object.