AutoOpen
@available(iOS 14.0, macOS 11.0, tvOS 14.0, watchOS 7.0, *)
@propertyWrapper
public struct AutoOpen<Partition> : DynamicProperty where Partition : BSON
AutoOpen
will try once to asynchronously open a Realm, but in case of no internet connection will return an opened realm
for the given appId and partitionValue which can be used within our view.
Add AutoOpen to your SwiftUI/View
or SwiftUI/App
, after a user is already logged in
or if a user is going to be logged in
@AutoOpen(appId: "app_id", partitionValue: <partition_value>, timeout: 4000) var autoOpen
This will immediately initiates a Realm.asyncOpen()
operation which will perform all work needed to get the Realm to
a usable state. (see Realm.asyncOpen() documentation)
This property wrapper will publish states of the current Realm.asyncOpen()
process like progress, errors and an opened realm,
which can be used to update the view
struct AutoOpenView: View {
@AutoOpen(appId: "app_id", partitionValue: <partition_value>) var autoOpen
var body: some View {
switch autoOpen {
case .notOpen:
ProgressView()
case .open(let realm):
ListView()
.environment(\.realm, realm)
case .error(_):
ErrorView()
case .progress(let progress):
ProgressView(progress)
}
}
}
This opened realm
can be later injected to the view as an environment value which will be used by our property wrappers
to populate the view with data from the opened realm
ListView()
.environment(\.realm, realm)
This property wrapper behaves similar as AsyncOpen
, and in terms of declaration and use is completely identical,
but with the difference of a offline-first approach.
-
A Publisher for
AsyncOpenState
, emits a state each time the asyncOpen state changes.Declaration
Swift
public var projectedValue: Published<AsyncOpenState>.Publisher { get }
-
This will cancel any notification from the property wrapper states
Declaration
Swift
public func cancel()
-
Initialize the property wrapper
Declaration
Parameters
appId
The unique identifier of your Realm app, if empty or
nil
will try to retrieve latest singular cached app.partitionValue
The
BSON
value the Realm is partitioned on.configuration
The
Realm.Configuration
used when creating the Realm, user’s sync configuration for the given partition value will be set as thesyncConfiguration
, if empty the configuration is set to thedefaultConfiguration
.timeout
The maximum number of milliseconds to allow for a connection to become fully established, if empty or
nil
no connection timeout is set. -
Declaration
Swift
public mutating func update()