ios多線程queue的差異,ios多線程queue

來源:互聯網
上載者:User

ios多線程queue的差異,ios多線程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:@"下載" forState:UIControlStateNormal];    [btn addTarget:self action:@selector(start) forControlEvents:UIControlEventTouchUpInside];    btn.backgroundColor = [UIColor cyanColor];    [self.view addSubview:btn];        UIButton *btn2 = [UIButton buttonWithType:UIButtonTypeRoundedRect];    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];    }/*實驗結果:async + globel  圖片下載成功,顯示有延遲  在main中設定圖片才正常async + main  正常sync + globel  圖片下載成功,顯示有延遲, 在main中設定才正常sync + main 圖片無法下載,線程卡死*/-(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  圖片下載成功,顯示有延遲  在main中設定圖片才正常async + main  正常sync + globel  圖片下載成功,顯示有延遲, 在main中設定才正常sync + main 圖片無法下載,線程卡死

copy與mutablecopy

copy產生的是不可變的對象

mutablecopy產生的是可變對象。


聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.