PromiseWrapper
public final class PromiseWrapper : WebcomWrapper, Hashable, CustomStringConvertible
The type that wraps DatasyncNode
in order to enrich it with a PromiseKit-based API.
See also the documentation section of WebcomWrapper
for the corresponding wrapped type.
To use this type, import the following dependency in your code:
import WebcomPromises
-
Declaration
Swift
public let wrapped: DatasyncNode
-
A reference to the parent of the node referred by this instance.
The root node does not have a parent, so this property returns a promise which is rejected for the root node. For all other nodes, this property always returns a promise which is fulfilled.
Note: With a reference to the root node,
relativeNode(for: "..")
returns a promise representing the root node.Declaration
Swift
public var parent: Promise<DatasyncNode.PromiseWrapper> { get }
-
Returns a reference to another node, relatively to the node referred by this instance.
This method only fails when the
path
is ill-formed, that is when it contains forbidden characters. All nodes always exists: this method always returns a fulfilled promise when thepath
is syntactically valid.The path consists of segments separated by
/
(slash) characters. Each segment corresponds to a move in the database tree:- The starting point is the node corresponding to this instance.
- The
.
segment is a no-operation. - The
..
segment indicates to go one level up in the tree. When already at the root node, this is a no-operation. - The empty segment, when using a
/
at the beginning or at the end of thepath
, or when using several consecutive/
, is a no-operation. - Any other valid segment indicates to go one level down towards the so-named child.
If the path referred by this node is
/foo/bar
:- and
path
is equal togeek
,/geek
,geek/
or/geek/
, the path of the resulting node is/foo/bar/geek
, - and
path
is equal to../geek/noob
or/../geek//noob/.
or../geek/./noob/.
, the path of the resulting node is/foo/geek/noob
, - and
path
is equal to../geek/../..//../..
, the resulting node is the root node.
Note: With a reference to the root node,
parent
returns a rejected promise.Declaration
Swift
public func relativeNode(for path: String) -> Promise<DatasyncNode.PromiseWrapper>
Parameters
path
The relative path of the node to refer. It can start or not with a
/
(slash) character. It is always relative.Return Value
A promise representing the node corresponding to the relative
path
. -
Returns a reference to a database node, relatively to a base node.
This operator returns:
node.relativeNode(for: path)
Declaration
Swift
public static func / (node: PromiseWrapper, path: String) -> Promise<DatasyncNode.PromiseWrapper>
Parameters
node
A reference to the base node.
path
The relative path of the node to refer. It can start or not with a
/
(slash) character. It is always relative.Return Value
A promise representing the node corresponding to the relative
path
. -
Creates a reference to a new child of the node referred by this instance.
The key of the new child is a unique identifier based on a timestamp. Such a key is also called a push identifier. The keys of several children created with this method are ordered chronologically. With this method, several users can add children to the same data node of a database at the same time without any write conflict.
Note: More exactly, since from a theoretical point of view all nodes always exist, this method actually returns a reference to a node whose current value is JSON
null
.Declaration
Swift
public func createChild() -> Promise<DatasyncNode.PromiseWrapper>
Return Value
A promise representing a new child of this node.
-
Gets the value of the node referred by this instance.
The
decodedValue()
method can be used to get decoded values.Declaration
Swift
public func value() -> Promise<DatasyncValue>
Return Value
A promise representing the node value.
-
Gets the value of the node referred by this instance.
This method behaves like the
value()
method but it returns a promise that gets a decoded value in case of success. When the value cannot be decoded, the promise is rejected with aWebcomError
having theWebcomError.SDKCode.decodingFailed
code. If this behavior does not fit your needs, use thevalue()
method and process the parameter as expected.The decoding is done using the
WebcomApplication.jsonDecoder
of theWebcomApplication
from which this node derives.Declaration
Swift
public func decodedValue<T>() -> Promise<T> where T : Decodable
Return Value
A promise with the decoded value of the referred node
-
Gets the value of the node referred by this instance.
This method behaves like the
value()
method but it returns a promise that gets a decoded value in case of success. When the value cannot be decoded, the promise is rejected with aWebcomError
having theWebcomError.SDKCode.decodingFailed
code. If this behavior does not fit your needs, use thevalue()
method and process the parameter as expected.The decoding is done using the
WebcomApplication.jsonDecoder
of theWebcomApplication
from which this node derives.Declaration
Swift
public func decodedValue<T>(as type: T.Type) -> Promise<T> where T : Decodable
Parameters
type
The target type for the decoding.
Return Value
A promise with the decoded value of the referred node
-
Clears the value of the node referred by this instance.
Clearing the value is equivalent to setting a JSON
null
value. Therefore, calling this method is equivalent to call:set(nil, at: time, queue: queue, then: completionCallback)
Declaration
Swift
@discardableResult public func clear(at time: DatasyncTime = .now) -> Promise<Void>
Parameters
time
The time at which to perform the operation.
Return Value
A promise with a void value.
-
Sets the value of the node referred by this instance.
Note: In most cases, you should rather use the
set(_:at:)
overload that takes anEncodable
parameter.All children of the referred node cleared before setting the new value. The
merge(_:with:at:)
method may be used to merge existing children with the ones of thevalue
when it is a JSON object or array.Declaration
Swift
@discardableResult public func set(_ raw: WebcomMarkers.Raw, _ json: Any?, at time: DatasyncTime = .now) -> Promise<Void>
Parameters
raw
Use the
.raw
value to disambiguate with theset(_:at:)
overload that takes anEncodable
parameter.json
The value to set. It must be valid JSON (object, array, string, number, boolean or
null
). Thenull
value can be specified withnil
orNSNull()
.time
The time at which to perform the operation.
Return Value
A promise with a void value.
-
Sets the value of the node referred by this instance.
All children of the referred node cleared before setting the new value. The
merge(with:at:)
method may be used to merge existing children with the ones of thevalue
when it is a JSON object or array.Declaration
Swift
@discardableResult public func set<T>(_ value: T?, at time: DatasyncTime = .now) -> Promise<Void> where T : Encodable
Parameters
value
The value to set. It will be encoded to JSON before being sent to the back-end, using the
WebcomApplication.jsonEncoder
of theWebcomApplication
from which this node derives. Thenull
value can be specified withnil
orNSNull()
.time
The time at which to perform the operation.
Return Value
A promise with a void value.
-
Sets the value of a node.
This operator is equivalent to:
node.set(value)
Declaration
Swift
@discardableResult public static func <- <T>(node: PromiseWrapper, value: T?) -> Promise<Void> where T : Encodable
Parameters
node
A reference to the node whose value must be set.
value
The value to set.
Return Value
A promise with a void value.
-
Merges a value with the current value of the node referred by this instance.
Note: In most cases, you should rather use the
merge(with:at:)
overload that takes anEncodable
parameter.Unlike what happens with the
set(_:_:at:)
:json
must be a valid JSON object,existing direct children (i.e. the descendants at the first level of depth) that exist in the current value are preserved when
json
does not specify them.
Declaration
Swift
@discardableResult public func merge(_ raw: WebcomMarkers.Raw, with json: [String : Any], at time: DatasyncTime = .now) -> Promise<Void>
Parameters
raw
Use the
.raw
value to disambiguate with themerge(with:at:)
overload that takes anEncodable
parameter.json
The value to merge with. It must be a valid JSON object.
time
The time at which to perform the operation.
Return Value
A promise with a void value.
-
Merges a value with the current value of the node referred by this instance.
Unlike what happens with the
set(_:at:)
:value
may not benil
,- the encoding of
value
to JSON must give a JSON object (or a JSON array but this is discouraged), or the method will fail, existing direct children (i.e. the descendants at the first level of depth) that exist in the current value are preserved when
value
does not specify them.
Declaration
Swift
@discardableResult public func merge<T>(with value: T, at time: DatasyncTime = .now) -> Promise<Void> where T : Encodable
Parameters
value
The value to merge with. It will be encoded to JSON before being sent to the back-end, using the
WebcomApplication.jsonEncoder
of theWebcomApplication
from which this node derives.time
The time at which to perform the operation.
Return Value
A promise with a void value.
-
Adds a new child with the given value to the node referred by this instance.
Note: In most cases, you should rather use the
push(_:at:)
overload that takes anEncodable
parameter.This method creates a new child to the referred node and then it sets its value.
Declaration
Swift
@discardableResult public func push(_ raw: WebcomMarkers.Raw, _ json: Any?) -> Promise<DatasyncNode.PromiseWrapper>
Parameters
raw
Use the
.raw
value to disambiguate with thepush(_:at:)
overload that takes anEncodable
parameter.json
The value to set for the created child. It must be valid JSON (object, array, string, number, boolean or
null
). Thenull
value can be specified withnil
orNSNull()
.Return Value
A promise representing the new child.
-
Adds a new child with the given value to the node referred by this instance.
This method creates a new child to the referred node and then it sets its value.
Declaration
Swift
@discardableResult public func push<T>(_ value: T?) -> Promise<DatasyncNode.PromiseWrapper> where T : Encodable
Parameters
value
The value to set for the created child. It will be encoded to JSON before being sent to the back-end, using the
WebcomApplication.jsonEncoder
of theWebcomApplication
from which this node derives. Thenull
value can be specified withnil
orNSNull()
.Return Value
A promise representing the new child.
-
Adds a new child to a node with the given value.
This operator is equivalent to:
node.push(value)
Declaration
Swift
@discardableResult public static func += <T>(node: PromiseWrapper, value: T?) -> Promise<DatasyncNode.PromiseWrapper> where T : Encodable
Parameters
node
A reference to the node to which a new child must be pushed.
value
The value to set for the created child.
Return Value
A promise representing the new child.
-
Atomically increments (or decrements) the value of the node referred by this instance.
When the node does not exist (in other words, when its initial value is
null
), the node is implicitly assumed having thestartValue
value and the incrementstep
is added to that implicit value.When the node exists and its initial value is not a number, the operation fails.
This operation is robust to concurrent increment operations. It is guaranteed that if n increment operations are performed at the same time, then the value of the node is incremented by n
steps
(in other words, none of the concurrent operations can overwrite another one).However, note that the returned value may not be synchronized with the actual value of the node, especially if other write operations are performed at the same time. You should rather use the
subscribe(to:childrenConstraint:stores:file:line:queue:onEvent:onCompletion:)
method to watch the actual value of the node.Declaration
Swift
public func increment(from startValue: NSNumber = 0, by step: NSNumber = 1) -> Promise<NSNumber>
Parameters
startValue
The implicit initial value of the node when it does not exist.
step
The increment to add to the initial value to get the final value. To decrement, use a negative value.
Return Value
A promise representing the resulting value of the increment operation.
-
Runs a transaction on the node referred by this instance.
A transaction is a process that atomically sets a new value to the node depending on its initial value. In other words, the new value is computed from the initial value and it is sets only if the value of the node has not been modified in the meantime. If the value of the node just before setting the new value is no longer the initial value, then the transaction is run again.
The transaction is specified with a function, which receives as its single parameter the initial value and returns an operation to execute. The operation may consist of setting a new value or clearing the value (which is equivalent to setting the
null
value). The operation may also indicate to abort the transaction.If the transaction consists in incrementing (or decrementing) the value of the referred node, you should rather use the
increment(from:by:)
method which is more efficient in this case.Declaration
Swift
@discardableResult public func runTransaction(_ function: @escaping DatasyncTransaction.Function) -> Promise<DatasyncTransaction.Result>
Parameters
function
The function that computes the operation to execute from the initial value of the node.
Return Value
A promise that represents the result of the transaction.
-
Subscribes to events related to the value of the referred node.
The
decodedValueSubscribe(to:childrenConstraint:stores:file:line:queue:onValue:onCompletion:)
method can be used to directly get decoded values.A subscription must always be stored somewhere in order to remain active. The easiest way is to let the
DatasyncManager
of the referred node store it by setting thestores
parameter totrue
(which is its default value). Otherwise, the returned subscription instance must be stored by the application. Until a subscription is cancelled or revoked, it remains active and consumes resources.A subscription is cancelled:
- explicitly using the
DatasyncSubscription.cancel()
method, - implicitly when it is stored in a
DatasyncManager
which is deinitialized, including when it has other references, - implicitly when it is deinitialized.
All subscriptions stored by a given
DatasyncManager
instance may also be cancelled explicitly at once using theDatasyncManager.unsubscribeFromAll()
method.A subscription may also be revoked by the back-end if the authenticated user loses read permission at the referred node.
To catch such a cancellation or revocation, use the
completionCallback
parameter.Important: Be careful with variables captured by the callback
If this instance belongs to a manager that is stored in a view controller (which is a strongly encouraged practice), and the
eventCallback
needs to use a property or a method of that view controller (which is quite common), use aweak
or anunowned
reference for that view controller, to break the following retain cycle:view controller ---> manager ---> event callback ^ | | | o---- use weak/unowned here ----o
Declaration
Swift
@discardableResult public func subscribe(to eventType: DatasyncEventType = .valueChange, childrenConstraint: DatasyncChildrenConstraint = .all, stores: Bool = true, file: StaticString = #fileID, line: UInt = #line, queue: DispatchQueue? = nil, onEvent eventCallback: @escaping (_ event: DatasyncEvent) -> Void, onCompletion completionCallback: ((_ completion: DatasyncSubscriptionCompletion) -> Void)? = nil) -> DatasyncSubscription?
Parameters
eventType
The type of events to subscribe.
childrenConstraint
A constraint to filter the children of the referred node that are affected by the subscription
stores
Indicates whether the returned subscription should be stored by the manager.
file
The file from which the method is called. This is used for debugging purposes.
line
The line from which the method is called. This is used for debugging purposes.
queue
The queue to which the provided
eventCallback
andcompletionCallback
dispatch. Whennil
, the default callback queue of the manager of this instance is used.eventCallback
The closure to call when the specified event occurs.
event
The event that occurred.
completionCallback
The closure to call when the subscription completes.
completion
A value representing how the subscription completed.
Return Value
An instance describing the subscription, or
nil
if the subscription failed. - explicitly using the
-
Subscribes to events related to the value of the referred node.
This method behaves like the
subscribe(to:childrenConstraint:stores:file:line:queue:onEvent:onCompletion:)
method with a callback that receives the decoded value brought by the event. When the value cannot be decoded, thedecodedValueCallback
is not called. Such a non-decodable value is simply ignored. If this behavior does not fit your needs, use thesubscribe(to:childrenConstraint:stores:file:line:queue:onEvent:onCompletion:)
method and process theevent
as expected.Declaration
Swift
@discardableResult public func decodedValueSubscribe<T>(to eventType: DatasyncEventType = .valueChange, childrenConstraint: DatasyncChildrenConstraint = .all, stores: Bool = true, file: StaticString = #fileID, line: UInt = #line, queue: DispatchQueue? = nil, onValue decodeValueCallback: @escaping (_ decodedValue: T) -> Void, onCompletion completionCallback: ((_ completion: DatasyncSubscriptionCompletion) -> Void)? = nil) -> DatasyncSubscription? where T : Decodable
Parameters
eventType
The type of events to subscribe.
childrenConstraint
A constraint to filter the children of the referred node that are affected by the subscription.
stores
Indicates whether the returned subscription should be stored by the manager.
file
The file from which the method is called. This is used for debugging purposes.
line
The line from which the method is called. This is used for debugging purposes.
queue
The queue to which the provided
eventCallback
andcompletionCallback
dispatch. Whennil
, the default callback queue of the manager of this instance is used.decodeValueCallback
The closure to call when the specified event occurs and the value can be decoded.
decodedValue
The value brought by the event that occurred.
completionCallback
The closure to call when the subscription completes.
completion
A value representing how the subscription completed.
Return Value
An instance describing the subscription, or
nil
if the subscription failed. -
Copies the value of the node referred by this instance to another node.
All children of the destination node are cleared before setting its value.
Declaration
Swift
public func copyValue(to destinationNode: DatasyncNode.PromiseWrapper) -> Promise<DatasyncValue>
Parameters
destinationNode
A reference to the node to set with the value of this node.
Return Value
A promise representing the copied value.
-
Copies the value of the node referred by this instance to another node.
All children of the destination node are cleared before setting its value.
Declaration
Swift
public func copyValue(to destinationPath: String) -> Promise<DatasyncValue>
Parameters
destinationPath
The path of the node to set with the value of this node.
Return Value
A promise representing the copied value.