2018-12-17 RAC-Memory Management

https://github.com/ReactiveCocoa/ReactiveObjC/blob/master/Documentation/MemoryManagement.md

Memory Management

1:you don't need to retain signals in order to process them.

Subscribers

1:Before going any further, it's worth noting that subscribeNext:error:completed: (and all variants thereof) create an implicit subscriber using the given blocks.

2:Any objects referenced from those blocks will therefore be retained as part of the subscription

3: Just like any other object, self won't be retained without a direct or indirect reference to it.

Finite or Short-Lived Signals

1:The most important guideline to RAC memory management is that a subscription is automatically terminated upon completion or error, and the subscriber removed.

For example

2:However, the RACSignal -> RACSubscriber relationship is torn down as soon as signal finishes, breaking the retain cycle.

3:This is often all you need, because the lifetime of the RACSignal in memory will naturally match the logical lifetime of the event stream.

Infinite Signals

1:Infinite signals (or signals that live so long that they might as well be infinite), however, will never tear down naturally. This is where disposables shine.

2:Disposing of a subscription will remove the associated subscriber, and just generally clean up any resources associated with that subscription.

3:However, as a general rule of thumb, if you have to manually manage a subscription's lifecycle, there's probably a better way to do what you want.

Signals Derived from self

1:This commonly occurs when using RACObserve() on a key path that's relative to self, and then applying a block that needs to capture self.

The easiest answer here is just to capture self weakly:


1
2

The design goal of "no retaining necessary" begs the question:

    how do we know when a signal should be deallocated? 

    What if it was just created, escaped an autorelease pool, and hasn't been retained yet?

Consequently

1:A created signal is automatically added to a global set of active signals.

2:The signal will wait for a single pass of the main run loop, and then remove itself from the active set if it has no subscribers. Unless the signal was retained somehow, it would deallocate at this point.

3:If something did subscribe in that run loop iteration, the signal stays in the set.

4:Later, when all the subscribers are gone, step 2 is triggered again.

©著作权归作者所有,转载或内容合作请联系作者
【社区内容提示】社区部分内容疑似由AI辅助生成,浏览时请结合常识与多方信息审慎甄别。
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

相关阅读更多精彩内容

  • rljs by sennchi Timeline of History Part One The Cognitiv...
    sennchi阅读 12,211评论 0 10
  • **2014真题Directions:Read the following text. Choose the be...
    又是夜半惊坐起阅读 13,618评论 0 23
  • 说点什么吧,最近也容易变得感动了,看到那最深处的人性也会莫名其妙的流下眼泪,虽然那事一点也不跟我相干,一个人独处也...
    阁夜阅读 1,783评论 0 1
  • 感觉时间慢慢变了:小时候,感觉一天的时间那么长,现在却这么短;感觉节奏变了:以前生活是那么的慢,现在不知为什么,显...
    人生就是一本简单的书阅读 1,549评论 0 0
  • 亲爱的小朋友们: 你们好! 我叫福来,已经在1937年8月29日告别了这个世界,现在在天堂。我就是...
    祥颐阅读 1,274评论 0 0

友情链接更多精彩内容