adriancarriger / angularfire2-offline

🔌 A simple wrapper for AngularFire2 to read and write to Firebase while offline, even after a complete refresh.
https://angularfire2-offline.firebaseapp.com/
MIT License
207 stars 48 forks source link

ListObservable doesn't support .push() #4

Closed cweidinger closed 7 years ago

cweidinger commented 7 years ago

Can you provide an example of how you would use angularfire2offline to write to a firebase location? Or would you just use angularfire to write to locations and only use angularfire2offline for the reads?

adriancarriger commented 7 years ago

Right now AngularFire2Offline only works with read-only methods.

You can use both AngularFire2 for writing to Firebase and AngularFire2Offline when you would like to be able to access data offline.

If you would like to have write methods added to this library, feel free to request that feature.

Here's an example of using both libraries together:

import { Component } from '@angular/core';
import { AngularFire, FirebaseListObservable } from 'angularfire2';
import {
  AngularFireOffline,
  ListObservable,
  ObjectObservable } from 'angularfire2-offline';

@Component({
  selector: 'project-name-app',
  template: `
  <h1>{{ (info | async)?.name }}</h1>
  <ul>
    <li *ngFor="let item of items | async">
      {{ item?.name }}
    </li>
  </ul>
  <button (click)="addItem('itemNameHere')">Add item</button>
  `
})
export class MyApp {
  info: ObjectObservable<any>;
  items: ListObservable<any[]>;
  afItems: FirebaseListObservable<any>;
  constructor(
    af: AngularFire,
    afo: AngularFireOffline) {
    this.info = afo.database.object('/info');
    this.items = afo.database.list('/items');
    this.afItems = af.database.list('/items');
  }
  addItem(itemName) {
    this.afItems.push({ name: itemName });
  }
}
niklasp commented 7 years ago

+1 i would like that feature - to have pushes stored in localstorage that are synced on connect. unfortunately my ts skills are not yet good enough to implement it myself

EDIT: just found out that it works 😮 . Can you explain how that synchronization works. Did not see it in your code.

adriancarriger commented 7 years ago

@niklasp Saving to Firebase while offline sounds like a useful feature!

giorgiopiatti commented 7 years ago

I've just find this library and I think that is very great! Saving data to Firebase while offline would be great, how do you plan to manage conflicts?

adriancarriger commented 7 years ago

Hi @giorgiopiatti

AngularFire2 Offline currently supports saving data to Firebase while offline for the related AngularFire2 write operations: Read object, Read list, Write object, Write list.

Write conflicts can happen with any Firebase app, but the issue is magnified with offline based writes.

I put together a few examples of how you might solve some common scenarios, but I'd love to hear additional use cases.

cweidinger commented 7 years ago

@adriancarriger You're a boss. This is a key piece of the angularfire tech for mobile phones where you can't step in when the user is about to force quit the app.

adriancarriger commented 7 years ago

@cweidinger Awesome, glad it's helpful!

giorgiopiatti commented 7 years ago

@adriancarriger Thankyou very much for the examples! I think that with timestamp, all can be done fine.