草庐IT

ios - block 不捕获 self

coder 2024-01-18 原文

我想尝试理解 block 捕获逻辑,现在我对此有疑问。我有 MeRequestNSNumber 属性。

@property (nonatomic) MeRequest *request;
@property (nonatomic) NSNumber *number; 

然后,在 viewDidLoad 中调用请求方法

self.request = [[MeRequest alloc] init];
[self.request meInfoSuccessBlock:^(NSDictionary *response) {

} failureBlock:^(Error *error) {
    self.number = @5;
}];

- (void)meInfoSuccessBlock:(RequestSuccessBlock)success failureBlock:(RequestFailureBlock)failure {

    self.method = @"GET";
    self.parameters = @{};
    [self performWithCompletion:^(id responseObject) {
        NSDictionary *response = (NSDictionary *)responseObject;
        if (success) {
            success(response);
        }
    } onFailure:^(Error *error) {
        if (failure) {
            failure(error);
        }
    }];
}

- (AFHTTPRequestOperation *)performWithCompletion:(void(^)(id responseObject))completion
                                        onFailure:(void(^)(Error *error))failure {

    NSURLRequest *request = [[NetworkManager sharedManager] requestWithMethod:self.method path:self.path parameters:self.parameters];

    if (_operation) {
        [_operation cancel];
    }

    _operation = [[NetworkManager sharedManager] HTTPRequestOperationWithRequest:request success:^(AFHTTPRequestOperation *operation, id responseObject) {
        _operation = nil;
        dispatch_semaphore_signal(_semaphore);

        if (completion) {
            completion(responseObject);
        }
    } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
        _operation = nil;
        dispatch_semaphore_signal(_semaphore);
        if (failure) {
            failure(_error);
        }
    }];

    [_operation start];
    return _operation;
}

failureBlock 中,我将数字设置为属性。当我离开这个 Controller 时,我在控制台中看到 dealloc 消息,该 Controller 已被释放。

- (void)dealloc {
    NSLog(@"%s", __PRETTY_FUNCTION__);
}

为什么 Controller 释放内存?我不使用对 self

的弱引用

最佳答案

要明确了解,您必须发布 MeRequest 类的实现。

在不知道这一点的情况下,这是一个有根据的猜测。

通过 meInfoSuccessBlock:failureBlock: 传递到 self.request 的 block 可能会在交易完成时被清空。也就是说,它可能是这样的:

- (void)meInfoSuccessBlock:... sB failureBlock:... fB {
   _sB = sB; // put ref in ivar
   _fB = fB; // because this is probably broken up across methods
   dispatch_async(_queue, ^{
        .... think hard ...
        if (success) _sB(...);
        else _fB(...);
        _sB = nil;
        _fB = nil;
   };
}

因此,首先,您不是在创建直接循环引用,而是——也许——self -> request -> _sB -> self 的循环引用。其次,通过在计算完成并进行回调后分配 _sB = nil,循环被打破。


或者,在您的情况下,您对仅在范围内存活的 block 具有强引用。 IE。有点像这样:

- (void)meInfoSuccessBlock:... sB failureBlock:... fB {
   dispatch_async(_queue, ^{
        .... think hard ...
        if (success) sB(...);
        else fB(...);
        // when the block finishes execution, fB and sB will be released
   };
   // when execution gets to here, the block above is the only strong references to sB and fB
}

也就是说,虽然您有一个保留周期,但该周期中的一个引用明确与回调 block 的生命周期相关联,并且由于它们仅在回调完成之前存在,因此它们会被销毁并破坏循环。

关于ios - block 不捕获 self ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34555049/

有关ios - block 不捕获 self的更多相关文章

  1. ruby - RSpec - 使用测试替身作为 block 参数 - 2

    我有一些Ruby代码,如下所示:Something.createdo|x|x.foo=barend我想编写一个测试,它使用double代替block参数x,这样我就可以调用:x_double.should_receive(:foo).with("whatever").这可能吗? 最佳答案 specify'something'dox=doublex.should_receive(:foo=).with("whatever")Something.should_receive(:create).and_yield(x)#callthere

  2. ruby-on-rails - Enumerator.new 如何处理已通过的 block ? - 2

    我在理解Enumerator.new方法的工作原理时遇到了一些困难。假设文档中的示例:fib=Enumerator.newdo|y|a=b=1loopdoy[1,1,2,3,5,8,13,21,34,55]循环中断条件在哪里,它如何知道循环应该迭代多少次(因为它没有任何明确的中断条件并且看起来像无限循环)? 最佳答案 Enumerator使用Fibers在内部。您的示例等效于:require'fiber'fiber=Fiber.newdoa=b=1loopdoFiber.yieldaa,b=b,a+bendend10.times.m

  3. ruby - 如何验证 IO.copy_stream 是否成功 - 2

    这里有一个很好的答案解释了如何在Ruby中下载文件而不将其加载到内存中:https://stackoverflow.com/a/29743394/4852737require'open-uri'download=open('http://example.com/image.png')IO.copy_stream(download,'~/image.png')我如何验证下载文件的IO.copy_stream调用是否真的成功——这意味着下载的文件与我打算下载的文件完全相同,而不是下载一半的损坏文件?documentation说IO.copy_stream返回它复制的字节数,但是当我还没有下

  4. Ruby 文件 IO 定界符? - 2

    我正在尝试解析一个文本文件,该文件每行包含可变数量的单词和数字,如下所示:foo4.500bar3.001.33foobar如何读取由空格而不是换行符分隔的文件?有什么方法可以设置File("file.txt").foreach方法以使用空格而不是换行符作为分隔符? 最佳答案 接受的答案将slurp文件,这可能是大文本文件的问题。更好的解决方案是IO.foreach.它是惯用的,将按字符流式传输文件:File.foreach(filename,""){|string|putsstring}包含“thisisanexample”结果的

  5. ruby - 在匿名 block 中产生 - 2

    我没有理解以下行为(另请参阅inthisSOthread):defdef_testputs'def_test.in'yieldifblock_given?puts'def_test.out'enddef_testdoputs'def_testok'endblock_test=procdo|&block|puts'block_test.in'block.callifblockputs'block_test.out'endblock_test.calldoputs'block_test'endproc_test=procdoputs'proc_test.in'yieldifblock_gi

  6. Get https://registry-1.docker.io/v2/: net/http: request canceled while waiting - 2

    1.错误信息:Errorresponsefromdaemon:Gethttps://registry-1.docker.io/v2/:net/http:requestcanceledwhilewaitingforconnection(Client.Timeoutexceededwhileawaitingheaders)或者:Errorresponsefromdaemon:Gethttps://registry-1.docker.io/v2/:net/http:TLShandshaketimeout2.报错原因:docker使用的镜像网址默认为国外,下载容易超时,需要修改成国内镜像地址(首先阿里

  7. ruby - Ruby 中的单 block AES 解密 - 2

    我需要尝试一些AES片段。我有一些密文c和一个keyk。密文已使用AES-CBC加密,并在前面加上IV。不存在填充,纯文本的长度是16的倍数。所以我这样做:aes=OpenSSL::Cipher::Cipher.new("AES-128-CCB")aes.decryptaes.key=kaes.iv=c[0..15]aes.update(c[16..63])+aes.final它工作得很好。现在我需要手动执行CBC模式,所以我需要单个block的“普通”AES解密。我正在尝试这个:aes=OpenSSL::Cipher::Cipher.new("AES-128-ECB")aes.dec

  8. ruby - 如何让Ruby捕获线程中的语法错误 - 2

    我正在尝试使用ruby​​编写一个双线程客户端,一个线程从套接字读取数据并将其打印出来,另一个线程读取本地数据并将其发送到远程服务器。我发现的问题是Ruby似乎无法捕获线程内的错误,这是一个示例:#!/usr/bin/rubyThread.new{loop{$stdout.puts"hi"abc.putsefsleep1}}loop{sleep1}显然,如果我在线程外键入abc.putsef,代码将永远不会运行,因为Ruby将报告“undefinedvariableabc”。但是,如果它在一个线程内,则没有错误报告。我的问题是,如何让Ruby捕获这样的错误?或者至少,报告线程中的错误?

  9. ruby-on-rails - 无法在 Rails 助手中捕获 block 的输出 - 2

    我在使用自定义RailsFormBuilder时遇到了问题,从昨天晚上开始我就发疯了。基本上我想对我的构建器方法之一有一个可选block,以便我可以在我的主要content_tag中显示其他内容。:defform_field(method,&block)content_tag(:div,class:'field')doconcatlabel(method,"Label#{method}")concattext_field(method)capture(&block)ifblock_given?endend当我在我的一个Slim模板中调用该方法时,如下所示:=f.form_field:e

  10. ruby - 具有两个参数的 block - 2

    我从用户Hirolau那里找到了这段代码:defsum_to_n?(a,n)a.combination(2).find{|x,y|x+y==n}enda=[1,2,3,4,5]sum_to_n?(a,9)#=>[4,5]sum_to_n?(a,11)#=>nil我如何知道何时可以将两个参数发送到预定义方法(如find)?我不清楚,因为有时它不起作用。这是重新定义的东西吗? 最佳答案 如果您查看Enumerable#find的文档,您会发现它只接受一个block参数。您可以将它发送两次的原因是因为Ruby可以方便地让您根据它的“并行赋

随机推荐