Adding two cents on improving precision and style. In most cases you will only use one or a couple of members of self
in this block, most likely just to update a slider. Casting self
is overkill. Instead, it's better to be explicit and cast only the objects that you truly need inside the block. For example, if it's an instance of UISlider*
, say, _timeSlider
, just do the following before the block declaration:
UISlider* __weak slider = _timeSlider;
Then just use slider
inside the block. Technically this is more precise as it narrows down the potential retain cycle to only the object that you need, not all the objects inside self
.
Full example:
UISlider* __weak slider = _timeSlider;
[_embeddedPlayer addPeriodicTimeObserverForInterval:CMTimeMake(1, 1)
queue:nil
usingBlock:^(CMTime time){
slider.value = time.value/time.timescale;
}
];
Additionally, most likely the object being cast to a weak pointer is already a weak pointer inside self
as well minimizing or eliminating completely the likelihood of a retain cycle. In the example above, _timeSlider
is actually a property stored as a weak reference, e.g:
@property (nonatomic, weak) IBOutlet UISlider* timeSlider;
In terms of coding style, as with C and C++, variable declarations are better read from right to left. Declaring SomeType* __weak variable
in this order reads more naturally from right to left as: variable is a weak pointer to SomeType
.