Explain __weak and __strong usage reasons in SDWebImage code

后端 未结 2 1888
灰色年华
灰色年华 2020-12-30 15:57

I think I understand strong and weak keywords well, but I don\'t understand how it\'s used in the code below. This code is from SDWebImage by Olivier Poitrey available on g

2条回答
  •  甜味超标
    2020-12-30 16:34

    The downloadWithUrl: method might take a long time. In that time, the user might decide to navigate away, eliminating the need for the SDWebImage object. To facilitate early cleanup of the object, the outer self reference is weak. This way, downloadWithUrl won't prevent the SDWebImage from being deallocated.

    Of course, if you actually want to work with self, you need a strong reference. So, the on-completion block of downloadWithUrl grabs a strong reference to self. If the object goes away in this time, sself will be nil. Otherwise, it will be a valid strong reference, indicating that the SDWebImage object is still around, and the object will finish its work at this time.

提交回复
热议问题