Aliases.swift 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120
  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. // These types don't change when wrapping in Swift
  21. // so we just typealias them to remove the 'RLM' prefix
  22. // MARK: Aliases
  23. /**
  24. `PropertyType` is an enum describing all property types supported in Realm models.
  25. For more information, see [Realm Models](https://realm.io/docs/swift/latest/#models).
  26. ### Primitive types
  27. * `Int`
  28. * `Bool`
  29. * `Float`
  30. * `Double`
  31. ### Object types
  32. * `String`
  33. * `Data`
  34. * `Date`
  35. * `Decimal128`
  36. * `ObjectId`
  37. ### Relationships: Array (in Swift, `List`) and `Object` types
  38. * `Object`
  39. * `Array`
  40. */
  41. public typealias PropertyType = RLMPropertyType
  42. /**
  43. An opaque token which is returned from methods which subscribe to changes to a Realm.
  44. - see: `Realm.observe(_:)`
  45. */
  46. public typealias NotificationToken = RLMNotificationToken
  47. /// :nodoc:
  48. public typealias ObjectBase = RLMObjectBase
  49. extension ObjectBase {
  50. /**
  51. Registers a block to be called each time the object changes.
  52. The block will be asynchronously called after each write transaction which
  53. deletes the object or modifies any of the managed properties of the object,
  54. including self-assignments that set a property to its existing value.
  55. For write transactions performed on different threads or in different
  56. processes, the block will be called when the managing Realm is
  57. (auto)refreshed to a version including the changes, while for local write
  58. transactions it will be called at some point in the future after the write
  59. transaction is committed.
  60. If no queue is given, notifications are delivered via the standard run
  61. loop, and so can't be delivered while the run loop is blocked by other
  62. activity. If a queue is given, notifications are delivered to that queue
  63. instead. When notifications can't be delivered instantly, multiple
  64. notifications may be coalesced into a single notification.
  65. Unlike with `List` and `Results`, there is no "initial" callback made after
  66. you add a new notification block.
  67. Only objects which are managed by a Realm can be observed in this way. You
  68. must retain the returned token for as long as you want updates to be sent
  69. to the block. To stop receiving updates, call `invalidate()` on the token.
  70. It is safe to capture a strong reference to the observed object within the
  71. callback block. There is no retain cycle due to that the callback is
  72. retained by the returned token and not by the object itself.
  73. - warning: This method cannot be called during a write transaction, or when
  74. the containing Realm is read-only.
  75. - parameter queue: The serial dispatch queue to receive notification on. If
  76. `nil`, notifications are delivered to the current thread.
  77. - parameter block: The block to call with information about changes to the object.
  78. - returns: A token which must be held for as long as you want updates to be delivered.
  79. */
  80. // swiftlint:disable:next identifier_name
  81. internal func _observe<T: ObjectBase>(on queue: DispatchQueue? = nil,
  82. _ block: @escaping (ObjectChange<T>) -> Void) -> NotificationToken {
  83. return RLMObjectBaseAddNotificationBlock(self, queue) { object, names, oldValues, newValues, error in
  84. if let error = error {
  85. block(.error(error as NSError))
  86. return
  87. }
  88. guard let names = names, let newValues = newValues else {
  89. block(.deleted)
  90. return
  91. }
  92. block(.change(object as! T, (0..<newValues.count).map { i in
  93. PropertyChange(name: names[i], oldValue: oldValues?[i], newValue: newValues[i])
  94. }))
  95. }
  96. }
  97. }