aws-appsync和react-native

时间:2019-05-06 04:05:07

标签: javascript amazon-web-services react-native crash aws-appsync

我正在开发一个移动应用程序,该应用程序需要在我的移动缓存中存储20000条记录。 从多个表中获取数据时,获取功能很棒(获取时无错误)

但是我在尝试更改此数据时遇到的主要问题。 我的应用程序崩溃了,我不知道为什么。 没有错误抛出,而我到处都放置try catch块

请告诉我我对此有一些选择

  1. 这可能是内存问题(我不这么认为,因为我的手机具有3GB内存和32GB ROM的更新手机)
  2. 这可能是解决程序的问题(不,因为我没有更新功能就进行了更新,所以它运行得很好) 请提出一些解决方案,因为我在此问题上花费了更多时间。

数据提取代码

class Assets extends React.PureComponent {
  componentDidUpdate = async (prevProps) => {
    const { nextTokenForAssets, loadingAssets, networkStatusAssets, loadMoreAssets } = this.props
    /**
     * Asset Pagination
     */
    try{
      if (networkStatusAssets === 7 && !loadingAssets && nextTokenForAssets !== prevProps.nextTokenForAssets ) {
        if (nextTokenForAssets !== null && nextTokenForAssets !== ' ' ) {
          loadMoreAssets()
        }
      }
    }catch(err){
      alert('error in downloading asset')
    }
  }

  render = () => {
    const { userPlantId, userPackageId, navigation, userId, nextTokenForAssets, loadingAssets, networkStatusAssets } = this.props
    return (
      // null
      <GetPhotos
        userPlantId={userPlantId}
        userPackageId={userPackageId}
        userId={userId}
        navigation={navigation}
        nextTokenForAssets={nextTokenForAssets}
        loadingAssets={loadingAssets}
        networkStatusAssets={networkStatusAssets}
      />
    )
  }
}

const GetAsset = compose(
  graphql(GetAssetsList, {
    name: 'AssetsList',
    options: (ownProps) => {
      return {
        fetchPolicy: 'cache-and-network',
        variables: {
          limit: 100,
          nextToken: ' ',
          PLANT_ID: ownProps.userPlantId ? ownProps.userPlantId : ' ',
          PACKAGE_ID: ownProps.userPackageId ? ownProps.userPackageId : ' '
        }
      }
    },
    props: ({ ownProps, AssetsList }) => {
      return ({
        listAssets: AssetsList.listASSETS ? AssetsList.listASSETS.items : [],
        nextTokenForAssets: AssetsList.listASSETS ? AssetsList.listASSETS.nextToken : ' ',
        loadingAssets: AssetsList.loading,
        networkStatusAssets: AssetsList.networkStatus,
        errorInAssetList: AssetsList.error,
        loadMoreAssets: () => {
          AssetsList.fetchMore({
            variables: {
              limit: 100,
              nextToken: AssetsList.listASSETS ? AssetsList.listASSETS.nextToken : ' ',
              PLANT_ID: ownProps.userPlantId ? ownProps.userPlantId : ' ',
              PACKAGE_ID: ownProps.userPackageId ? ownProps.userPackageId : ' '
            },
            updateQuery: (prevResult, { fetchMoreResult }) => {
              const newList = fetchMoreResult.listASSETS ? fetchMoreResult.listASSETS.items :[]
              const prevList = prevResult.listASSETS ? prevResult.listASSETS.items : []
              const newItems = [
                ...newList,
                ...prevList
              ]

              const newData = {
                ...prevResult.listASSETS,
                items: newItems,
                nextToken: fetchMoreResult.listASSETS ? fetchMoreResult.listASSETS.nextToken : ' '
              }
              const finalData = {
                ...prevResult,
                listASSETS: newData
              }
              return finalData
            }
          })
        }
      })
    }
  })
)(Assets)
export default GetAsset

这里是我的突变代码-

import UpdateAsset from '../gqlQueries/UpdateAsset'
import GetAssetsList from '../gqlQueries/GetAssetsList'
import Toast from '../components/Toast'

export default async (asset, client, variables ) => {
  try {
    const result = await client.mutate({
      mutation: UpdateAsset,
      variables: asset,
      update: (cache, { data: { updateASSETS } }) => {
        const query = GetAssetsList
        // Read query from cache
        const data = cache.readQuery({ query, variables })
        // Add newly created item to the cache copy
        let filterData = data.listASSETS.items.find(item => item.ASSETS_ID === updateASSETS.ASSETS_ID)
        let newData = { ...filterData,
          ASSETS_NAME : updateASSETS.ASSETS_NAME,
          ASSETS_SHORT_DESCRIPTION : updateASSETS.ASSETS_SHORT_DESCRIPTION,
          ASSETS_LASTMODIFIEDDATE : updateASSETS.ASSETS_LASTMODIFIEDDATE,
          ASSETS_UNABLE_TO_LOCATE : updateASSETS.ASSETS_UNABLE_TO_LOCATE,
          ASSETS_AUDIT_COMPLETED : updateASSETS.ASSETS_AUDIT_COMPLETED,
          CLASSIFICATION_ID : updateASSETS.CLASSIFICATION_ID,
          ASSETS_PARENT_ASSET_ID : updateASSETS.ASSETS_PARENT_ASSET_ID,
          ASSETS_PARENT_ASSET_NAME : updateASSETS.ASSETS_PARENT_ASSET_NAME,
          ASSETS_LASTMODIFIEDBY_ID : updateASSETS.ASSETS_LASTMODIFIEDBY_ID,
          ASSETS_LASTMODIFIEDBY_NAME : updateASSETS.ASSETS_LASTMODIFIEDBY_NAME,
          ASSETS_LATEST_AUDIT: updateASSETS.ASSETS_LATEST_AUDIT,
          ASSETS_LATEST_AUDITOR: updateASSETS.ASSETS_LATEST_AUDITOR
        }
        data.listASSETS.items = data.listASSETS.items.filter(item => item.ASSETS_ID !== updateASSETS.ASSETS_ID)
        data.listASSETS.items.unshift(newData)
        //Overwrite the cache with the new results
        cache.writeQuery({ query, variables, data })
      },
      optimisticResponse: () => ({
        updateASSETS: { ...asset, __typename: 'ASSETS' }
      }),
    })
  } catch (error) {
    Toast('Error: '+ error)
  }
}

0 个答案:

没有答案