Sha*_*hra 6 dart firebase flutter google-cloud-firestore
我在颤振应用程序中使用 Firestore。每次用户启动应用程序时,它都会从 Firestore Cloud 检索一些数据。
QuerySnapshot dataSnapshot = await Firestore.instance
.collection('/data')
.getDocuments();
Run Code Online (Sandbox Code Playgroud)
当用户第一次打开应用程序时,他需要在线连接,获取数据,正如 Firebase 文档所说
对于 Android 和 iOS,默认启用离线持久化。要禁用持久性,请将 PersistenceEnabled 选项设置为 false。
因此,它应该保存应用程序之前读取过的数据,以便在设备离线时检索它;因此用户可以随时使用已读取的相同数据访问应用程序。
问题是:在设备离线时检索数据需要很长时间,使用相同的代码并且没有任何更改!。
我尝试配置需要多少时间?离线时,大约需要 8 分 40 秒。但在线时,只需 10 秒,甚至可能更短。
那么我该如何解决这个问题呢?
============
我设法获得了有关此问题的更多日志,这花费了很多时间,并将使用离线保存的数据启动应用程序,它会打印此日志
This typically indicates that your device does not have a healthy Internet connection at the moment. The client will operate in offline mode until it is able to successfully connect to the backend.
然后以 3 秒为例(时间不多)并继续下一个作品。
我也确实在GitHub 中打开了一个新问题。
有没有办法限制它花费的时间?
最后,在GitHub 问题中的diegoveloper评论的帮助下,我找到了解决方案。
await Firestore.instance .collection("Collection") .getDocuments(source: source)
如果我决定每次检查源代码然后使用它,或者我可以在启动新的 Flutter 项目时使用它,这是一个很好的解决方案,但现在我已经有很多代码需要更好的解决方案。所以我决定分叉 cloud_firestore 包并编辑它。
您可以在这里找到它:https://github.com/ShadyBoshra2012/flutterfire/tree/master/packages/cloud_firestore
// The source of which the data will come from.
static Source _source = Source.serverAndCache;
static Source get source => _source;
Future<void> settings(
{bool persistenceEnabled,
String host,
bool sslEnabled,
bool timestampsInSnapshotsEnabled,
int cacheSizeBytes,
Source source}) async {
await channel.invokeMethod<void>('Firestore#settings', <String, dynamic>{
'app': app.name,
'persistenceEnabled': persistenceEnabled,
'host': host,
'sslEnabled': sslEnabled,
'timestampsInSnapshotsEnabled': timestampsInSnapshotsEnabled,
'cacheSizeBytes': cacheSizeBytes,
});
if (source != null) _source = source;
}
Run Code Online (Sandbox Code Playgroud)
query.dart
source = Firestore.source;第 92 行
document_reference.dart
source = Firestore.source;第 83 行
因此,您可以通过使用 Google 的连接包以这种方式使用我的分叉存储库: https: //pub.dev/packages/connectivity。
pubspec.yaml在文件中添加我的分叉存储库
cloud_firestore:
git:
url: https://github.com/ShadyBoshra2012/flutterfire.git
path: packages/cloud_firestore
Run Code Online (Sandbox Code Playgroud)
然后在您的第一个屏幕或主屏幕中
var connectivityResult = await (Connectivity().checkConnectivity());
if (connectivityResult == ConnectivityResult.none) {
await Firestore.instance.settings(source: Source.cache);
} else {
await Firestore.instance.settings(source: Source.serverAndCache);
}
Run Code Online (Sandbox Code Playgroud)
如果您想在更改连接状态时刷新源:
StreamSubscription subscription;
void initState() {
super.initState();
// Check the internet connection after each change
// of the connection.
subscription = Connectivity()
.onConnectivityChanged
.listen((ConnectivityResult result) async {
// Check the internet connection and then choose the appropriate
// source for it.
var connectivityResult = await (Connectivity().checkConnectivity());
if (connectivityResult == ConnectivityResult.none) {
await Firestore.instance.settings(source: Source.cache);
} else {
await Firestore.instance.settings(source: Source.serverAndCache);
}
});
}
@override
void dispose() {
super.dispose();
subscription.cancel();
}
Run Code Online (Sandbox Code Playgroud)
所以我希望大家都能看到它,并等待 Flutter Team 编写出越来越好的解决方案。感谢大家的参与。
| 归档时间: |
|
| 查看次数: |
2407 次 |
| 最近记录: |