如何让对象在block后台队列中销毁?

请问:“把对象捕获到 block 中,然后扔到后台队列去随便发送个消息以避免编译器警告,就可以让对象在后台线程销毁了。”
我写了个demo,为啥不生效呢?作者的原意是什么呀?

image.png

原文链接:https://blog.ibireme.com/2015...

我的代码

  • (void)viewDidLoad {
    [super viewDidLoad];
    [self t93_destory];

}

  • (void)t93_destory {
    NSArray *arr11 = @[@"aaa", @"bbb", @"ccc"];
    NSArray *tmp = arr11;
    arr11 = nil;
    NSLog(@"111__%@_%@", arr11, tmp);
    dispatch_async(dispatch_queue_create("kqykqy", DISPATCH_QUEUE_CONCURRENT), ^{

    NSLog(@"222 _%@_%@_%@",[NSThread currentThread], arr11, tmp);
    
    [tmp class];
    
    NSLog(@"333 tmp_%@_%@_%@",[NSThread currentThread], arr11, tmp);

    });

    sleep(2);
    NSLog(@"444_%@_%@_%@",[NSThread currentThread], arr11, tmp);

}

阅读 2.1k
1 个回答

1、测试方式不对
2、作者的原意是指: “当容器类持有大量对象时,其销毁时的资源消耗就非常明显,可以通过一个小 Tip:把对象捕获到 block 中,然后扔到后台队列去随便发送个消息以避免编译器警告,就可以让对象在后台线程销毁了 ”

看关键字:
前提:持有大量对象时,个人理解:对象比较多或是占用资源比较大时
原因:销毁时的资源消耗就非常明显,个人理解: CPU 占用太多,影响性能啦
目的:避免编译器警告
撰写回答
你尚未登录,登录后可以
  • 和开发者交流问题的细节
  • 关注并接收问题和回答的更新提醒
  • 参与内容的编辑和改进,让解决方法与时俱进
推荐问题