3

このエラーを取り除くことはできません。配列から 1 つのアイテムを削除して状態を更新すると発生します。

いくつかのデバッグの後、アプリをリロードし、この画面に直接移動して削除すると、エラーが表示されないことがわかりました。しかし、この画面に移動して戻ってから、この画面に戻って削除すると、エラーが表示されます。画面を 10 回読み込むと、このような (30) エラーが発生します。

私の推測では、ポップルートをダッシュ​​ボードに戻すときにfirebase接続を閉じていないということです。または、使用しているナビゲーターがシーンを正しくアンロードしていません。または、deleteRow() 関数に何か問題があります

他にできることは本当にありません.同じ質問がウェブ全体にありますが、私のシナリオには当てはまらないようです。

  constructor(props) {
    super(props);
    this.state = {
      dataSource: new ListView.DataSource({
        rowHasChanged: (row1, row2) => row1 !== row2,
      }),
    };

    const user = firebase.auth().currentUser;
    if (user != null) {
      this.itemsRef = this.getRef().child(`Stores/${user.uid}/`);
    } else {
      Alert.alert('Error', 'error!');
    }
    this.connectedRef = firebase.database().ref('.info/connected');
  }

  componentWillMount() {
    this.connectedRef.on('value', this.handleValue);
  }

  componentWillReceiveProps() {
    this.connectedRef.on('value', this.handleValue);
  }

  componentWillUnmount() {
    this.connectedRef.off('value', this.handleValue);
  }

  /*eslint-disable */
  getRef() {
    return firebase.database().ref();
  }
  /*eslint-enable */

  handleValue = (snap) => {
    if (snap.val() === true) {
      this.listenForItems(this.itemsRef);
    } else {
      //this.offlineForItems();
    }
  };

  listenForItems(itemsRef) {
    this.itemsRef.on('value', (snap) => {
      const items = [];
      snap.forEach((child) => {
        items.unshift({
          title: child.val().title,
          _key: child.key,
        });
      });
      offline2.save('itemsS', items);
      this.setState({
        dataSource: this.state.dataSource.cloneWithRows(items),
      });
    });
  }

  offlineForItems() {
    offline2.get('itemsS').then((items) => {
      this.setState({
        dataSource: this.state.dataSource.cloneWithRows(items),
      });
    });
  }

  deleteConfirm(data, secId, rowId, rowMap) {
    Alert.alert(
        'Warning!',
        'Are you sure?',
      [
          { text: 'OK', onPress: () => this.deleteRow(data, secId, rowId, rowMap) },
          { text: 'Cancel', onPress: () => this.deleteCancel(data, secId, rowId, rowMap) },
      ]
    );
  }

  deleteCancel(data, secId, rowId, rowMap) {
    rowMap[`${secId}${rowId}`].closeRow();
  }

  deleteRow(data, secId, rowId, rowMap) {
    rowMap[`${secId}${rowId}`].closeRow();
    this.itemsRef.child(data._key).remove();
    offline2.get('itemsS').then((items) => {
      const itemsTemp = items;
      let index = -1;
      for (let i = 0; i < itemsTemp.length; i += 1) {
        if (itemsTemp[i]._key === data._key) {
          index = i;
        }
      }
      if (index > -1) {
        itemsTemp.splice(index, 1);
      }
      // Decrement stores counter
      offline2.get('storesTotal').then((value) => {
        const itemsReduce = value - 1;
        this.setState({ storesValue: itemsReduce });
        offline2.save('storesTotal', itemsReduce);
      });

      offline2.save('itemsS', itemsTemp);
      this.setState({
        dataSource: this.state.dataSource.cloneWithRows(itemsTemp),
      });
    });
  }
4

3 に答える 3

2

問題は彼らが言及したものでしたが、解決策は提供されていません。しばらくして外部の助けを借りて、これで問題が解決しました。リスナーを削除するだけです。

  componentWillUnmount() {
    this.itemsRef.off(); // this line
    this.connectedRef.off('value', this.handleValue);
  }
于 2016-12-12T12:06:33.410 に答える
1

エラーが発生しているかどうかはわかりませんが、これはエラーです。イベント ハンドラーを、できればコンストラクターでバインドする必要があります。バインドを解除する (イベント リスナーを削除する) 必要があるため、同じ関数を参照する必要があります。 .

constructor(props) {
    super(props);
    this.state = {
      dataSource: new ListView.DataSource({
        rowHasChanged: (row1, row2) => row1 !== row2,
      }),
    };

    const user = firebase.auth().currentUser;
    if (user != null) {
      this.itemsRef = this.getRef().child(`Stores/${user.uid}/`);
    } else {
      Alert.alert('Error', 'error!');
    }
    this.connectedRef = firebase.database().ref('.info/connected');
    this.handleValue = this.handleValue.bind(this) // here added
  }

それがうまくいくことを願って、あなたの問題も解決します。

于 2016-12-12T04:00:02.537 に答える