如何在xcode中避免此警告.这是代码片段:
[player(AVPlayer object) addPeriodicTimeObserverForInterval:CMTimeMakeWithSeconds(0.1, 100)
queue:nil usingBlock:^(CMTime time) {
current+=1;
if(current==60)
{
min+=(current/60);
current = 0;
}
[timerDisp(UILabel) setText:[NSString stringWithFormat:@"%02d:%02d",min,current]];///warning occurs in this line
}];
Run Code Online (Sandbox Code Playgroud) cocoa-touch objective-c retain avplayer automatic-ref-counting
我正在阅读Xcode的文档,这里有些令我困惑的事情:
__block typeof(self) tmpSelf = self;
[self methodThatTakesABlock:^ {
[tmpSelf doSomething];
}];
Run Code Online (Sandbox Code Playgroud)
从文档中复制以下内容:
块形成对其捕获的变量的强引用.如果
self在块中使用 ,则块形成强引用self,因此如果self还具有对块的强引用(通常它具有),则会产生强引用循环.要避免循环,您需要__block在块外部创建一个弱(或)引用,如上例所示.
我不明白'弱(或__block)'是什么意思?
是
__block typeof(self) tmpSelf = self;
Run Code Online (Sandbox Code Playgroud)
和
__weak typeof(self) tmpSelf = self;
Run Code Online (Sandbox Code Playgroud)
这里完全一样吗?
我在文件中找到了另一篇文章:
注意:在垃圾收集环境中,如果将两个
__weak和__block修饰符都应用于变量,则该块将无法确保它保持活动状态.
所以,我完全不解.
memory-management weak-references objective-c ownership objective-c-blocks
与此问题非常相似,我正在尝试将使用ASIHTTPRequest&的项目转换ASIFormDataRequest为ARC.
在我的视图控制器类中,我经常request在完成块中引用和使用对象的属性(查看响应代码,响应数据等):
__block ASIFormDataRequest *request = [ASIFormDataRequest requestWithURL:[NSURL URLWithString:SOME_URL]];
[request setCompletionBlock:^{
if([request responseStatusCode] == 200) ....etc
Run Code Online (Sandbox Code Playgroud)
转换为ARC时,我收到警告:
在此块中强烈捕获"请求"可能会导致保留周期
这样做的正确方法是什么?
另一个SO用户在前一个帖子中指出,简单添加__weak可能会导致请求在块完成之前被释放,我认为这是真的.
如何在ARC下的完成/故障块中正确引用这些属性?
iphone objective-c asihttprequest ios automatic-ref-counting
在我支持iOS ARC的代码中,我需要将"self"和其他对象传递给一个块.更具体地讲,我需要与自我和互动ASIHTTPRequest内部对象ASIHTTPRequest的completionBlock.
_operation = [[NSInvocationOperation alloc] initWithTarget:self selector:@selector(parseServerReply) object:nil];
_request = [ASIHTTPRequest requestWithURL:@"server.address"];
// ...
[_request setCompletionBlock:^{
[self setResponseString:_request.responseString];
[[MyAppDelegate getQueue] addOperation:_operation];
}];
Run Code Online (Sandbox Code Playgroud)
为了避免以下警告:Capturing "self" strongly in this block will likely lead to a retain cycle.我已经修改了我的代码,以便__weak在此帖子之后添加属性块中使用的对象:修复警告"在此块中强烈捕获[一个对象]可能会导致保留周期"在启用ARC的代码中
结果代码是:
_operation = [[NSInvocationOperation alloc] initWithTarget:self selector:@selector(parseServerReply) object:nil];
_request = [ASIHTTPRequest requestWithURL:@"server.address"];
// ...
__weak NSOperation * operation = _operation;
__weak ASIHTTPRequest * request = _request;
__weak typeof(self) * self_ = self;
[request …Run Code Online (Sandbox Code Playgroud) 我AFNetworking在我的项目中使用,但它有2个警告:

完整的警告字符串是:
/AFNetworking/AFHTTPClient.m:575:38: Capturing 'operation' strongly in this block is likely to lead to a retain cycle
我很确定这是100%安全的,但我不想错过任何东西.我有以下代码
- (void) scheduleControlSurfaceProcess {
[self.operationQueueForMessageProcessing addOperationWithBlock:^{
// do something
[self scheduleControlSurfaceProcess];
}];
}
Run Code Online (Sandbox Code Playgroud)
自我是单身人士.该块作为非主线程线程非常出色.我没有在profiler中看到任何内存问题(我不太相信).
那么,我可以忽略这个警告,"Block会被捕获对象强烈保留的对象保留吗?" 如果没有,我怎么能坚持要释放块(用ARC)?让警告消失很容易,但分配id what = self似乎无法解决问题.
编辑:正如我在这个问题中意识到的那么晚,这里真正的问题是我从块内部重新安排.这显然是有问题的,因为每个块保留下一个.
nsoperationqueue ios objective-c-blocks automatic-ref-counting
objective-c ×4
ios ×3
iphone ×2
afnetworking ×1
avplayer ×1
cocoa-touch ×1
ios5 ×1
ownership ×1
retain ×1
warnings ×1