Results.swift 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152
  1. ////////////////////////////////////////////////////////////////////////////
  2. //
  3. // Copyright 2014 Realm Inc.
  4. //
  5. // Licensed under the Apache License, Version 2.0 (the "License");
  6. // you may not use this file except in compliance with the License.
  7. // You may obtain a copy of the License at
  8. //
  9. // http://www.apache.org/licenses/LICENSE-2.0
  10. //
  11. // Unless required by applicable law or agreed to in writing, software
  12. // distributed under the License is distributed on an "AS IS" BASIS,
  13. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  14. // See the License for the specific language governing permissions and
  15. // limitations under the License.
  16. //
  17. ////////////////////////////////////////////////////////////////////////////
  18. import Foundation
  19. import Realm
  20. // MARK: MinMaxType
  21. /**
  22. Types of properties which can be used with the minimum and maximum value APIs.
  23. - see: `min(ofProperty:)`, `max(ofProperty:)`
  24. */
  25. @_marker public protocol MinMaxType {}
  26. extension NSNumber: MinMaxType {}
  27. extension Double: MinMaxType {}
  28. extension Float: MinMaxType {}
  29. extension Int: MinMaxType {}
  30. extension Int8: MinMaxType {}
  31. extension Int16: MinMaxType {}
  32. extension Int32: MinMaxType {}
  33. extension Int64: MinMaxType {}
  34. extension Date: MinMaxType {}
  35. extension NSDate: MinMaxType {}
  36. extension Decimal128: MinMaxType {}
  37. extension AnyRealmValue: MinMaxType {}
  38. extension Optional: MinMaxType where Wrapped: MinMaxType {}
  39. // MARK: AddableType
  40. /**
  41. Types of properties which can be used with the sum and average value APIs.
  42. - see: `sum(ofProperty:)`, `average(ofProperty:)`
  43. */
  44. @_marker public protocol AddableType {}
  45. extension NSNumber: AddableType {}
  46. extension Double: AddableType {}
  47. extension Float: AddableType {}
  48. extension Int: AddableType {}
  49. extension Int8: AddableType {}
  50. extension Int16: AddableType {}
  51. extension Int32: AddableType {}
  52. extension Int64: AddableType {}
  53. extension Decimal128: AddableType {}
  54. extension AnyRealmValue: AddableType {}
  55. extension Optional: AddableType where Wrapped: AddableType {}
  56. /**
  57. Types of properties which can be directly sorted or distincted.
  58. - see: `sum(ascending:)`, `distinct()`
  59. */
  60. @_marker public protocol SortableType {}
  61. extension AnyRealmValue: SortableType {}
  62. extension Data: SortableType {}
  63. extension Date: SortableType {}
  64. extension Decimal128: SortableType {}
  65. extension Double: SortableType {}
  66. extension Float: SortableType {}
  67. extension Int16: SortableType {}
  68. extension Int32: SortableType {}
  69. extension Int64: SortableType {}
  70. extension Int8: SortableType {}
  71. extension Int: SortableType {}
  72. extension String: SortableType {}
  73. extension Optional: SortableType where Wrapped: SortableType {}
  74. /**
  75. Types which have properties that can be sorted or distincted on.
  76. */
  77. @_marker public protocol KeypathSortable {}
  78. extension ObjectBase: KeypathSortable {}
  79. extension Projection: KeypathSortable {}
  80. /**
  81. `Results` is an auto-updating container type in Realm returned from object queries.
  82. `Results` can be queried with the same predicates as `List<Element>`, and you can
  83. chain queries to further filter query results.
  84. `Results` always reflect the current state of the Realm on the current thread, including during write transactions on
  85. the current thread. The one exception to this is when using `for...in` enumeration, which will always enumerate over
  86. the objects which matched the query when the enumeration is begun, even if some of them are deleted or modified to be
  87. excluded by the filter during the enumeration.
  88. `Results` are lazily evaluated the first time they are accessed; they only run queries when the result of the query is
  89. requested. This means that chaining several temporary `Results` to sort and filter your data does not perform any
  90. unnecessary work processing the intermediate state.
  91. Once the results have been evaluated or a notification block has been added, the results are eagerly kept up-to-date,
  92. with the work done to keep them up-to-date done on a background thread whenever possible.
  93. Results instances cannot be directly instantiated.
  94. */
  95. @frozen public struct Results<Element: RealmCollectionValue>: Equatable, RealmCollectionImpl {
  96. internal let collection: RLMCollection
  97. /// A human-readable description of the objects represented by the results.
  98. public var description: String {
  99. return RLMDescriptionWithMaxDepth("Results", collection, RLMDescriptionMaxDepth)
  100. }
  101. // MARK: Initializers
  102. internal init(collection: RLMCollection) {
  103. self.collection = collection
  104. }
  105. internal init(_ collection: RLMCollection) {
  106. self.collection = collection
  107. }
  108. // MARK: Object Retrieval
  109. /**
  110. Returns the object at the given `index`.
  111. - parameter index: The index.
  112. */
  113. public subscript(position: Int) -> Element {
  114. throwForNegativeIndex(position)
  115. return staticBridgeCast(fromObjectiveC: collection.object(at: UInt(position)))
  116. }
  117. // MARK: Equatable
  118. public static func == (lhs: Results<Element>, rhs: Results<Element>) -> Bool {
  119. lhs.collection.isEqual(rhs.collection)
  120. }
  121. /// :nodoc:
  122. public func makeIterator() -> RLMIterator<Element> {
  123. return RLMIterator(collection: collection)
  124. }
  125. }
  126. extension Results: Encodable where Element: Encodable {}