【发布时间】:2018-12-25 16:29:52
【问题描述】:
我的应用程序从服务器获取数百个项目,然后将这些数据加载到平面列表 5 项目 x 5 项目 onEndReached 中。但是,当我尝试滚动几十个项目时,我的应用不再响应 onPress(在平面列表之外),并且内存使用量开始激增导致崩溃:
Click here to see image of memory usage
我尝试了一切,从使用 getItemLayout、PureComponent、maxToRenderPerBatch、windowSize,我从我的项目中删除了所有图像,所有图像都是可触摸的,但仍然无法解决这个问题。
我当然是在发布模式下运行我的应用程序。
我不知道如何解决这个问题,因为它只发生在 iOS 上,而且我对 iOS 开发人员知之甚少(我是一个 android 人......)。
这是我的商品的代码:
import React, { PureComponent } from 'react';
import {
View, Text, TouchableOpacity, StyleSheet, Image, ImageBackground, FlatList
} from 'react-native';
import globalStyle from '../globalStyle';
import MaterialCommunityIcon from 'react-native-vector-icons/MaterialCommunityIcons';
import consts from '../utils/consts';
import { formatDistance, formatPriceRange, parseTime, formatTime } from '../utils/utils';
import ItemOffer from './ItemOffer';
import Offer, { STATE_ENUM } from '../entities/Offer';
import LinearGradient from 'react-native-linear-gradient';
import FontAwesomeIcon from 'react-native-vector-icons/FontAwesome';
import { AppEventsLogger } from 'react-native-fbsdk';
export default class ItemShop extends PureComponent {
constructor(props) {
super(props);
this.state = {
shopIdx: this.props.shopIdx,
liked: this.props.liked,
selectedOfferIdx: 0,
currentOffer: this.props.offers[0],
offers: this.props.offers
};
}
render() {
return (
<View style={{marginTop: 5, marginBottom: 10,
alignItems:"stretch", marginLeft: 10,
marginRight: 10, overflow: "hidden", backgroundColor: "transparent", height: 300}}>
<TouchableOpacity
onPress={() => {
this.props.onPressShop(this.state.shopIdx);
}}
>
<View
style={{flexDirection: "row", justifyContent: "space-between", alignItems:"center", height: 50, backgroundColor: globalStyle.colors.bg}}>
<View>
<Text style={[globalStyle.style.h1, {fontFamily: globalStyle.fonts.AvertaBold}]}>{this.props.name}</Text>
<Text style={globalStyle.style.p}>{this.props.food} - {formatPriceRange(this.props.price)} </Text>
</View>
<View style={{flexDirection: "row", alignItems:"center"}}>
<Text style={[globalStyle.style.p, {marginRight: 5}]}>{formatDistance(this.props.distance)}</Text>
<MaterialCommunityIcon name={'map-marker-radius'} size={20} color={globalStyle.colors.darkGray} />
</View>
</View>
</TouchableOpacity>
</View>
);
}
}
我的平面列表的代码:
<FlatList
ref={(ref)=> this.shopList = ref }
data={this.state.shops}
initialNumToRender={4}
onScrollBeginDrag={() => {
this.setState({dragging: true});
}}
onScrollEndDrag={() => {
this.setState({dragging: false});
}}
getItemLayout={(data, index) => (
{length: 315, offset: 315 * index, index}
)}
onEndReached={this.loadMore}
renderItem={this.renderItem}
refreshControl={
<RefreshControl
refreshing={Platform.OS === 'ios' ? false : this.state.loading}
onRefresh={this.getShops}
tintColor={Platform.OS === 'ios' ? "transparent" : globalStyle.colors.orange}
/>
}
/>
如果需要,还有我的 renderItem 函数的代码:
renderItem = ({item, index}) => (
<ItemShop
onPressShop={this.onPressShop}
onPressWiniit={this.toggleModal}
onPressLike={this.onPressLike}
distance={item.distance}
shopIdx={index}
name={item.NAME}
food={item.FOOD}
price={item.PRICE_RANGE}
offers={item.OFFERS}
liked={item.liked}
/>
);
但是,如果将我的 ItemShop 渲染函数更改为以下内容,它可以工作:
render() {
return (
<View style={{marginTop: 5, marginBottom: 10,
alignItems:"stretch", marginLeft: 10,
marginRight: 10, overflow: "hidden", backgroundColor: "transparent", height: 300}}>
<Text style={[globalStyle.style.h1, {fontFamily: globalStyle.fonts.AvertaBold}]}>{this.props.name}</Text>
</View>
);
}
我真的可以在这里使用帮助或确认为错误,因为我的截止日期真的很快:(
谢谢!
【问题讨论】:
-
关于内存泄漏和
FlatList的帖子很少。 example。例如,您可以尝试使用removeClippedSubviews,看看是否有帮助 -
@KevinAmiranoff 是的,我已经尝试过 removeClippedSubviews 的真假,它没有改变任何东西:(
-
@HugoLaplace 你找到解决办法了吗?我也有这个问题。
-
@pramjeet 我自己找不到修复程序,所以最终使用了github.com/Flipkart/recyclerlistview,效果很好:)
标签: ios react-native memory out-of-memory react-native-flatlist