当用户从后台打开我的应用程序时,我遇到了使我的应用程序变慢的问题。查看日志,似乎websocket已断开连接,然后再次连接,这触发了我的实时数据库功能,该功能更新了我的redux存储,并重新呈现了我的视图。这就是让我的应用程序慢下来的原因。
我考虑过使用以下来自react-native的函数来处理我的重新连接和断开连接,但是我认为这不是正确的方法。
AppState.addEventListener('change', this._handleAppStateChange);
这是5-10分钟后我从后台打开应用程序时得到的日志:
c:0:0:0 Websocket connection was disconnected.
util.js:133 c:0:0:0 WebSocket is closing itself
util.js:133 c:0:0: Realtime connection lost.
util.js:133 c:0:0: Closing realtime connection.
util.js:133 c:0:0: Shutting down all connections
util.js:133 p:0: data client disconnected
util.js:133 p:0: Trying to reconnect in 0ms
util.js:133 0: onDisconnectEvents
app.js:150 distanceMi
app.js:151 0
util.js:133 p:0: Making a connection attempt
util.js:133 getToken() completed. Creating connection.
util.js:133 c:0:1: Connection created
RCTLog.js:47 SocketRocket: In debug mode. Allowing connection to any root cert
util.js:133 c:0:1:0 Websocket connecting to wss://s-usc1c-nss-249.firebaseio.com/.ws?v=5&ls==&ns=instabo-production
RCTLog.js:47 SocketRocket: In debug mode. Allowing connection to any root cert
c:0:1:0 Websocket connected.
util.js:133 c:0:1: Realtime connection established.
util.js:133 p:0: connection ready
util.js:133 p:0: {"r":10,"a":"auth","b":{"cred":"token"}}
util.js:133 p:0: Listen on /instavice/variables for default
util.js:133 p:0: {"r":11,"a":"q","b":{"p":"/instavice/variables","h":"gETk+lRA+OQ6CA3WxV1tJp899vk="}}
util.js:133 p:0: Listen on /nurses/IEvkY2X161bf1IMgOMayW2oDq0l2 for default
util.js:133 p:0: {"r":12,"a":"q","b":{"p":"/nurses/nurseID","h":"DZANoj/kEuhff2s4ng1Zie1q/24="}}
util.js:133 p:0: Listen on /offersNurse for default
util.js:133 p:0: {"r":13,"a":"q","b":{"p":"/offersNurse","h":"="}}
util.js:133 p:0: from server: {"r":10,"b":{"s":"ok","d":{"auth":{"email_verified":true,"provider":"password","email":"birolleauf@wlnss.co","user_id":"","token":{"email_verified":true,"email":"birolleauf@wlnss.co","exp":1566869220,"user_id":"","iat":1566865620,"sub":"IEvkY2X161bf1IMgOMayW2oDq0l2","aud":"instabo-production","auth_time":1566865620,"iss":"https://securetoken.google.com/instabo-production","firebase":{"identities":{"email":["@wlnss.co"]},"sign_in_provider":"password"}},"uid":""},"expires":1566869220}}}
util.js:133 c:0:1: Primary connection is healthy.
util.js:133 p:0: from server: {"r":11,"b":{"s":"ok","d":{}}}
util.js:133 p:0: listen response {"s":"ok","d":{}}
util.js:133 p:0: from server: {"r":12,"b":{"s":"ok","d":{}}}
util.js:133 p:0: listen response {"s":"ok","d":{}}
util.js:133 p:0: from server: {"r":13,"b":{"s":"ok","d":{}}}
util.js:133 p:0: listen response {"s":"ok","d":{}}
这是我用来实时获取用户数据和优惠的代码:
const OffersRef = getDatabase().ref('offersNurse')
OffersRef.on('value', snap => {
///// some code
/// update redux store
})
const userRef = getDatabase().ref('nurses/' + userID)
onValueUser = userRef.on('value', function(snap) {
///// some code
/// update redux store
})
我想实现一个功能,该功能可以在应用程序在后台运行时保持我的网络套接字连接,或者可以避免我的实时功能再次被触发。
干杯!