Skip to content
This repository was archived by the owner on Apr 4, 2023. It is now read-only.

Commit e1da9f4

Browse files
#61 Offline capabilities for Firebase database
1 parent 6d638fd commit e1da9f4

File tree

5 files changed

+53
-2
lines changed

5 files changed

+53
-2
lines changed

docs/DATABASE.md

Lines changed: 22 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -158,4 +158,25 @@ but if you only want to wipe everything at `/users`, do this:
158158

159159
```js
160160
firebase.remove("/users");
161-
```
161+
```
162+
163+
### keepInSync
164+
The Firebase Realtime Database synchronizes and stores a local copy of the data for active listeners (see the methods above). In addition, you can keep specific locations in sync.
165+
166+
The client will automatically download the data at these locations and keep it in sync even if the reference has no active listeners.
167+
168+
```js
169+
firebase.keepInSync(
170+
"/users", // which path in your Firebase needs to be kept in sync?
171+
true // set to false to disable this feature again
172+
).then(
173+
function () {
174+
console.log("firebase.keepInSync is ON for /users");
175+
},
176+
function (error) {
177+
console.log("firebase.keepInSync error: " + error);
178+
}
179+
);
180+
```
181+
182+
By default, 10MB of previously synced data will be cached. If the cache outgrows its configured size, the Firebase Realtime Database will purge data that has been used least recently. Data that is kept in sync, will not be purged from the cache. _(From the official doc for [iOS](https://firebase.google.com/docs/database/ios/offline-capabilities) and [Android](https://firebase.google.com/docs/database/android/offline-capabilities))._

firebase.android.js

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -634,6 +634,19 @@ firebase.deleteUser = function (arg) {
634634
});
635635
};
636636

637+
firebase.keepInSync = function (path, switchOn) {
638+
return new Promise(function (resolve, reject) {
639+
try {
640+
var where = firebase.instance.child(path);
641+
where.keepSynced(switchOn);
642+
resolve();
643+
} catch (ex) {
644+
console.log("Error in firebase.keepInSync: " + ex);
645+
reject(ex);
646+
}
647+
});
648+
};
649+
637650
firebase._addObservers = function(to, updateCallback) {
638651
var listener = new com.google.firebase.database.ChildEventListener({
639652
onChildAdded: function (snapshot, previousChildKey) {

firebase.d.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -316,6 +316,10 @@ declare module "nativescript-plugin-firebase" {
316316
export function query(onValueEvent: (data: FBData) => void, path: string, options: QueryOptions): Promise<any>;
317317
export function addChildEventListener(onChildEvent: (data: FBData) => void, path: string): Promise<any>;
318318
export function addValueEventListener(onValueEvent: (data: FBData) => void, path: string): Promise<any>;
319+
/**
320+
* Tells the client to keep its local cache in sync with the server automatically.
321+
*/
322+
export function keepInSync(path: string, switchOn: boolean): Promise<any>;
319323

320324
// Auth
321325
export function login(options: LoginOptions): Promise<User>;

firebase.ios.js

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -678,6 +678,19 @@ firebase._addObservers = function(to, updateCallback) {
678678
});
679679
};
680680

681+
firebase.keepInSync = function (path, switchOn) {
682+
return new Promise(function (resolve, reject) {
683+
try {
684+
var where = firebase.instance.childByAppendingPath(path);
685+
where.keepSynced(switchOn);
686+
resolve();
687+
} catch (ex) {
688+
console.log("Error in firebase.keepInSync: " + ex);
689+
reject(ex);
690+
}
691+
});
692+
};
693+
681694
firebase.addChildEventListener = function (updateCallback, path) {
682695
return new Promise(function (resolve, reject) {
683696
try {

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "nativescript-plugin-firebase",
3-
"version": "3.4.0",
3+
"version": "3.4.1",
44
"description" : "Fire. Base. Firebase!",
55
"main" : "firebase.js",
66
"nativescript": {

0 commit comments

Comments
 (0)