-
Notifications
You must be signed in to change notification settings - Fork 1.9k
fix(database): fix DatabasePagingSource pagination when using orderByChild #2336
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
66db2bd
fix(database): fix DatabasePagingSource pagination when using orderBy…
demolaf 5df651d
fix(database): make DatabasePagingKey public and add equals/hashCode
demolaf a9e554b
fix(database): surface load errors to logcat and fix lifecycle observ…
demolaf bbd4687
fix(database): extend orderByValue() cursor support and use shared Te…
demolaf File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
104 changes: 104 additions & 0 deletions
104
database/src/androidTest/java/com/firebase/ui/database/paging/DatabasePagingSourceTest.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,104 @@ | ||
| package com.firebase.ui.database.paging; | ||
|
|
||
| import androidx.paging.PagingSource; | ||
| import androidx.test.core.app.ApplicationProvider; | ||
| import androidx.test.ext.junit.runners.AndroidJUnit4; | ||
|
|
||
| import com.firebase.ui.database.Bean; | ||
| import com.firebase.ui.database.TestUtils; | ||
| import com.google.firebase.FirebaseApp; | ||
| import com.google.firebase.database.DataSnapshot; | ||
| import com.google.firebase.database.DatabaseError; | ||
| import com.google.firebase.database.DatabaseReference; | ||
| import com.google.firebase.database.FirebaseDatabase; | ||
| import com.google.firebase.database.ValueEventListener; | ||
|
|
||
| import org.junit.After; | ||
| import org.junit.Before; | ||
| import org.junit.Test; | ||
| import org.junit.runner.RunWith; | ||
|
|
||
| import java.util.HashSet; | ||
| import java.util.Set; | ||
| import java.util.concurrent.CountDownLatch; | ||
| import java.util.concurrent.TimeUnit; | ||
|
|
||
| import androidx.annotation.NonNull; | ||
|
|
||
| import static org.junit.Assert.assertEquals; | ||
| import static org.junit.Assert.assertTrue; | ||
|
|
||
| @RunWith(AndroidJUnit4.class) | ||
| public class DatabasePagingSourceTest { | ||
| private static final int PAGE_SIZE = 3; | ||
| private static final int TOTAL_ITEMS = 6; | ||
|
|
||
| private DatabaseReference mRef; | ||
|
|
||
| @Before | ||
| public void setUp() throws InterruptedException { | ||
| FirebaseApp app = TestUtils.getAppInstance(ApplicationProvider.getApplicationContext()); | ||
| mRef = FirebaseDatabase.getInstance(app).getReference().child("paging_test"); | ||
|
|
||
| mRef.removeValue(); | ||
| for (int i = 1; i <= TOTAL_ITEMS; i++) { | ||
| mRef.push().setValue(new Bean(i)); | ||
| } | ||
|
|
||
| CountDownLatch latch = new CountDownLatch(1); | ||
| mRef.addValueEventListener(new ValueEventListener() { | ||
| @Override | ||
| public void onDataChange(@NonNull DataSnapshot snapshot) { | ||
| if (snapshot.getChildrenCount() >= TOTAL_ITEMS) { | ||
| mRef.removeEventListener(this); | ||
| latch.countDown(); | ||
| } | ||
| } | ||
|
|
||
| @Override | ||
| public void onCancelled(@NonNull DatabaseError error) {} | ||
| }); | ||
| assertTrue("Timed out seeding test data", latch.await(30, TimeUnit.SECONDS)); | ||
| } | ||
|
|
||
| @After | ||
| public void tearDown() { | ||
| mRef.removeValue(); | ||
| } | ||
|
|
||
| @Test | ||
| public void testOrderByChild_noDuplicatesAcrossPages() { | ||
| DatabasePagingSource source = new DatabasePagingSource(mRef.orderByChild("number")); | ||
|
|
||
| PagingSource.LoadResult<DatabasePagingKey, DataSnapshot> result1 = | ||
| source.loadSingle(new PagingSource.LoadParams.Refresh<>(null, PAGE_SIZE, false)) | ||
| .timeout(30, TimeUnit.SECONDS) | ||
| .blockingGet(); | ||
|
|
||
| assertTrue(result1 instanceof PagingSource.LoadResult.Page); | ||
| PagingSource.LoadResult.Page<DatabasePagingKey, DataSnapshot> page1 = | ||
| (PagingSource.LoadResult.Page<DatabasePagingKey, DataSnapshot>) result1; | ||
| assertEquals(PAGE_SIZE, page1.getData().size()); | ||
|
|
||
| DatabasePagingKey nextKey = page1.getNextKey(); | ||
|
|
||
| PagingSource.LoadResult<DatabasePagingKey, DataSnapshot> result2 = | ||
| source.loadSingle(new PagingSource.LoadParams.Append<>(nextKey, PAGE_SIZE, false)) | ||
| .timeout(30, TimeUnit.SECONDS) | ||
| .blockingGet(); | ||
|
|
||
| assertTrue(result2 instanceof PagingSource.LoadResult.Page); | ||
| PagingSource.LoadResult.Page<DatabasePagingKey, DataSnapshot> page2 = | ||
| (PagingSource.LoadResult.Page<DatabasePagingKey, DataSnapshot>) result2; | ||
|
|
||
| Set<String> allKeys = new HashSet<>(); | ||
| for (DataSnapshot snapshot : page1.getData()) { | ||
| allKeys.add(snapshot.getKey()); | ||
| } | ||
| for (DataSnapshot snapshot : page2.getData()) { | ||
| assertTrue("Duplicate key across pages: " + snapshot.getKey(), | ||
| allKeys.add(snapshot.getKey())); | ||
| } | ||
| assertEquals(TOTAL_ITEMS, allKeys.size()); | ||
| } | ||
| } |
35 changes: 35 additions & 0 deletions
35
database/src/main/java/com/firebase/ui/database/paging/DatabasePagingKey.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,35 @@ | ||
| package com.firebase.ui.database.paging; | ||
|
|
||
| import java.util.Objects; | ||
|
|
||
| public class DatabasePagingKey { | ||
| private final Object mChildValue; | ||
| private final String mNodeKey; | ||
|
|
||
| public DatabasePagingKey(Object childValue, String nodeKey) { | ||
| mChildValue = childValue; | ||
| mNodeKey = nodeKey; | ||
| } | ||
|
|
||
| public Object getChildValue() { | ||
| return mChildValue; | ||
| } | ||
|
|
||
| public String getNodeKey() { | ||
| return mNodeKey; | ||
| } | ||
|
|
||
| @Override | ||
| public boolean equals(Object o) { | ||
| if (this == o) return true; | ||
| if (o == null || getClass() != o.getClass()) return false; | ||
| DatabasePagingKey that = (DatabasePagingKey) o; | ||
| return Objects.equals(mChildValue, that.mChildValue) && | ||
| Objects.equals(mNodeKey, that.mNodeKey); | ||
| } | ||
|
|
||
| @Override | ||
| public int hashCode() { | ||
| return Objects.hash(mChildValue, mNodeKey); | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.