meteorrn / meteor-react-native

Meteor client for React Native matching Meteor Spec
https://guide.meteor.com/react-native.html
Other
59 stars 31 forks source link

UseTracker causes the Phone to heat up extensively #118

Closed lanphammmm closed 1 year ago

lanphammmm commented 1 year ago

Descriptions I use useTracker to listening real-time data when the server update. But the phone heats up quickly when used for a short time (1-2 minutes). I checked and without using useTracker the phone is not as hot as before. This only happens on iOS.

Code

  const subscribeRef = useRef()

  const subscription = Meteor.useTracker(() => {
    if (!subscribeRef.current) {
      const subscriber = Meteor.subscribe(
        'account',
        userId,
      )
      subscribeRef.current = subscriber
    } else {
      if (subscribeRef.current?.ready()) {
        return AccountCollection.find({
          'userId': userId,
        }).fetch()
      }
    }
  })
jankapunkt commented 1 year ago

Hello @l4n2k1 I have an iPad here and could try to reproduce this. In order to reproduce I additionally need:

lanphammmm commented 1 year ago

Hi @jankapunkt I have located it in AppNavigator.tsx and used it normally. After a short time, it will heat up.

UserTracker.ts

const Meteor = meteorCore.getMeteorInstance()
let UserCollection = new Meteor.Mongo.Collection('user')

export const useUserTracker = () => {
  const dispatch = useAppDispatch()
  const subscribeRef = useRef<{ ready: () => boolean } | null>(null)

  const userInfo = useAppSelector(getUserInfo)

  const userSubscription = Meteor.useTracker(() => {
    if (!subscribeRef.current) {
      const subscriber = Meteor.subscribe('userInfo', String(userInfo?._id))
      subscribeRef.current = subscriber
    } else {
      if (subscribeRef.current?.ready()) {
        return UserCollection.find({
          userInfo: userInfo?._id,
        }).fetch()
      }
    }
  })

  useAppState((appState: AppStateStatus) => {
    if (appState === 'background' || appState === 'inactive') {
      subscribeRef.current = null
    }
  })

  useUpdateEffect(() => {
    if (!subscribeRef.current?.ready()) {
      return
    }
    const isDiff = _.differenceWith(userSubscription, userInfo, _.isEqual)

    if (isDiff) {
      dispatch(updateUserInfo(userSubscription))
    }
  }, [userSubscription])
}

Navigator

const AppNavigator = () = {
    useUserTracker()

    return <View/>
}

const Navigation = () => {
  return isLogin ? <AppNavigator /> : <AuthNavigator />
}

Document: https://guide.meteor.com/react-native.html#usage IOS version: iOS 16

jankapunkt commented 1 year ago

@l4n2k1 I forgot to ask something more:

I checked and without using useTracker the phone is not as hot as before

What did you use instead of useTracker then?

lanphammmm commented 1 year ago

Hi @jankapunkt ja I don't use any substitute for it. I just turned it off

github-actions[bot] commented 1 year ago

Closing this issue due to no activity. Feel free to reopen.