1 ////////////////////////////////////////////////////////////////////////////
3 // Copyright 2016 Realm Inc.
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
9 // http://www.apache.org/licenses/LICENSE-2.0
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.
17 ////////////////////////////////////////////////////////////////////////////
22 Objects of types which conform to `ThreadConfined` can be managed by a Realm, which will make
23 them bound to a thread-specific `Realm` instance. Managed objects must be explicitly exported
24 and imported to be passed between threads.
26 Managed instances of objects conforming to this protocol can be converted to a thread-safe
27 reference for transport between threads by passing to the `ThreadSafeReference(to:)` constructor.
29 Note that only types defined by Realm can meaningfully conform to this protocol, and defining new
30 classes which attempt to conform to it will not make them work with `ThreadSafeReference`.
32 public protocol ThreadConfined {
33 // Must also conform to `AssistedObjectiveCBridgeable`
36 The Realm which manages the object, or `nil` if the object is unmanaged.
38 Unmanaged objects are not confined to a thread and cannot be passed to methods expecting a
39 `ThreadConfined` object.
41 var realm: Realm? { get }
43 /// Indicates if the object can no longer be accessed because it is now invalid.
44 var isInvalidated: Bool { get }
48 An object intended to be passed between threads containing a thread-safe reference to its
49 thread-confined object.
51 To resolve a thread-safe reference on a target Realm on a different thread, pass to
54 - warning: A `ThreadSafeReference` object must be resolved at most once.
55 Failing to resolve a `ThreadSafeReference` will result in the source version of the
56 Realm being pinned until the reference is deallocated.
58 - note: Prefer short-lived `ThreadSafeReference`s as the data for the version of the source Realm
59 will be retained until all references have been resolved or deallocated.
61 - see: `ThreadConfined`
62 - see: `Realm.resolve(_:)`
64 public class ThreadSafeReference<Confined: ThreadConfined> {
65 private let swiftMetadata: Any?
68 Indicates if the reference can no longer be resolved because an attempt to resolve it has
69 already occurred. References can only be resolved once.
71 public var isInvalidated: Bool { return objectiveCReference.isInvalidated }
73 private let objectiveCReference: RLMThreadSafeReference<RLMThreadConfined>
76 Create a thread-safe reference to the thread-confined object.
78 - parameter threadConfined: The thread-confined object to create a thread-safe reference to.
80 - note: You may continue to use and access the thread-confined object after passing it to this
83 public init(to threadConfined: Confined) {
84 let bridged = (threadConfined as! AssistedObjectiveCBridgeable).bridged
85 swiftMetadata = bridged.metadata
86 objectiveCReference = RLMThreadSafeReference(threadConfined: bridged.objectiveCValue as! RLMThreadConfined)
89 internal func resolve(in realm: Realm) -> Confined? {
90 guard let objectiveCValue = realm.rlmRealm.__resolve(objectiveCReference) else { return nil }
91 return ((Confined.self as! AssistedObjectiveCBridgeable.Type).bridging(from: objectiveCValue, with: swiftMetadata) as! Confined)
97 Returns the same object as the one referenced when the `ThreadSafeReference` was first
98 created, but resolved for the current Realm for this thread. Returns `nil` if this object was
99 deleted after the reference was created.
101 - parameter reference: The thread-safe reference to the thread-confined object to resolve in
104 - warning: A `ThreadSafeReference` object must be resolved at most once.
105 Failing to resolve a `ThreadSafeReference` will result in the source version of the
106 Realm being pinned until the reference is deallocated.
107 An exception will be thrown if a reference is resolved more than once.
109 - warning: Cannot call within a write transaction.
111 - note: Will refresh this Realm if the source Realm was at a later version than this one.
113 - see: `ThreadSafeReference(to:)`
115 public func resolve<Confined>(_ reference: ThreadSafeReference<Confined>) -> Confined? {
116 return reference.resolve(in: self)