菜鸟教程小白 发表于 2022-12-11 20:22:38

ios - 仅来自平面列表 iOS 的 React-Native 巨大内存使用和崩溃


                                            <p><p>我的应用程序从服务器获取数百个项目,然后将这些数据加载到平面列表 5 项目 x 5 项目 onEndReached 中。但是,当我尝试滚动几十个项目时,我的应用不再响应 onPress(在平面列表之外),并且内存使用量开始激增导致崩溃:</p>

<p> <a href="/image/C6mJo.png" rel="noreferrer noopener nofollow">Click here to see image of memory usage</a> </p>

<p>我尝试了所有方法,从使用 getItemLayout、PureComponent、maxToRenderPerBatch、windowSize,我从我的项目中删除了所有图像,所有图像都是可触摸的,但仍然无法解决这个问题。</p>

<p>我当然是在 Release模式下运行我的应用程序。</p>

<p>我不知道如何解决这个问题,因为它只发生在 iOS 上,而且我对 iOS 开发人员知之甚少(我是一个 android 人......)。</p>

<p>这是我的商品的代码:</p>

<pre><code>import React, { PureComponent } from &#39;react&#39;;
import {
View, Text, TouchableOpacity, StyleSheet, Image, ImageBackground, FlatList
} from &#39;react-native&#39;;
import globalStyle from &#39;../globalStyle&#39;;
import MaterialCommunityIcon from &#39;react-native-vector-icons/MaterialCommunityIcons&#39;;
import consts from &#39;../utils/consts&#39;;
import { formatDistance, formatPriceRange, parseTime, formatTime } from &#39;../utils/utils&#39;;
import ItemOffer from &#39;./ItemOffer&#39;;
import Offer, { STATE_ENUM } from &#39;../entities/Offer&#39;;
import LinearGradient from &#39;react-native-linear-gradient&#39;;
import FontAwesomeIcon from &#39;react-native-vector-icons/FontAwesome&#39;;
import { AppEventsLogger } from &#39;react-native-fbsdk&#39;;


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,
   offers: this.props.offers
   };
}



render() {
    return (
      &lt;View style={{marginTop: 5, marginBottom: 10,
      alignItems:&#34;stretch&#34;, marginLeft: 10,
      marginRight: 10, overflow: &#34;hidden&#34;, backgroundColor: &#34;transparent&#34;, height: 300}}&gt;
      &lt;TouchableOpacity
      onPress={() =&gt; {
          this.props.onPressShop(this.state.shopIdx);
      }}
      &gt;
          &lt;View
          style={{flexDirection: &#34;row&#34;, justifyContent: &#34;space-between&#34;, alignItems:&#34;center&#34;, height: 50, backgroundColor: globalStyle.colors.bg}}&gt;
            &lt;View&gt;
            &lt;Text style={}&gt;{this.props.name}&lt;/Text&gt;
            &lt;Text style={globalStyle.style.p}&gt;{this.props.food} - {formatPriceRange(this.props.price)} &lt;/Text&gt;
            &lt;/View&gt;
            &lt;View style={{flexDirection: &#34;row&#34;, alignItems:&#34;center&#34;}}&gt;
            &lt;Text style={}&gt;{formatDistance(this.props.distance)}&lt;/Text&gt;
            &lt;MaterialCommunityIcon name={&#39;map-marker-radius&#39;} size={20} color={globalStyle.colors.darkGray} /&gt;
            &lt;/View&gt;
          &lt;/View&gt;
      &lt;/TouchableOpacity&gt;
      &lt;/View&gt;
    );
}
}
</code></pre>

<p>我的平面列表的代码:</p>

<pre><code>    &lt;FlatList
      ref={(ref)=&gt; this.shopList = ref }
      data={this.state.shops}
      initialNumToRender={4}
      onScrollBeginDrag={() =&gt; {
      this.setState({dragging: true});
      }}
      onScrollEndDrag={() =&gt; {
      this.setState({dragging: false});
      }}
      getItemLayout={(data, index) =&gt; (
      {length: 315, offset: 315 * index, index}
      )}
      onEndReached={this.loadMore}
      renderItem={this.renderItem}
      refreshControl={
      &lt;RefreshControl
            refreshing={Platform.OS === &#39;ios&#39; ? false : this.state.loading}
            onRefresh={this.getShops}
            tintColor={Platform.OS === &#39;ios&#39; ? &#34;transparent&#34; : globalStyle.colors.orange}
         /&gt;
      }
    /&gt;
</code></pre>

<p>如果需要,还有我的 renderItem 函数的代码:</p>

<pre><code>renderItem = ({item, index}) =&gt; (
    &lt;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}
    /&gt;
);
</code></pre>

<p>但是,如果将我的 ItemShop 渲染函数更改为以下内容,它可以工作:</p>

<pre><code>render() {
    return (
      &lt;View style={{marginTop: 5, marginBottom: 10,
      alignItems:&#34;stretch&#34;, marginLeft: 10,
      marginRight: 10, overflow: &#34;hidden&#34;, backgroundColor: &#34;transparent&#34;, height: 300}}&gt;

            &lt;Text style={}&gt;{this.props.name}&lt;/Text&gt;

      &lt;/View&gt;
    );
}
</code></pre>

<p>我真的可以在这里使用帮助或确认为错误,因为我的截止日期真的很快:(</p>

<p>谢谢!</p></p>
                                    <br><hr><h1><strong>Best Answer-推荐答案</ strong></h1><br>
                                            <p><p>我认为您可以尝试以下步骤:
1.使用调试器工具分析内存堆,并尝试找出堆中保留的内容
2.为你的flatlist指定一个静态<code>keyExtractor</code>(item.id or item.name)来提高性能</p></p>
                                   
                                                <p style="font-size: 20px;">关于ios - 仅来自平面列表 iOS 的 React-Native 巨大内存使用和崩溃,我们在Stack Overflow上找到一个类似的问题:
                                                        <a href="https://stackoverflow.com/questions/51390389/" rel="noreferrer noopener nofollow" style="color: red;">
                                                                https://stackoverflow.com/questions/51390389/
                                                        </a>
                                                </p>
                                       
页: [1]
查看完整版本: ios - 仅来自平面列表 iOS 的 React-Native 巨大内存使用和崩溃