在启用ARC的代码中,如何在使用基于块的API时修复有关潜在保留周期的警告?
警告:
Capturing 'request' strongly in this block is likely to lead to a retain cycle
由这段代码生成:
ASIHTTPRequest *request = [[ASIHTTPRequest alloc] initWithURL:...
[request setCompletionBlock:^{
NSDictionary *jsonDictionary = [[CJSONDeserializer deserializer] deserialize:request.rawResponseData error:nil];
// ...
}];
Run Code Online (Sandbox Code Playgroud)
警告与request
块内对象的使用相关联.
cocoa cocoa-touch retain asihttprequest automatic-ref-counting
我需要阻止.但编译器会发出警告
"在这个区块强势捕捉'自我'可能会导致保留周期"
__weak typeof(self) weakSelf = self;
[generalInstaImage setImageWithURLRequest:[NSURLRequest requestWithURL:[NSURL URLWithString:data[@"images"][@"low_resolution"][@"url"]]] placeholderImage:[UIImage imageNamed:@"Default"] success:^(NSURLRequest *request, NSHTTPURLResponse *response, UIImage *image) {
NSLog(@"success");
[generalInstaImage setImage: image];
[weakSelf saveImage:generalInstaImage.image withName:data[@"id"]];
} failure:^(NSURLRequest *request, NSHTTPURLResponse *response, NSError *error) {
NSLog(@"fail");
}];
Run Code Online (Sandbox Code Playgroud)
我尝试编写类似的例子weakSelf.generalInstaImage
,但编译器生成错误而不编译.
objective-c ios objective-c-blocks automatic-ref-counting retain-cycle
免责声明:这是一个很长的帖子,但对于那些使用新的ObjectiveC JavascriptCore框架并在ObjC和JS之间进行异步编码的人来说,这可能非常有价值.
大家好,我是Objective C的新手,我正在将一个javascript通信库集成到我的iOS应用程序中.
无论如何,我一直在尝试使用iOS7中引入的新ObjectiveC JavaScriptCore框架.它在很大程度上非常棒,尽管到目前为止记录很差.
混合语言惯例真的很奇怪,但在某些方面也有点解放.
我应该补充一点,我当然正在使用ARC,因此这有助于从Javascript世界中获得很多.但是在ObjectiveC和JSContext callBacks之间移动时,我有一个非常具体的内存使用问题.就像我在Javascript中执行一个函数,然后执行一些异步代码,然后回调到一个定义的ObjectiveC块,然后调用一个定义的JS回调......我只是想确保我做得对(即一些地方没有泄漏记忆)!
只是为了做正确的事情(因为我引用了一个类self
来调用ObjectiveC callBacks我创建了一个weakSelf
所以它对ARC很好用(从问题中引用:在这个块中强烈捕获self可能会导致保留周期):
__unsafe_unretained typeof(self) weakSelf = self;
Run Code Online (Sandbox Code Playgroud)
现在,说我有一个JSContext
并添加一个功能.我希望这个函数接受一个callBack函数并以"Hello"作为参数调用它,并将另一个函数作为callBack传递.即.
// Add a new JSContext.
JSContext context = [[JSContext alloc] initWithVirtualMachine:[[JSVirtualMachine alloc] init]];
// Add a function to the context. This function takes a callBack function and calls it back with "Hello"
[context evaluateScript: @"var functionA = function(callBack){
var aMessage = "Foo";
callBack(aMessage, function(message){
/* message should say: Foo Bar */
});
}" …
Run Code Online (Sandbox Code Playgroud) objective-c objective-c-blocks automatic-ref-counting javascriptcore ios7
我正在尝试使用Objective-C编辑Reachability块中的变量,这是代码:
- (void)testInternetConnection
{
internetReachableFoo = [Reachability reachabilityWithHostname:@"www.google.com"];
// Internet is reachable
internetReachableFoo.reachableBlock = ^(Reachability*reach)
{
// Update the UI on the main thread
dispatch_async(dispatch_get_main_queue(), ^{
NSLog(@"Connessione ad Internet disponibile");
checkConnection = YES;
if(!lastConnectionState)
{
lastConnectionState = YES;
if(doItemsDownload)
[self displayChoice];
}
});
};
// Internet is not reachable
internetReachableFoo.unreachableBlock = ^(Reachability*reach)
{
// Update the UI on the main thread
dispatch_async(dispatch_get_main_queue(), ^{
NSLog(@"Connessione ad Internet non disponibile");
checkConnection = NO;
lastConnectionState = NO;
});
};
[internetReachableFoo startNotifier];
}
Run Code Online (Sandbox Code Playgroud)
凡 …