【发布时间】:2019-11-27 09:01:17
【问题描述】:
我们正在 dart 中开发一个应用程序,我们希望在本地存储数据,以便当应用程序重新启动时,即使没有连接,用户也能看到数据。我已经阅读了 setPersistenceEnabled 属性来做到这一点,但我认为我们做错了。请找到以下详细信息 - 有人可以帮助我们吗?
在安卓模拟器 Nexus 5 上运行应用
我们已将应用程序与 Android 版本的 Firebase 数据库连接起来。
在 pubspec.yaml 中包含 Firebase_database 和 Firebase_core 包
下面是我们在 main.dart 中调用 setPersistenceEnabled 方法的代码 - 正在使用有状态小部件。
此外,在获取特定集合的数据时,我们使用的是 REST API。
如果我们做错了什么或需要更多详细信息,请告诉我。
我有 connected_products.dart 文件,我在其中创建了这个函数来调用数据库实例。
FirebaseDatabase getDatabaseInstance() {
if (database == null) {
print("should fire once");
database = FirebaseDatabase.instance;
database.setPersistenceEnabled(true);
database.setPersistenceCacheSizeBytes(25000000); //25 MB
}
return database;
}
在下面的同一个文件中,我有一个获取产品的功能
Future<Null> fetchProducts() {
_isLoading = true;
notifyListeners();
return getDatabaseInstance()
.reference()
.child("products")
.once()
.then<Null>((DataSnapshot snapshot) {
final List<Product> fetchedProductList = [];
snapshot.value.forEach((dynamic productData) {
final Product product = Product(
blockno: productData['BLOCK_NO'],
name: productData['NAME'],
id: productData['CARD_NO'].toString().isEmpty
? 'Not Available'
: productData['CARD_NO'],
serialno: productData['SR_NO'],
surname: productData['SURNAME'],
firstname: productData['FIRSTNAME'], // This is modified fo search
age: productData['AGE'],
gender: productData['GENDER'],
acnumber: productData['AC_NO'],
isDone: productData['IsDone']);
fetchedProductList.add(product);
});
_products = fetchedProductList;
_isLoading = false;
_isFetchCalled = true;
notifyListeners();
_selProductIndex = null;
}).catchError((error) {
_isLoading = false;
print(error);
notifyListeners();
return;
});
}
我有另一个名为 phone.dart 的文件 - 我在其中调用此函数
@override
initState() {
if (widget.model.getFetchCalled == false) {
widget.model.fetchProducts();
widget.model.checkToSeeString();
}
widget.model.fetchPhone();
super.initState();
}
所以当我运行构建时,我在日志中得到以下响应。所以不确定数据是否被缓存,因为当我再次关闭和打开应用程序并且我无法看到数据并且不得不通过访问电话小部件进行数据库调用(互联网连接已打开但数据库调用将不会发生,直到我访问 phone.dart 小部件):
我是否需要明确地从本地存储中读取数据?如果是的话怎么做
日志详情
Background concurrent copying GC freed 920803(35MB) AllocSpace objects, 0(0B) LOS objects, 4% free, 121MB/127MB, paused 1.904ms total 3.032s
W/ample.first_ap( 5637): JNI critical lock held for 16.668ms on Thread[1,tid=5637,Runnable,Thread*=0xe9f5c000,peer=0x756f0760,"main"]
I/ample.first_ap( 5637): Background concurrent copying GC freed 930100(35MB) AllocSpace objects, 0(0B) LOS objects, 4% free, 123MB/129MB, paused 22.580ms total 3.628s
W/ample.first_ap( 5637): JNI critical lock held for 19.275ms on Thread[1,tid=5637,Runnable,Thread*=0xe9f5c000,peer=0x756f0760,"main"]
I/ample.first_ap( 5637): Background concurrent copying GC freed 928850(35MB) AllocSpace objects, 0(0B) LOS objects, 4% free, 130MB/136MB, paused 4.890ms total 2.929s
I/ample.first_ap( 5637): Background concurrent copying GC freed 1427703(38MB) AllocSpace objects, 3(7MB) LOS objects, 3% free, 161MB/167MB, paused 1.943ms total 2.252s
I/ample.first_ap( 5637): Background concurrent copying GC freed 2657600(56MB) AllocSpace objects, 498(23MB) LOS objects, 2% free, 229MB/235MB, paused 1.617ms total 3.073s
I/ample.first_ap( 5637): Background concurrent copying GC freed 2324689(78MB) AllocSpace objects, 1485(69MB) LOS objects, 2% free, 213MB/219MB, paused 1.554ms total 2.347s
I/ample.first_ap( 5637): Waiting for a blocking GC ProfileSaver
I/ample.first_ap( 5637): Background concurrent copying GC freed 2098267(69MB) AllocSpace objects, 1313(61MB) LOS objects, 2% free, 239MB/245MB, paused 1.858ms total 4.004s
I/ample.first_ap( 5637): WaitForGcToComplete blocked ProfileSaver on HeapTrim for 14.705s
I/ample.first_ap( 5637): Waiting for a blocking GC Alloc
I/ample.first_ap( 5637): Background concurrent copying GC freed 5390417(114MB) AllocSpace objects, 12(42MB) LOS objects, 2% free, 226MB/232MB, paused 1.479ms total 2.502s
I/ample.first_ap( 5637): WaitForGcToComplete blocked Alloc on ProfileSaver for 78.480ms
I/ample.first_ap( 5637): Starting a blocking GC Alloc
I/ample.first_ap( 5637): Background concurrent copying GC freed 2346436(77MB) AllocSpace objects, 1451(67MB) LOS objects, 2% free, 218MB/224MB, paused 1.214ms total 1.639s
I/ample.first_ap( 5637): Background concurrent copying GC freed 2119192(71MB) AllocSpace objects, 1400(65MB) LOS objects, 4% free, 129MB/135MB, paused 1.393ms total 2.332s
【问题讨论】:
-
是什么让您认为
setPersistenceEnabled(true)不起作用? -
嗨弗兰克-我尝试在模拟器上运行应用程序,使用 REST API 在应用程序中获取数据,然后我关闭了它。后来当我再次启动应用程序时,数据不存在,不得不再次调用数据库来获取数据。
-
我们必须查看您如何检索数据的代码,但鉴于您提到 REST API,您似乎是在 SDK 之外检索数据,这意味着 SDK 可以'不要为你缓存数据。
-
让我在某个时间粘贴代码,但同时在从我正在使用的数据库集合中获取数据时,即“http.get(databaseURL/colletion.json)”。这可能是原因吗?我需要用 database().reference.child('collection-name') 替换它吗??
-
您可以使用
database.setPersistenceEnabled(true)在 Firebase 客户端上启用持久性。只有您通过该客户端读取的数据才会由该客户端保存。如果您没有使用同一个客户端来读取数据,则该客户端不会保留该数据。
标签: firebase firebase-realtime-database flutter dart