本文介绍了@autoreleasepool循环或循环@autoreleasepool?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在循环操作中将大量自动释放的对象放入autoreleasepool中是一种很好的做法。我发现有人把@autoreleasepool放在循环中,但其他人把循环放在@autoreleasepool中。

It's a good practice to put lots of autoreleased object in an autoreleasepool at loop action. I found someone put the @autoreleasepool in loop but others put loop in @autoreleasepool.

1:

while ([rs next]) {
    @autoreleasepool {
        NSDictionary *dict = [self dictFromXX];
        //...
    }
}

2:

@autoreleasepool {
    while ([rs next]) {
        NSDictionary *dict = [self dictFromXX];
        //...
    }
}

这是更好?或者代码1和2之间有什么区别?

Which is better? or any difference between code 1 and 2?

谢谢!

推荐答案

在每次迭代的第一个示例中,池都已耗尽。如果迭代的主体涉及大量自动释放的对象,这是有道理的。

In your first example for every iteration the pool is drained. This makes sense if the body of the iteration involves a lot of autoreleased objects.

第二个例子只会在循环后消耗池一次。

The second example will only drain the pool once after the loop.

因此,如果循环的内部导致内存膨胀,那么请选择第一个选项。如果整个循环中的内存膨胀是可接受的,则循环然后使用选项2。

So if the internals of the loop are causing the memory bloat then go for option one. If the memory bloat over the whole loop is acceptable then loop then use option two.

这篇关于@autoreleasepool循环或循环@autoreleasepool?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-04 05:26