MutableSet
public final class MutableSet<Element> : RLMSwiftCollectionBase where Element : RealmCollectionValue
extension MutableSet: ObservableObject, RealmSubscribable
extension MutableSet: RealmCollection
extension MutableSet: Decodable where Element: Decodable
extension MutableSet: Encodable where Element: Encodable
MutableSet
is the container type in Realm used to define to-many relationships with distinct values as objects.
Like Swift’s Set
, MutableSet
is a generic type that is parameterized on the type it stores. This can be either an Object
subclass or one of the following types: Bool
, Int
, Int8
, Int16
, Int32
, Int64
, Float
, Double
,
String
, Data
, Date
, Decimal128
, and ObjectId
(and their optional versions)
Unlike Swift’s native collections, MutableSet
s are reference types, and are only immutable if the Realm that manages them
is opened as read-only.
MutableSet’s can be filtered and sorted with the same predicates as Results<Element>
.
Properties of MutableSet
type defined on Object
subclasses must be declared as let
and cannot be dynamic
.
-
The Realm which manages the set, or
nil
if the set is unmanaged.Declaration
Swift
public var realm: Realm? { get }
-
Indicates if the set can no longer be accessed.
Declaration
Swift
public var isInvalidated: Bool { get }
-
Creates a
MutableSet
that holds Realm model objects of typeElement
.Declaration
Swift
public override init()
-
Returns the number of objects in this MutableSet.
Declaration
Swift
public var count: Int { get }
-
Returns an
Array
containing the results of invokingvalueForKey(_:)
usingkey
on each of the collection’s objects.Declaration
Swift
@nonobjc public func value(forKey key: String) -> [AnyObject]
-
Returns an
Array
containing the results of invokingvalueForKeyPath(_:)
usingkeyPath
on each of the collection’s objects.Declaration
Swift
@nonobjc public func value(forKeyPath keyPath: String) -> [AnyObject]
Parameters
keyPath
The key path to the property whose values are desired.
-
Invokes
setValue(_:forKey:)
on each of the collection’s objects using the specifiedvalue
andkey
.Warning
This method can only be called during a write transaction.
Declaration
Swift
public override func setValue(_ value: Any?, forKey key: String)
Parameters
value
The object value.
key
The name of the property whose value should be set on each object.
-
Returns a Boolean value indicating whether the Set contains the given object.
Declaration
Swift
public func contains(_ object: Element) -> Bool
Parameters
object
The element to find in the MutableSet.
-
Returns a Boolean value that indicates whether this set is a subset of the given set.
Declaration
Swift
public func isSubset(of possibleSuperset: MutableSet<Element>) -> Bool
Parameters
object
Another MutableSet to compare.
-
Returns a Boolean value that indicates whether this set intersects with another given set.
Declaration
Swift
public func intersects(_ otherSet: MutableSet<Element>) -> Bool
Parameters
object
Another MutableSet to compare.
-
Returns a
Results
containing the objects in the set, but sorted.Objects are sorted based on the values of the given key path. For example, to sort a set of
Student
s from youngest to oldest based on theirage
property, you might callstudents.sorted(byKeyPath: "age", ascending: true)
.Warning
MutableSets may only be sorted by properties of boolean,
Date
,NSDate
, single and double-precision floating point, integer, and string types.Declaration
Swift
public func sorted(byKeyPath keyPath: String, ascending: Bool = true) -> Results<Element>
Parameters
keyPath
The key path to sort by.
ascending
The direction to sort in.
-
Returns a
Results
containing the objects in the set, but sorted.Warning
MutableSets may only be sorted by properties of boolean,
Date
,NSDate
, single and double-precision floating point, integer, and string types.Declaration
Swift
public func sorted<S: Sequence>(by sortDescriptors: S) -> Results<Element> where S.Iterator.Element == SortDescriptor
-
Returns the minimum (lowest) value of the given property among all the objects in the set, or
nil
if the set is empty.Warning
Only a property whose type conforms to the
MinMaxType
protocol can be specified.Declaration
Swift
public func min<T>(ofProperty property: String) -> T? where T : MinMaxType
Parameters
property
The name of a property whose minimum value is desired.
-
Returns the maximum (highest) value of the given property among all the objects in the set, or
nil
if the set is empty.Warning
Only a property whose type conforms to the
MinMaxType
protocol can be specified.Declaration
Swift
public func max<T>(ofProperty property: String) -> T? where T : MinMaxType
Parameters
property
The name of a property whose maximum value is desired.
-
Returns the sum of the values of a given property over all the objects in the set.
Warning
Only a property whose type conforms to the
AddableType
protocol can be specified.Declaration
Swift
public func sum<T>(ofProperty property: String) -> T where T : AddableType
Parameters
property
The name of a property whose values should be summed.
-
Returns the average value of a given property over all the objects in the set, or
nil
if the set is empty.Warning
Only a property whose type conforms to the
AddableType
protocol can be specified.Declaration
Swift
public func average<T>(ofProperty property: String) -> T? where T : AddableType
Parameters
property
The name of a property whose average value should be calculated.
-
Inserts an object to the set if not already present.
Warning
This method may only be called during a write transaction.
Declaration
Swift
public func insert(_ object: Element)
Parameters
object
An object.
-
Inserts the given sequence of objects into the set if not already present.
Warning
This method may only be called during a write transaction.Declaration
Swift
public func insert<S>(objectsIn objects: S) where Element == S.Element, S : Sequence
-
Removes an object in the set if present. The object is not removed from the Realm that manages it.
Warning
This method may only be called during a write transaction.
Declaration
Swift
public func remove(_ object: Element)
Parameters
object
The object to remove.
-
Removes all objects from the set. The objects are not removed from the Realm that manages them.
Warning
This method may only be called during a write transaction.Declaration
Swift
public func removeAll()
-
Mutates the set in place with the elements that are common to both this set and the given sequence.
Warning
This method may only be called during a write transaction.
Declaration
Swift
public func formIntersection(_ other: MutableSet<Element>)
Parameters
other
Another set.
-
Mutates the set in place and removes the elements of the given set from this set.
Warning
This method may only be called during a write transaction.
Declaration
Swift
public func subtract(_ other: MutableSet<Element>)
Parameters
other
Another set.
-
Inserts the elements of the given sequence into the set.
Warning
This method may only be called during a write transaction.
Declaration
Swift
public func formUnion(_ other: MutableSet<Element>)
Parameters
other
Another set.
-
Registers a block to be called each time the collection changes.
The block will be asynchronously called with the initial results, and then called again after each write transaction which changes either any of the objects in the collection, or which objects are in the collection.
The
change
parameter that is passed to the block reports, in the form of indices within the collection, which of the objects were added, removed, or modified during each write transaction. See theRealmCollectionChange
documentation for more information on the change information supplied and an example of how to use it to update aUITableView
.At the time when the block is called, the collection will be fully evaluated and up-to-date, and as long as you do not perform a write transaction on the same thread or explicitly call
realm.refresh()
, accessing it will never perform blocking work.If no queue is given, notifications are delivered via the standard run loop, and so can’t be delivered while the run loop is blocked by other activity. If a queue is given, notifications are delivered to that queue instead. When notifications can’t be delivered instantly, multiple notifications may be coalesced into a single notification. This can include the notification with the initial collection.
For example, the following code performs a write transaction immediately after adding the notification block, so there is no opportunity for the initial notification to be delivered first. As a result, the initial notification will reflect the state of the Realm after the write transaction.
let results = realm.objects(Dog.self) print("dogs.count: \(dogs?.count)") // => 0 let token = dogs.observe { changes in switch changes { case .initial(let dogs): // Will print "dogs.count: 1" print("dogs.count: \(dogs.count)") break case .update: // Will not be hit in this example break case .error: break } } try! realm.write { let dog = Dog() dog.name = "Rex" person.dogs.insert(dog) } // end of run loop execution context
You must retain the returned token for as long as you want updates to be sent to the block. To stop receiving updates, call
invalidate()
on the token.Warning
This method cannot be called during a write transaction, or when the containing Realm is read-only.
Declaration
Swift
public func observe(on queue: DispatchQueue? = nil, _ block: @escaping (RealmCollectionChange<MutableSet>) -> Void) -> NotificationToken
Parameters
queue
The serial dispatch queue to receive notification on. If
nil
, notifications are delivered to the current thread.block
The block to be called whenever a change occurs.
Return Value
A token which must be held for as long as you want updates to be delivered.
-
Declaration
Swift
public var isFrozen: Bool { get }
-
Declaration
Swift
public func freeze() -> MutableSet
-
Declaration
Swift
public func thaw() -> MutableSet?
-
Returns a human-readable description of the objects contained in the MutableSet.
Declaration
Swift
@objc public override var description: String { get }
-
A publisher that emits Void each time the collection changes.
Despite the name, this actually emits after the collection has changed.
Declaration
Swift
public var objectWillChange: RealmPublishers.WillChange<MutableSet> { get }
-
The type of the objects stored within the set.
Declaration
Swift
public typealias ElementType = Element
-
Returns a
RLMIterator
that yields successive elements in theMutableSet
.Declaration
Swift
public func makeIterator() -> RLMIterator<Element>
-
The position of the first element in a non-empty collection. Identical to endIndex in an empty collection.
Declaration
Swift
public var startIndex: Int { get }
-
The collection’s “past the end” position. endIndex is not a valid argument to subscript, and is always reachable from startIndex by zero or more applications of successor().
Declaration
Swift
public var endIndex: Int { get }
-
Declaration
Swift
public func index(after i: Int) -> Int
-
Declaration
Swift
public func index(before i: Int) -> Int
-
Warning
Ordering is not guaranteed on a MutableSet. Subscripting is implemented for convenience should not be relied on.Declaration
Swift
public subscript(position: Int) -> Element { get }
-
Warning
Ordering is not guaranteed on a MutableSet.first
is implemented for convenience should not be relied on.Declaration
Swift
public var first: Element? { get }
-
Returns the minimum (lowest) value in the set, or
nil
if the set is empty.Declaration
Swift
public func min() -> Element?
-
Returns the maximum (highest) value in the set, or
nil
if the set is empty.Declaration
Swift
public func max() -> Element?
-
Returns the sum of the values in the set.
Declaration
Swift
public func sum() -> Element
-
Returns the average of the values in the set, or
nil
if the set is empty.Declaration
Swift
public func average<T>() -> T? where T : AddableType
-
Declaration
Swift
public convenience init(from decoder: Decoder) throws
-
Declaration
Swift
public func encode(to encoder: Encoder) throws