Java Code Examples for com.google.firebase.database.DatabaseReference#push()

The following examples show how to use com.google.firebase.database.DatabaseReference#push() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: RxFirebaseHelper.java    From EasyFirebase with Apache License 2.0 6 votes vote down vote up
public <T> Observable<T> push(final DatabaseReference databaseReference) {
    final Observable<T> observable = Observable.create(new Observable.OnSubscribe<T>() {
                                                           @Override
                                                           public void call(final Subscriber<? super T> subscriber) {
                                                               databaseReference.addListenerForSingleValueEvent(new ValueEventListener() {
                                                                   @Override
                                                                   public void onDataChange(DataSnapshot dataSnapshot) {
                                                                       subscriber.onNext(null);
                                                                       subscriber.onCompleted();
                                                                   }

                                                                   @Override
                                                                   public void onCancelled(DatabaseError databaseError) {

                                                                   }
                                                               });
                                                           }
                                                       });

    databaseReference.push();
    return observable;
}
 
Example 2
Source File: DataTestIT.java    From firebase-admin-java with Apache License 2.0 4 votes vote down vote up
@Test
@Ignore
public void testDeltaSyncWithUnfilteredQuery()
    throws InterruptedException, ExecutionException, TestFailure, TimeoutException {
  List<DatabaseReference> refs = IntegrationTestUtils.getRandomNode(masterApp, 2);
  DatabaseReference writeRef = refs.get(0);
  DatabaseReference readRef = refs.get(1);

  // List must be large enough to trigger delta sync.
  Map<String, Object> longList = new HashMap<>();
  for (long i = 0; i < 50; i++) {
    String key = writeRef.push().getKey();
    longList.put(key,
        new MapBuilder().put("order", i).put("text", "This is an awesome message!").build());
  }

  new WriteFuture(writeRef, longList).timedGet();

  // start listening.
  final List<DataSnapshot> readSnapshots = new ArrayList<>();
  final Semaphore readSemaphore = new Semaphore(0);
  readRef.orderByChild("order").addValueEventListener(new ValueEventListener() {
    @Override
    public void onDataChange(DataSnapshot snapshot) {
      readSnapshots.add(snapshot);
      readSemaphore.release();
    }

    @Override
    public void onCancelled(DatabaseError error) {
    }
  });

  TestHelpers.waitFor(readSemaphore);
  TestHelpers.assertDeepEquals(longList, readSnapshots.get(0).getValue());

  // Add a new child while readRef is offline.
  readRef.getDatabase().goOffline();

  DatabaseReference newChildRef = writeRef.push();
  Map<String, Object> newChild = new MapBuilder().put("order", 50L)
      .put("text", "This is a new child!").build();
  new WriteFuture(newChildRef, newChild).timedGet();
  longList.put(newChildRef.getKey(), newChild);

  // Go back online and make sure we get the new item.
  readRef.getDatabase().goOnline();
  TestHelpers.waitFor(readSemaphore);
  TestHelpers.assertDeepEquals(longList, readSnapshots.get(1).getValue());
}
 
Example 3
Source File: RealtimeTestIT.java    From firebase-admin-java with Apache License 2.0 4 votes vote down vote up
@Test
@Ignore
public void testShutdown()
    throws InterruptedException, ExecutionException, TestFailure, TimeoutException {
  DatabaseConfig config = TestHelpers.getDatabaseConfig(masterApp);

  // Shut it down right away
  RepoManager.interrupt(config);
  Thread.sleep(6 * 1000); // Long enough for all of the threads to exit
  assertTrue(config.isStopped());

  // Test that we can use an existing ref
  DatabaseReference ref = FirebaseDatabase.getInstance().getReference();
  DatabaseReference pushed = ref.push();
  try {
    new WriteFuture(pushed, "foo").timedGet();
    fail("Should time out, we're offline");
  } catch (TimeoutException t) {
    // Expected, we're offline
  }
  assertFalse(config.isStopped());
  RepoManager.resume(config);

  final Semaphore ready = new Semaphore(0);
  ref.child(".info/connected").addValueEventListener(new ValueEventListener() {
    @Override
    public void onDataChange(DataSnapshot snapshot) {
      Boolean connected = snapshot.getValue(Boolean.class);
      if (connected) {
        ready.release(1);
      }
    }

    @Override
    public void onCancelled(DatabaseError error) {
    }
  });

  // Wait for us to be connected so we send the buffered put
  TestHelpers.waitFor(ready);

  DataSnapshot snap = TestHelpers.getSnap(pushed);
  assertEquals("foo", snap.getValue(String.class));
}
 
Example 4
Source File: SalesPendingPayButtonIntentService.java    From stockita-point-of-sale with MIT License 4 votes vote down vote up
/**
 * Helper method to proceed the open bill and pass data into the server
 * @param userUid                       The user's uid
 * @param salesHeaderModel              {@link SalesHeaderModel} instance
 * @param salesDetailModelList          {@link SalesDetailModel} arrayList instance
 */
private void performOpenBill(String userUid, SalesHeaderModel salesHeaderModel, ArrayList<SalesDetailModel> salesDetailModelList) {

    // Get the node to /paidSalesHeader
    DatabaseReference salesHeaderRef = FirebaseDatabase.getInstance().getReference()
            .child(userUid)
            .child(Constants.FIREBASE_OPEN_SALES_HEADER_LOCATION);

    // Create the push() key
    DatabaseReference salesHeaderKey = salesHeaderRef.push();
    String headerKey = salesHeaderKey.getKey();

    // Pass the pojo
    salesHeaderKey.setValue(salesHeaderModel);


    /**
     * Store the {@link SalesDetailModel} into the server
     */

    // The sales detail is already packed in the list, now we need the size of the list
    int sizeOfList = salesDetailModelList.size();

    // Get the node to /paidSalesDetail
    DatabaseReference salesDetailRef = FirebaseDatabase.getInstance().getReference()
            .child(userUid).child(Constants.FIREBASE_OPEN_SALES_DETAIL_LOCATION)
            .child(headerKey);

    // Iterate
    for (int i = 0; i < sizeOfList; i++) {

        // Get the model form the list
        SalesDetailModel salesDetailModel = salesDetailModelList.get(i);

        // create a new push() key for each element in the list
        salesDetailRef.push().setValue(salesDetailModel);

    }

    // Now delete all data in the /salesDetailPending node.
    DatabaseReference salesDetailPendingRef = FirebaseDatabase.getInstance().getReference()
            .child(userUid)
            .child(Constants.FIREBASE_SALES_DETAIL_PENDING_LOCATION);

    // set the value to null means delete all
    salesDetailPendingRef.setValue(null);


    // Delete all data in the local database after
    ContentResolver contentResolver = getBaseContext().getContentResolver();
    contentResolver.delete(ContractData.SalesDetailPendingEntry.CONTENT_URI, null, null);

}