-
Notifications
You must be signed in to change notification settings - Fork 1.8k
Add RealmList.removeAllFromRealm and Realm.clear #2061
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
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -72,8 +72,10 @@ public void setUp() throws Exception { | |
|
|
||
| @After | ||
| public void tearDown() throws Exception { | ||
| testRealm.close(); | ||
| Realm.deleteRealm(testRealm.getConfiguration()); | ||
| if (testRealm != null) { | ||
| testRealm.close(); | ||
| Realm.deleteRealm(testRealm.getConfiguration()); | ||
| } | ||
| } | ||
|
|
||
| private RealmList<Dog> createNonManagedDogList() { | ||
|
|
@@ -979,4 +981,110 @@ public void testSettingListClearsOldItems() { | |
|
|
||
| assertEquals(1, two.getObjects().size()); | ||
| } | ||
|
|
||
| @Test | ||
| public void testRemoveAllFromRealm() { | ||
| Owner owner = testRealm.where(Owner.class).findFirst(); | ||
| RealmList<Dog> dogs = owner.getDogs(); | ||
| assertEquals(TEST_OBJECTS, dogs.size()); | ||
|
|
||
| testRealm.beginTransaction(); | ||
| dogs.removeAllFromRealm(); | ||
| testRealm.commitTransaction(); | ||
| assertEquals(0, dogs.size()); | ||
| assertEquals(0, testRealm.where(Dog.class).count()); | ||
| } | ||
|
|
||
| @Test | ||
| public void testRealmRemoveAllNotManagedList() { | ||
| Owner owner = testRealm.where(Owner.class).findFirst(); | ||
| RealmList<Dog> dogs = owner.getDogs(); | ||
| assertEquals(TEST_OBJECTS, dogs.size()); | ||
|
|
||
| RealmList<Dog> notManagedDogs = new RealmList<Dog>(); | ||
| for (Dog dog : dogs) { | ||
| notManagedDogs.add(dog); | ||
| } | ||
|
|
||
| testRealm.beginTransaction(); | ||
| notManagedDogs.removeAllFromRealm(); | ||
| testRealm.commitTransaction(); | ||
| assertEquals(0, dogs.size()); | ||
| assertEquals(0, notManagedDogs.size()); | ||
| assertEquals(0, testRealm.where(Dog.class).count()); | ||
| } | ||
|
|
||
| @Test | ||
| public void testRealmRemoveAllOutsideTransaction() { | ||
| Owner owner = testRealm.where(Owner.class).findFirst(); | ||
| RealmList<Dog> dogs = owner.getDogs(); | ||
| try { | ||
| dogs.removeAllFromRealm(); | ||
| fail("removeAllFromRealm should be called in a transaction."); | ||
| } catch (IllegalStateException e) { | ||
| assertEquals("Changing Realm data can only be done from inside a transaction.", e.getMessage()); | ||
| } | ||
| } | ||
|
|
||
| @Test | ||
| public void testRemoveAllFromListStandaloneObjectShouldThrow() { | ||
| final RealmList<Dog> list = new RealmList<Dog>(); | ||
|
|
||
| testRealm.beginTransaction(); | ||
| Dog dog1 = testRealm.where(Dog.class).findFirst(); | ||
| testRealm.commitTransaction(); | ||
| Dog dog2 = new Dog(); | ||
|
|
||
| list.add(dog1); | ||
| list.add(dog2); | ||
|
|
||
| testRealm.beginTransaction(); | ||
| try { | ||
| list.removeAllFromRealm(); | ||
| fail("Cannot remove a list with a standalone object in it!"); | ||
| } catch (IllegalStateException e) { | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Should this be an IllegalArgumentException instead?
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. No, the exception is sent from
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Ah, good point 👍 |
||
| assertEquals("Object malformed: missing object in Realm. Make sure to instantiate RealmObjects with" + | ||
| " Realm.createObject()", e.getMessage()); | ||
| } finally { | ||
| testRealm.cancelTransaction(); | ||
| } | ||
|
|
||
| assertEquals(TEST_OBJECTS, testRealm.where(Dog.class).count()); | ||
| assertEquals(2, list.size()); | ||
| } | ||
|
|
||
| @Test | ||
| public void testRemoveAllFromRealmEmptyList() { | ||
| RealmList<Dog> dogs = testRealm.where(Owner.class).findFirst().getDogs(); | ||
| assertEquals(TEST_OBJECTS, dogs.size()); | ||
|
|
||
| testRealm.beginTransaction(); | ||
| dogs.removeAllFromRealm(); | ||
| testRealm.commitTransaction(); | ||
| assertEquals(0, dogs.size()); | ||
| assertEquals(0, testRealm.where(Dog.class).count()); | ||
|
|
||
| // The dogs is empty now. | ||
| testRealm.beginTransaction(); | ||
| dogs.removeAllFromRealm(); | ||
| testRealm.commitTransaction(); | ||
| assertEquals(0, dogs.size()); | ||
| assertEquals(0, testRealm.where(Dog.class).count()); | ||
|
|
||
| } | ||
|
|
||
| @Test | ||
| public void testRemoveAllFromRealmInvalidListShouldThrow() { | ||
| RealmList<Dog> dogs = testRealm.where(Owner.class).findFirst().getDogs(); | ||
| assertEquals(TEST_OBJECTS, dogs.size()); | ||
| testRealm.close(); | ||
| testRealm = null; | ||
|
|
||
| try { | ||
| dogs.removeAllFromRealm(); | ||
| fail("dogs is invalid and it should throw an exception"); | ||
| } catch (IllegalStateException e) { | ||
| assertEquals("This Realm instance has already been closed, making it unusable.", e.getMessage()); | ||
| } | ||
| } | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -263,11 +263,12 @@ public void move(int oldPos, int newPos) { | |
| } | ||
|
|
||
| /** | ||
| * Removes all elements from this list, leaving it empty. | ||
| * Removes all elements from this list, leaving it empty. This method doesn't remove the objects from the Realm. | ||
| * | ||
| * @throws IllegalStateException if Realm instance has been closed or parent object has been removed. | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Missing whitespace between section and throws |
||
| * @see List#isEmpty | ||
| * @see List#size | ||
| * @see #removeAllFromRealm() | ||
| */ | ||
| @Override | ||
| public void clear() { | ||
|
|
@@ -299,6 +300,26 @@ public E remove(int location) { | |
| } | ||
| } | ||
|
|
||
| /** | ||
| * Removes all elements from this list and delete them from the corresponding Realm. This method can be called on a | ||
| * non-managed {@link RealmList} if all of the RealmObjects in the list are managed by Realm. | ||
| * | ||
| * @throws IllegalStateException if the Realm instance has been closed, the parent object has been removed, the | ||
| * method is called in a wrong thread or any RealmObject in the list is not managed by Realm. | ||
| * @see #clear() | ||
| */ | ||
| public void removeAllFromRealm() { | ||
| if (managedMode) { | ||
| checkValidView(); | ||
| view.removeAllTargetRows(); | ||
| } else { | ||
| for (RealmObject object : nonManagedList) { | ||
| object.removeFromRealm(); | ||
| } | ||
| nonManagedList.clear(); | ||
| } | ||
| } | ||
|
|
||
| /** | ||
| * Returns the element at the specified location in this list. | ||
| * | ||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
What does this test?
RealmListis not managed.RealmListcontains standalone object.I think we should split this into two tests.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
oh,
testRealmRemoveAllNotManagedList alreadyexists and it does not throw theIllegalStateException.so, this tests IllegalStateException will be thrown if the RealmList contains standalone object.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
See testRealmRemoveAllNotManagedList