Skip to content
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

Remove object from list #7724

Draft
wants to merge 6 commits into
base: master
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
27 changes: 27 additions & 0 deletions RealmSwift/List.swift
Original file line number Diff line number Diff line change
Expand Up @@ -154,6 +154,33 @@ public final class List<Element: RealmCollectionValue>: RLMSwiftCollectionBase,
rlmArray.removeObject(at: UInt(index))
}

/**
Removes an object in the list if present, otherwise throws an error. The object is not removed from the Realm that manages it.

- warning: This method may only be called during a write transaction.

- parameter object: The object to remove.
*/
public func remove(_ object: Element) {
guard let index = self.index(of: object) else {
throwRealmException("Object does not exist in List") // ???: Is this too harsh? Should method just return a bool or something?
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is an exception much? Should nothing happen if the object isn't present?

}
self.remove(at: index)
}

/**
Removes a sequence of objects in the list if present, otherwise throws an error. The objects are not removed from the Realm that manages it.

- warning: This method may only be called during a write transaction.

- parameter objects: The objects to remove.
*/
public func remove<S: Sequence>(objectsIn objects: S) where S.Iterator.Element == Element {
for obj in objects {
remove(obj)
}
}

/**
Removes all objects from the list. The objects are not removed from the Realm that manages them.

Expand Down
21 changes: 21 additions & 0 deletions RealmSwift/Tests/AnyRealmValueTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -387,6 +387,27 @@ class AnyRealmValueListTests<O: ObjectFactory, V: AnyValueFactory>: AnyRealmValu
assertThrows(array[1])
}

func testRemoveWithObject() {
assertThrows(array.remove(values[0]))

array.append(objectsIn: values)
assertEqual(values[0], array[0])
assertEqual(values[1], array[1])
assertEqual(values[2], array[2])
assertThrows(array[3])

array.remove(values[0])
assertThrows(array.remove(values[0]))
assertEqual(values[1], array[0])
assertEqual(values[2], array[1])
assertThrows(array[2])

array.remove(array[1])
assertThrows(array.remove(values[2])) // values[2] was already removed in line 406
assertEqual(values[1], array[0])
assertThrows(array[1])
}

func testRemoveLast() {
assertThrows(array.removeLast())

Expand Down
40 changes: 40 additions & 0 deletions RealmSwift/Tests/ListTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -244,6 +244,46 @@ class ListTests: TestCase {
assertThrows(array.remove(atOffsets: [1]))
}

func testRemoveObject() {
guard let array = array, let str1 = str1, let str2 = str2 else {
fatalError("Test precondition failure")
}

array.append(objectsIn: [str1, str2, str1])
array.remove(str2)

XCTAssertEqual(array.count, 2)
assertEqual(array[0], str1)
assertEqual(array[1], str1)
assertThrows(array.remove(str2))
}

func testRemoveWhenDuplicates() {
guard let array = array, let str1 = str1, let str2 = str2 else {
fatalError("Test precondition failure")
}

array.append(objectsIn: [str1, str2, str1])
array.remove(str1)

XCTAssertEqual(array.count, 2)
assertEqual(array[0], str2) // Expect the str1 that was at index 0 to be removed
assertEqual(array[1], str1)
}

func testRemoveSequence() {
guard let array = array, let str1 = str1, let str2 = str2 else {
fatalError("Test precondition failure")
}

array.append(objectsIn: [str1, str2, str1])
array.remove(objectsIn: [str2, str1])

XCTAssertEqual(array.count, 1)
assertEqual(array[0], str1)
assertThrows(array.remove(str2))
}

func testRemoveLast() {
guard let array = array, let str1 = str1, let str2 = str2 else {
fatalError("Test precondition failure")
Expand Down
1 change: 0 additions & 1 deletion RealmSwift/Tests/SwiftUITests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -150,7 +150,6 @@ class SwiftUITests: TestCase {
state.projectedValue.remove(at: 0)
XCTAssertEqual(state.wrappedValue.count, 0)
}

func testManagedListAppendRemoveObservedObject() throws {
let object = SwiftUIObject()
var state = StateRealmObject(wrappedValue: object.list)
Expand Down