react-native/React/Views/RCTRefreshControl.m
Janic Duplessis 671b975d92 Fix a bug with ListView with sticky headers + RefreshControl
Summary:The bug is caused by a weird race condition. What happens is that when calling `UIRefreshControl#endRefreshing` the `UIScrollView` delegate `scrollViewDidScroll` function is called synchronously and then `dockClosestSectionHeader` crashes because the sticky header indexes are updated but not the contentView children.

I fixed it by adding an updating property on `RCTRefreshControl` and setting it before calling `endRefreshing` so we can know not to call `dockClosestSectionHeader` at that moment.

Tested with both `RefreshControl` and `onRefreshStart` prop.

I reproduced the bug by replacing ListViewExample.js in UIExplorer with https://gist.github.com/janicduplessis/05fc58e852f3e80e51b9

Fixes #5440

cc nicklockwood
Closes https://github.com/facebook/react-native/pull/5445

Differential Revision: D2953984

Pulled By: nicklockwood

fb-gh-sync-id: c17a6a75ab31ef54d478706ed17a8115a11d734e
shipit-source-id: c17a6a75ab31ef54d478706ed17a8115a11d734e
2016-02-19 05:55:36 -08:00

100 lines
2.6 KiB
Objective-C

/**
* Copyright (c) 2015-present, Facebook, Inc.
* All rights reserved.
*
* This source code is licensed under the BSD-style license found in the
* LICENSE file in the root directory of this source tree. An additional grant
* of patent rights can be found in the PATENTS file in the same directory.
*/
#import "RCTRefreshControl.h"
#import "RCTUtils.h"
@implementation RCTRefreshControl {
BOOL _initialRefreshingState;
BOOL _isInitialRender;
}
- (instancetype)init
{
if ((self = [super init])) {
[self addTarget:self action:@selector(refreshControlValueChanged) forControlEvents:UIControlEventValueChanged];
_isInitialRender = true;
}
return self;
}
RCT_NOT_IMPLEMENTED(- (instancetype)initWithCoder:(NSCoder *)aDecoder)
- (void)layoutSubviews
{
[super layoutSubviews];
// If the control is refreshing when mounted we need to call
// beginRefreshing in layoutSubview or it doesn't work.
if (_isInitialRender && _initialRefreshingState) {
[self beginRefreshing];
}
_isInitialRender = false;
}
- (void)beginRefreshing
{
// When using begin refreshing we need to adjust the ScrollView content offset manually.
UIScrollView *scrollView = (UIScrollView *)self.superview;
CGPoint offset = {scrollView.contentOffset.x, scrollView.contentOffset.y - self.frame.size.height};
// Don't animate when the prop is set initialy.
if (_isInitialRender) {
scrollView.contentOffset = offset;
[super beginRefreshing];
} else {
// `beginRefreshing` must be called after the animation is done. This is why it is impossible
// to use `setContentOffset` with `animated:YES`.
[UIView animateWithDuration:0.25
delay:0
options:UIViewAnimationOptionBeginFromCurrentState
animations:^(void) {
[scrollView setContentOffset:offset];
} completion:^(__unused BOOL finished) {
[super beginRefreshing];
}];
}
}
- (NSString *)title
{
return self.attributedTitle.string;
}
- (void)setTitle:(NSString *)title
{
self.attributedTitle = [[NSAttributedString alloc] initWithString:title];
}
- (void)setRefreshing:(BOOL)refreshing
{
if (self.refreshing != refreshing) {
if (refreshing) {
// If it is the initial render, beginRefreshing will get called
// in layoutSubviews.
if (_isInitialRender) {
_initialRefreshingState = refreshing;
} else {
[self beginRefreshing];
}
} else {
[self endRefreshing];
}
}
}
- (void)refreshControlValueChanged
{
if (_onRefresh) {
_onRefresh(nil);
}
}
@end