ThreadSafeReference.swift 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120
  1. ////////////////////////////////////////////////////////////////////////////
  2. //
  3. // Copyright 2016 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 Realm
  19. /**
  20. Objects of types which conform to `ThreadConfined` can be managed by a Realm, which will make
  21. them bound to a thread-specific `Realm` instance. Managed objects must be explicitly exported
  22. and imported to be passed between threads.
  23. Managed instances of objects conforming to this protocol can be converted to a thread-safe
  24. reference for transport between threads by passing to the `ThreadSafeReference(to:)` constructor.
  25. Note that only types defined by Realm can meaningfully conform to this protocol, and defining new
  26. classes which attempt to conform to it will not make them work with `ThreadSafeReference`.
  27. */
  28. public protocol ThreadConfined {
  29. // Must also conform to `AssistedObjectiveCBridgeable`
  30. /**
  31. The Realm which manages the object, or `nil` if the object is unmanaged.
  32. Unmanaged objects are not confined to a thread and cannot be passed to methods expecting a
  33. `ThreadConfined` object.
  34. */
  35. var realm: Realm? { get }
  36. /// Indicates if the object can no longer be accessed because it is now invalid.
  37. var isInvalidated: Bool { get }
  38. }
  39. /**
  40. An object intended to be passed between threads containing a thread-safe reference to its
  41. thread-confined object.
  42. To resolve a thread-safe reference on a target Realm on a different thread, pass to
  43. `Realm.resolve(_:)`.
  44. - warning: A `ThreadSafeReference` object must be resolved at most once.
  45. Failing to resolve a `ThreadSafeReference` will result in the source version of the
  46. Realm being pinned until the reference is deallocated.
  47. - note: Prefer short-lived `ThreadSafeReference`s as the data for the version of the source Realm
  48. will be retained until all references have been resolved or deallocated.
  49. - see: `ThreadConfined`
  50. - see: `Realm.resolve(_:)`
  51. */
  52. public class ThreadSafeReference<Confined: ThreadConfined> {
  53. private let swiftMetadata: Any?
  54. /**
  55. Indicates if the reference can no longer be resolved because an attempt to resolve it has
  56. already occurred. References can only be resolved once.
  57. */
  58. public var isInvalidated: Bool { return objectiveCReference.isInvalidated }
  59. private let objectiveCReference: RLMThreadSafeReference<RLMThreadConfined>
  60. /**
  61. Create a thread-safe reference to the thread-confined object.
  62. - parameter threadConfined: The thread-confined object to create a thread-safe reference to.
  63. - note: You may continue to use and access the thread-confined object after passing it to this
  64. constructor.
  65. */
  66. public init(to threadConfined: Confined) {
  67. let bridged = (threadConfined as! AssistedObjectiveCBridgeable).bridged
  68. swiftMetadata = bridged.metadata
  69. objectiveCReference = RLMThreadSafeReference(threadConfined: bridged.objectiveCValue as! RLMThreadConfined)
  70. }
  71. internal func resolve(in realm: Realm) -> Confined? {
  72. guard let objectiveCValue = realm.rlmRealm.__resolve(objectiveCReference) else { return nil }
  73. return ((Confined.self as! AssistedObjectiveCBridgeable.Type).bridging(from: objectiveCValue, with: swiftMetadata) as! Confined)
  74. }
  75. }
  76. extension Realm {
  77. // MARK: Thread Safe Reference
  78. /**
  79. Returns the same object as the one referenced when the `ThreadSafeReference` was first
  80. created, but resolved for the current Realm for this thread. Returns `nil` if this object was
  81. deleted after the reference was created.
  82. - parameter reference: The thread-safe reference to the thread-confined object to resolve in
  83. this Realm.
  84. - warning: A `ThreadSafeReference` object must be resolved at most once.
  85. Failing to resolve a `ThreadSafeReference` will result in the source version of the
  86. Realm being pinned until the reference is deallocated.
  87. An exception will be thrown if a reference is resolved more than once.
  88. - warning: Cannot call within a write transaction.
  89. - note: Will refresh this Realm if the source Realm was at a later version than this one.
  90. - see: `ThreadSafeReference(to:)`
  91. */
  92. public func resolve<Confined>(_ reference: ThreadSafeReference<Confined>) -> Confined? {
  93. return reference.resolve(in: self)
  94. }
  95. }