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

The following examples show how to use com.google.firebase.database.DatabaseReference#keepSynced() . 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: KeepSyncedTest.java    From firebase-android-sdk with Apache License 2.0 6 votes vote down vote up
@Test
public void manyKeepSyncedCallsDontAccumulate() throws Exception {
  DatabaseReference ref = IntegrationTestHelpers.getRandomNode();

  ref.keepSynced(true);
  ref.keepSynced(true);
  ref.keepSynced(true);
  assertIsKeptSynced(ref);

  // If it were balanced, this would not be enough
  ref.keepSynced(false);
  ref.keepSynced(false);
  assertNotKeptSynced(ref);

  // If it were balanced, this would not be enough
  ref.keepSynced(true);
  assertIsKeptSynced(ref);

  // cleanup
  ref.keepSynced(false);
}
 
Example 2
Source File: HomeBaseFragment.java    From kute with Apache License 2.0 6 votes vote down vote up
/********************************* Data Querying Methods *************************/
//Load friends from firebase
public void getFirebaseFriend() {
    /******************** Getting Friends From Firebase *************/
    String ref = "Friends/" + getActivity().getSharedPreferences("user_credentials", 0).getString("Name", null);
    Log.d(TAG, "Firebase Reference :" + ref);
    DatabaseReference friends = FirebaseDatabase.getInstance().getReference(ref);
    friends.keepSynced(true);
    friends.addListenerForSingleValueEvent(new ValueEventListener() {
        @Override
        public void onDataChange(DataSnapshot dataSnapshot) {
            for (DataSnapshot k : dataSnapshot.getChildren()) {
                friend_list.add(k.getKey());
                Log.d(TAG, "Debug Firebase data query" + k.getValue().toString());
            }
            
            Log.d(TAG, String.format("The Friend List Size is %d",friend_list.size()));
            getFirstFriendProfile();

        }

        @Override
        public void onCancelled(DatabaseError databaseError) {
        }
    });
}
 
Example 3
Source File: KeepSyncedTest.java    From firebase-android-sdk with Apache License 2.0 6 votes vote down vote up
@Test
public void keepSyncedNoDoesNotAffectExistingListener() throws Exception {
  DatabaseReference ref = IntegrationTestHelpers.getRandomNode();

  ref.keepSynced(true);
  assertIsKeptSynced(ref);

  ReadFuture readFuture =
      new ReadFuture(
          ref,
          new ReadFuture.CompletionCondition() {
            @Override
            public boolean isComplete(List<EventRecord> events) {
              return events.get(events.size() - 1).getSnapshot().getValue().equals("done");
            }
          });

  // cleanup
  ref.keepSynced(false);

  // Should trigger our listener.
  ref.setValue("done");
  readFuture.timedGet();
}
 
Example 4
Source File: LoadPersonRoutesAsyncTask.java    From kute with Apache License 2.0 6 votes vote down vote up
@Override
protected ArrayList<Route> doInBackground(String... params) {
    //Return the retrieved list of person's  routes from firebase
    String id=params[0];
    String route_ref="Routes/"+id;
    DatabaseReference routes_ref= FirebaseDatabase.getInstance().getReference(route_ref);
    routes_ref.keepSynced(true);
    routes_ref.addListenerForSingleValueEvent(new ValueEventListener() {
        @Override
        public void onDataChange(DataSnapshot dataSnapshot) {
            for (DataSnapshot routes:dataSnapshot.getChildren()){
                Route temp=routes.getValue(Route.class);
                Log.i(TAG,temp.getName());
                routes_list.add(temp);
            }
            messenger_to_activity.onTaskCompleted(routes_list);
        }

        @Override
        public void onCancelled(DatabaseError databaseError) {
        }
    });
    return null;
}
 
Example 5
Source File: KeepSyncedTestIT.java    From firebase-admin-java with Apache License 2.0 6 votes vote down vote up
@Test
public void testKeepSyncedWithExistingListener() throws Exception {
  DatabaseReference ref = IntegrationTestUtils.getRandomNode(masterApp);

  ReadFuture readFuture;
  ref.keepSynced(true);
  try {
    assertIsKeptSynced(ref);

    readFuture =
        new ReadFuture(ref, new ReadFuture.CompletionCondition() {
            @Override
            public boolean isComplete(List<EventRecord> events) {
              return events.get(events.size() - 1).getSnapshot().getValue().equals("done");
            }
          });
  } finally {
    // cleanup
    ref.keepSynced(false);
  }

  // Should trigger our listener.
  ref.setValueAsync("done");
  readFuture.timedGet();
}
 
Example 6
Source File: KeepSyncedTestIT.java    From firebase-admin-java with Apache License 2.0 6 votes vote down vote up
@Test
public void testRemoveSingleListener() throws Exception {
  DatabaseReference ref = IntegrationTestUtils.getRandomNode(masterApp);

  ref.keepSynced(true);
  try {
    assertIsKeptSynced(ref);

    // This will add and remove a listener.
    new ReadFuture(
        ref,
        new ReadFuture.CompletionCondition() {
          @Override
          public boolean isComplete(List<EventRecord> events) {
            return true;
          }
        })
        .timedGet();

    assertIsKeptSynced(ref);
  } finally {
    // cleanup
    ref.keepSynced(false);
  }
}
 
Example 7
Source File: KeepSyncedTestIT.java    From firebase-admin-java with Apache License 2.0 6 votes vote down vote up
@Test
public void testMultipleKeepSynced() throws Exception {
  DatabaseReference ref = IntegrationTestUtils.getRandomNode(masterApp);

  try {
    ref.keepSynced(true);
    ref.keepSynced(true);
    ref.keepSynced(true);
    assertIsKeptSynced(ref);

    // If it were balanced, this would not be enough
    ref.keepSynced(false);
    ref.keepSynced(false);
    assertNotKeptSynced(ref);

    // If it were balanced, this would not be enough
    ref.keepSynced(true);
    assertIsKeptSynced(ref);
  } finally {
    // cleanup
    ref.keepSynced(false);
  }
}
 
Example 8
Source File: KeepSyncedTestIT.java    From firebase-admin-java with Apache License 2.0 5 votes vote down vote up
@Test
public void testKeepSyncedAffectOnQueries() throws Exception {
  DatabaseReference ref = IntegrationTestUtils.getRandomNode(masterApp);
  ref.keepSynced(true);
  Query query = ref.limitToFirst(5);
  query.keepSynced(true);
  assertIsKeptSynced(ref);

  ref.keepSynced(false);
  assertNotKeptSynced(ref);
  // currently, setting false on the default query affects all queries at that location
  assertNotKeptSynced(query);
}
 
Example 9
Source File: FirestackDatabase.java    From react-native-firestack with MIT License 5 votes vote down vote up
@ReactMethod
public void keepSynced(
  final String path,
  final Boolean enable,
  final Callback callback) {
    DatabaseReference ref = this.getDatabaseReferenceAtPath(path);
    ref.keepSynced(enable);

    WritableMap res = Arguments.createMap();
    res.putString("status", "success");
    res.putString("path", path);
    callback.invoke(null, res);
}
 
Example 10
Source File: OfflineActivity.java    From snippets-android with Apache License 2.0 5 votes vote down vote up
private void keepSynced() {
    // [START rtdb_keep_synced]
    DatabaseReference scoresRef = FirebaseDatabase.getInstance().getReference("scores");
    scoresRef.keepSynced(true);
    // [END rtdb_keep_synced]

    // [START rtdb_undo_keep_synced]
    scoresRef.keepSynced(false);
    // [END rtdb_undo_keep_synced]
}
 
Example 11
Source File: KeepSyncedTestIT.java    From firebase-admin-java with Apache License 2.0 5 votes vote down vote up
@Test
public void testKeptSyncedRoot() throws Exception {
  DatabaseReference ref = FirebaseDatabase.getInstance().getReference();

  ref.keepSynced(true);
  try {
    assertIsKeptSynced(ref);
  } finally {
    // cleanup
    ref.keepSynced(false);
  }
}
 
Example 12
Source File: KeepSyncedTestIT.java    From firebase-admin-java with Apache License 2.0 5 votes vote down vote up
@Test
public void testKeptSyncedChild() throws Exception {
  DatabaseReference ref = IntegrationTestUtils.getRandomNode(masterApp);
  DatabaseReference child = ref.child("random-child");

  ref.keepSynced(true);
  try {
    assertIsKeptSynced(child);
  } finally {
    // cleanup
    ref.keepSynced(false);
  }
}
 
Example 13
Source File: KeepSyncedTest.java    From firebase-android-sdk with Apache License 2.0 5 votes vote down vote up
@Test
public void keepSynced() throws Exception {
  DatabaseReference ref = IntegrationTestHelpers.getRandomNode();
  ref.keepSynced(true);
  assertIsKeptSynced(ref);

  ref.keepSynced(false);
  assertNotKeptSynced(ref);
}
 
Example 14
Source File: KeepSyncedTestIT.java    From firebase-admin-java with Apache License 2.0 5 votes vote down vote up
@Test
public void testKeepSynced() throws Exception {
  DatabaseReference ref = IntegrationTestUtils.getRandomNode(masterApp);
  ref.keepSynced(true);
  assertIsKeptSynced(ref);

  ref.keepSynced(false);
  assertNotKeptSynced(ref);
}
 
Example 15
Source File: KeepSyncedTest.java    From firebase-android-sdk with Apache License 2.0 5 votes vote down vote up
@Test
public void rootIsKeptSynced() throws Exception {
  DatabaseReference ref = IntegrationTestHelpers.getRandomNode().getRoot();

  ref.keepSynced(true);
  assertIsKeptSynced(ref);

  // cleanup
  ref.keepSynced(false);
}
 
Example 16
Source File: KeepSyncedTest.java    From firebase-android-sdk with Apache License 2.0 5 votes vote down vote up
@Test
public void childIsKeptSynced() throws Exception {
  DatabaseReference ref = IntegrationTestHelpers.getRandomNode();
  DatabaseReference child = ref.child("random-child");

  ref.keepSynced(true);
  assertIsKeptSynced(child);

  // cleanup
  ref.keepSynced(false);
}
 
Example 17
Source File: KeepSyncedTest.java    From firebase-android-sdk with Apache License 2.0 5 votes vote down vote up
@Test
public void removeSingleListenerDoesNotAffectKeepSynced() throws Exception {
  DatabaseReference ref = IntegrationTestHelpers.getRandomNode();

  ref.keepSynced(true);
  assertIsKeptSynced(ref);

  // This will add and remove a listener.
  new ReadFuture(ref, (List<EventRecord> events) -> true).timedGet();

  assertIsKeptSynced(ref);

  // cleanup
  ref.keepSynced(false);
}
 
Example 18
Source File: KeepSyncedTest.java    From firebase-android-sdk with Apache License 2.0 5 votes vote down vote up
@Test
public void keepSyncedAffectsQueries() throws Exception {
  DatabaseReference ref = IntegrationTestHelpers.getRandomNode();
  ref.keepSynced(true);
  Query query = ref.limitToFirst(5);
  query.keepSynced(true);
  assertIsKeptSynced(ref);

  ref.keepSynced(false);
  assertNotKeptSynced(ref);
  assertNotKeptSynced(
      query); // currently, setting false on the default query affects all queries at that
  // location
}
 
Example 19
Source File: UsersActivity.java    From ChatApp with Apache License 2.0 4 votes vote down vote up
@Override
protected void onCreate(Bundle savedInstanceState)
{
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_users);

    // RecyclerView related

    LinearLayoutManager linearLayoutManager = new LinearLayoutManager(getApplicationContext());

    RecyclerView recyclerView = findViewById(R.id.users_recycler);
    recyclerView.setHasFixedSize(true);
    recyclerView.setLayoutManager(linearLayoutManager);

    DividerItemDecoration mDividerItemDecoration = new DividerItemDecoration(recyclerView.getContext(), linearLayoutManager.getOrientation());
    recyclerView.addItemDecoration(mDividerItemDecoration);

    // Initializing Users database

    DatabaseReference usersDatabase = FirebaseDatabase.getInstance().getReference().child("Users");
    usersDatabase.keepSynced(true); // For offline use

    // Initializing adapter

    FirebaseRecyclerOptions<User> options = new FirebaseRecyclerOptions.Builder<User>().setQuery(usersDatabase.orderByChild("name"), User.class).build();

    adapter = new FirebaseRecyclerAdapter<User, UserHolder>(options)
    {
        @Override
        protected void onBindViewHolder(final UserHolder holder, int position, User model)
        {
            final String userid = getRef(position).getKey();

            holder.setHolder(userid);
            holder.getView().setOnClickListener(new View.OnClickListener()
            {
                @Override
                public void onClick(View view)
                {
                    Intent userProfileIntent = new Intent(UsersActivity.this, ProfileActivity.class);
                    userProfileIntent.putExtra("userid", userid);
                    startActivity(userProfileIntent);
                }
            });
        }

        @Override
        public UserHolder onCreateViewHolder(ViewGroup parent, int viewType)
        {
            View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.user, parent, false);

            return new UserHolder(UsersActivity.this, view, getApplicationContext());
        }
    };

    recyclerView.setAdapter(adapter);
}
 
Example 20
Source File: ChatFragment.java    From ChatApp with Apache License 2.0 4 votes vote down vote up
@Override
public View onCreateView(@NonNull LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState)
{
    final View view = inflater.inflate(R.layout.fragment_chat, container, false);

    String currentUserId = FirebaseAuth.getInstance().getCurrentUser().getUid();

    // Initialize Chat Database

    DatabaseReference chatDatabase = FirebaseDatabase.getInstance().getReference().child("Chat").child(currentUserId);
    chatDatabase.keepSynced(true); // For offline use

    // RecyclerView related

    LinearLayoutManager linearLayoutManager = new LinearLayoutManager(getContext());
    linearLayoutManager.setReverseLayout(true);
    linearLayoutManager.setStackFromEnd(true);

    RecyclerView recyclerView = view.findViewById(R.id.chat_recycler);
    recyclerView.setHasFixedSize(true);
    recyclerView.setLayoutManager(linearLayoutManager);

    DividerItemDecoration mDividerItemDecoration = new DividerItemDecoration(recyclerView.getContext(), linearLayoutManager.getOrientation());
    recyclerView.addItemDecoration(mDividerItemDecoration);

    // Initializing adapter

    FirebaseRecyclerOptions<Chat> options = new FirebaseRecyclerOptions.Builder<Chat>().setQuery(chatDatabase.orderByChild("timestamp"), Chat.class).build();

    adapter = new FirebaseRecyclerAdapter<Chat, ChatHolder>(options)
    {
        @Override
        public ChatHolder onCreateViewHolder(ViewGroup parent, int viewType)
        {
            View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.user, parent, false);

            return new ChatHolder(getActivity(), view, getContext());
        }

        @Override
        protected void onBindViewHolder(final ChatHolder holder, int position, final Chat model)
        {
            final String userid = getRef(position).getKey();

            holder.setHolder(userid, model.getMessage(), model.getTimestamp(), model.getSeen());
            holder.getView().setOnClickListener(new View.OnClickListener()
            {
                @Override
                public void onClick(View view)
                {
                    Intent chatIntent = new Intent(getContext(), ChatActivity.class);
                    chatIntent.putExtra("userid", userid);
                    startActivity(chatIntent);
                }
            });
        }

        @Override
        public void onDataChanged()
        {
            super.onDataChanged();

            TextView text = view.findViewById(R.id.f_chat_text);

            if(adapter.getItemCount() == 0)
            {
                text.setVisibility(View.VISIBLE);
            }
            else
            {
                text.setVisibility(View.GONE);
            }
        }
    };

    recyclerView.setAdapter(adapter);
    return view;
}