首页 > 解决方案 > Firestore 和 Realtime DB 重新连接后响应时间过长

问题描述

关闭wifi并立即再次打开后,第一次尝试从firestore获取数据需要太长时间,导致保存到变量时为空值。Firebase 实时数据库在第一次尝试写入时也需要大约 40 秒的时间来响应。在随后的每次读取、写入和删除时,它几乎都是瞬间完成的。

以下是我的 gradle 实现

有什么方法可以让第一次读写更快?


日志错误

D/WebSocket: ws_0 - WebSocket error.
com.google.firebase.database.tubesock.WebSocketException: IO Exception
    at com.google.firebase.database.tubesock.WebSocketWriter.runWriter(com.google.firebase:firebase-database@@16.0.5:159)
    at com.google.firebase.database.tubesock.WebSocketWriter.access$000(com.google.firebase:firebase-database@@16.0.5:30)
    at com.google.firebase.database.tubesock.WebSocketWriter$1.run(com.google.firebase:firebase-database@@16.0.5:47)
    at java.lang.Thread.run(Thread.java:818)
 Caused by: java.net.SocketException: Socket is closed
    at com.android.org.conscrypt.OpenSSLSocketImpl.checkOpen(OpenSSLSocketImpl.java:247)
    at com.android.org.conscrypt.OpenSSLSocketImpl.-wrap0(OpenSSLSocketImpl.java)
    at com.android.org.conscrypt.OpenSSLSocketImpl$SSLOutputStream.write(OpenSSLSocketImpl.java:756)
    at java.nio.channels.Channels$OutputStreamChannel.write(Channels.java:348)
    at com.google.firebase.database.tubesock.WebSocketWriter.writeMessage(com.google.firebase:firebase-database@@16.0.5:138)
    at com.google.firebase.database.tubesock.WebSocketWriter.runWriter(com.google.firebase:firebase-database@@16.0.5:152)
    at com.google.firebase.database.tubesock.WebSocketWriter.access$000(com.google.firebase:firebase-database@@16.0.5:30) 
    at com.google.firebase.database.tubesock.WebSocketWriter$1.run(com.google.firebase:firebase-database@@16.0.5:47) 
    at java.lang.Thread.run(Thread.java:818) 

D/PersistentConnection: pc_0 - Successfully fetched token, opening connection
D/Connection: conn_1 - Opening a connection
D/EventRaiser: Raising /ce1b4691-84d6-434b-b12d-9341bcc25b3b: CHILD_REMOVED: { Confirmation: start }
D/EventRaiser: Raising /ce1b4691-84d6-434b-b12d-9341bcc25b3b: CHILD_REMOVED: { Connection: 71 }
Raising /ce1b4691-84d6-434b-b12d-9341bcc25b3b: CHILD_REMOVED: { Pattern: KxE5LORvglndgvTu/ubCDfcx7XD0BklxNAQg5OMvaJmm6DwLwcHr+/hXumI3SaAJlxrES6uhwSTmJracG2jpG7tFMKqCw0+DdSUwUl9cZ8StIvY3TfDbDr5gw+ttJKQa8UAzdZH2RDkIM4rRPLIOVsEZUupYaAMmpZFZBLmHJ6c= }
Raising /ce1b4691-84d6-434b-b12d-9341bcc25b3b: CHILD_REMOVED: { Pattern_2: NONE }
Raising /ce1b4691-84d6-434b-b12d-9341bcc25b3b: CHILD_REMOVED: { Time: 10:00 }
D/WebSocket: ws_1 - websocket opened
ws_1 - Reset keepAlive
D/WebSocket: ws_1 - ws message: {"t":"c","d":{"t":"h","d":{"ts":1546755382835,"v":"5","h":"s-usc1c-nss-234.firebaseio.com","s":"ZZNBMUWEuz0kWNZY9SzSZSBiucb6SVL4"}}}
D/WebSocket: ws_1 - Reset keepAlive. Remaining: 44997
D/WebSocket: ws_1 - HandleNewFrameCount: 1
s_1 - handleIncomingFrame complete frame: {d={d={v=5, h=s-usc1c-nss-234.firebaseio.com, ts=1546755382835, s=ZZNBMUWEuz0kWNZY9SzSZSBiucb6SVL4}, t=h}, t=c}
D/Connection: conn_1 - Got control message: {d={v=5, h=s-usc1c-nss-234.firebaseio.com, ts=1546755382835, s=ZZNBMUWEuz0kWNZY9SzSZSBiucb6SVL4}, t=h}
conn_1 - realtime connection established
D/PersistentConnection: pc_0 - onReady
   pc_0 - handling timestamp
   pc_0 - calling restore state
   pc_0 - Restoring auth.
/Connection: conn_1 - Sending data (contents hidden)
D/WebSocket: ws_1 - Reset keepAlive. Remaining: 44999
D/WebSocket: ws_1 - ws message: {"t":"d","d":{"r":16,"b":{"s":"ok","d":{"auth":{"email_verified":true,"provider":"password","email":"tp37@mail.apu.edu.my","user_id":"VjYBKLV8tIRlgFNXBzt0pZQz7gH2","token":{"email_verified":true,"email":"tp37@mail.apu.edu.my","exp":1546758210,"user_id":"VjYBKLV8tIRlgFNXBzt0pZQz7gH2","staff":true,"iat":1546754610,"sub":"VjYBKLV8tIRlgFNXBzt0pZQz7gH2","aud":"postal-system","auth_time":1546618590,"iss":"https://securetoken.google.com/postal-system","firebase":{"identities":{"email":["tp37@mail.apu.edu.my"]},"sign_in_provider":"password"}},"uid":"VjYBKLV8tIRlgFNXBzt0pZQz7gH2","staff":true},"expires":1546758210}}}}
D/WebSocket: ws_1 - Reset keepAlive. Remaining: 44795
ws_1 - HandleNewFrameCount: 1
D/WebSocket: ws_1 - handleIncomingFrame complete frame: {d={r=16, b={d={expires=1546758210, auth={email=tp37@mail.apu.edu.my, uid=VjYBKLV8tIRlgFNXBzt0pZQz7gH2, staff=true, token={sub=VjYBKLV8tIRlgFNXBzt0pZQz7gH2, iss=https://securetoken.google.com/postal-system, aud=postal-system, firebase={sign_in_provider=password, identities={email=[tp37@mail.apu.edu.my]}}, email=tp37@mail.apu.edu.my, auth_time=1546618590, iat=1546754610, staff=true, exp=1546758210, user_id=VjYBKLV8tIRlgFNXBzt0pZQz7gH2, email_verified=true}, user_id=VjYBKLV8tIRlgFNXBzt0pZQz7gH2, provider=password, email_verified=true}}, s=ok}}, t=d}
D/Connection: conn_1 - received data message: {r=16, b={d={expires=1546758210, auth={email=tp37@mail.apu.edu.my, uid=VjYBKLV8tIRlgFNXBzt0pZQz7gH2, staff=true, token={sub=VjYBKLV8tIRlgFNXBzt0pZQz7gH2, iss=https://securetoken.google.com/postal-system, aud=postal-system, firebase={sign_in_provider=password, identities={email=[tp37@mail.apu.edu.my]}}, email=tp37@mail.apu.edu.my, auth_time=1546618590, iat=1546754610, staff=true, exp=1546758210, user_id=VjYBKLV8tIRlgFNXBzt0pZQz7gH2, email_verified=true}, user_id=VjYBKLV8tIRlgFNXBzt0pZQz7gH2, provider=password, email_verified=true}}, s=ok}}
D/PersistentConnection: pc_0 - Restoring outstanding listens
pc_0 - Restoring listen ce1b4691-84d6-434b-b12d-9341bcc25b3b (params: {})
D/Connection: conn_1 - Sending data: {d={a=q, r=17, b={h=, p=ce1b4691-84d6-434b-b12d-9341bcc25b3b}}, t=d}
D/WebSocket: ws_1 - Reset keepAlive. Remaining: 44999
D/PersistentConnection: pc_0 - Restoring writes.
D/Connection: conn_1 - Sending data: {d={a=p, r=18, b={d={Pattern=, 
Confirmation=NONE, Time=NONE, Connection=NONE, Pattern_2=NONE}, p=e0afc0d1-6020-4d88-94e7-24104d154f28}}, t=d}
D/WebSocket: ws_1 - Reset keepAlive. Remaining: 44999
D/WebSocket: ws_1 - ws message: {"t":"d","d":{"r":17,"b":{"s":"ok","d":{}}}}
D/WebSocket: ws_1 - Reset keepAlive. Remaining: 44793
 ws_1 - HandleNewFrameCount: 1
 ws_1 - handleIncomingFrame complete frame: {d={r=17, b={d={}, s=ok}}, t=d}
D/Connection: conn_1 - received data message: {r=18, b={d=, s=ok}}
D/PersistentConnection: pc_0 - p response: {d=, s=ok}
D/PersistentConnection: pc_0 - Listening on e0afc0d1-6020-4d88-94e7-24104d154f28 (params: {})
 pc_0 - Adding listen query: e0afc0d1-6020-4d88-94e7-24104d154f28 (params: {})
D/Connection: conn_1 - Sending data: {d={a=q, r=19, b={h=, p=e0afc0d1-6020-4d88-94e7-24104d154f28}}, t=d}
D/WebSocket: ws_1 - Reset keepAlive. Remaining: 44306
D/EGL_emulation: eglMakeCurrent: 0xaa1d27e0: ver 2 0 (tinfo 0xac358240)
E/Surface: getSlotFromBufferLocked: unknown buffer: 0xaa058400
D/Connection: conn_1 - Sending data: {d={a=o, r=20, b={d=null, p=e0afc0d1-6020-4d88-94e7-24104d154f28}}, t=d}
D/WebSocket: ws_1 - Reset keepAlive. Remaining: 44988
D/EGL_emulation: eglMakeCurrent: 0xaa1d27e0: ver 2 0 (tinfo 0xac358240)
D/WebSocket: ws_1 - ws message: {"t":"d","d":{"b":{"p":"e0afc0d1-6020-4d88-94e7-24104d154f28","d":{"Confirmation":"NONE","Connection":"NONE","Pattern":"","Pattern_2":"NONE","Time":"NONE"}},"a":"d"}}
D/WebSocket: ws_1 - ws message: {"t":"d","d":{"r":19,"b":{"s":"ok","d":{}}}}
    ws_1 - handleIncomingFrame complete frame: {d={r=19, b={d={}, s=ok}}, t=d}
D/Connection: conn_1 - received data message: {r=19, b={d={}, s=ok}}
D/EventRaiser: Raising /e0afc0d1-6020-4d88-94e7-24104d154f28: CHILD_ADDED: { Confirmation: NONE }
D/EventRaiser: Raising /e0afc0d1-6020-4d88-94e7-24104d154f28: CHILD_ADDED: { Connection: NONE }
Raising /e0afc0d1-6020-4d88-94e7-24104d154f28: CHILD_ADDED: { Pattern:  }
Raising /e0afc0d1-6020-4d88-94e7-24104d154f28: CHILD_ADDED: { Pattern_2: NONE }
Raising /e0afc0d1-6020-4d88-94e7-24104d154f28: CHILD_ADDED: { Time: NONE }
D/WebSocket: ws_1 - ws message: {"t":"d","d":{"r":20,"b":{"s":"ok","d":""}}}
D/WebSocket: ws_1 - Reset keepAlive. Remaining: 44988
  ws_1 - HandleNewFrameCount: 1
  ws_1 - handleIncomingFrame complete frame: {d={r=20, b={d=, s=ok}}, t=d}
D/Connection: conn_1 - received data message: {r=20, b={d=, s=ok}}

Application terminated.

标签: androidfirebasefirebase-realtime-database

解决方案


当它断开连接时,Firebase 客户端进入指数退避循环,您无法从应用程序代码中控制它。

但是对于短暂的断开连接来说,40 秒似乎有点长。您可能希望启用调试日志记录并检查 logcat 输出以查看它是否显示任何提示,说明为什么需要这么长时间。


推荐阅读