RLMThreadSafeReference.h 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  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 <Foundation/Foundation.h>
  19. @class RLMRealm;
  20. NS_ASSUME_NONNULL_BEGIN
  21. /**
  22. Objects of types which conform to `RLMThreadConfined` can be managed by a Realm, which will make
  23. them bound to a thread-specific `RLMRealm` instance. Managed objects must be explicitly exported
  24. and imported to be passed between threads.
  25. Managed instances of objects conforming to this protocol can be converted to a thread-safe
  26. reference for transport between threads by passing to the
  27. `+[RLMThreadSafeReference referenceWithThreadConfined:]` constructor.
  28. Note that only types defined by Realm can meaningfully conform to this protocol, and defining new
  29. classes which attempt to conform to it will not make them work with `RLMThreadSafeReference`.
  30. */
  31. @protocol RLMThreadConfined <NSObject>
  32. // Conformance to the `RLMThreadConfined_Private` protocol will be enforced at runtime.
  33. /**
  34. The Realm which manages the object, or `nil` if the object is unmanaged.
  35. Unmanaged objects are not confined to a thread and cannot be passed to methods expecting a
  36. `RLMThreadConfined` object.
  37. */
  38. @property (nonatomic, readonly, nullable) RLMRealm *realm;
  39. /// Indicates if the object can no longer be accessed because it is now invalid.
  40. @property (nonatomic, readonly, getter = isInvalidated) BOOL invalidated;
  41. @end
  42. /**
  43. An object intended to be passed between threads containing a thread-safe reference to its
  44. thread-confined object.
  45. To resolve a thread-safe reference on a target Realm on a different thread, pass to
  46. `-[RLMRealm resolveThreadSafeReference:]`.
  47. @warning A `RLMThreadSafeReference` object must be resolved at most once.
  48. Failing to resolve a `RLMThreadSafeReference` will result in the source version of the
  49. Realm being pinned until the reference is deallocated.
  50. @note Prefer short-lived `RLMThreadSafeReference`s as the data for the version of the source Realm
  51. will be retained until all references have been resolved or deallocated.
  52. @see `RLMThreadConfined`
  53. @see `-[RLMRealm resolveThreadSafeReference:]`
  54. */
  55. @interface RLMThreadSafeReference<__covariant Confined : id<RLMThreadConfined>> : NSObject
  56. /**
  57. Create a thread-safe reference to the thread-confined object.
  58. @param threadConfined The thread-confined object to create a thread-safe reference to.
  59. @note You may continue to use and access the thread-confined object after passing it to this
  60. constructor.
  61. */
  62. + (instancetype)referenceWithThreadConfined:(Confined)threadConfined;
  63. /**
  64. Indicates if the reference can no longer be resolved because an attempt to resolve it has already
  65. occurred. References can only be resolved once.
  66. */
  67. @property (nonatomic, readonly, getter = isInvalidated) BOOL invalidated;
  68. #pragma mark - Unavailable Methods
  69. /**
  70. `-[RLMThreadSafeReference init]` is not available because `RLMThreadSafeReference` cannot be
  71. created directly. `RLMThreadSafeReference` instances must be obtained by calling
  72. `-[RLMRealm resolveThreadSafeReference:]`.
  73. */
  74. - (instancetype)init __attribute__((unavailable("RLMThreadSafeReference cannot be created directly")));
  75. /**
  76. `-[RLMThreadSafeReference new]` is not available because `RLMThreadSafeReference` cannot be
  77. created directly. `RLMThreadSafeReference` instances must be obtained by calling
  78. `-[RLMRealm resolveThreadSafeReference:]`.
  79. */
  80. + (instancetype)new __attribute__((unavailable("RLMThreadSafeReference cannot be created directly")));
  81. @end
  82. NS_ASSUME_NONNULL_END