DatabaseManager
public class DatabaseManager: DatabaseManagerProtocol
Database interaction class. Controls setup of replication, access to documents, manipulation of attachments, etc.
-
Sets up initial database replication.
Declaration
Swift
public static func setupReplication(forDatabase database: String, withUrl url: URL, continuous: Bool, pullOnly: Bool, onComplete: @escaping (_ success: Bool, _ error: Error?) -> Void, firstSyncComplete syncComplete: (() -> Void)?)
Parameters
forDatabase
Name of database to start replicating. Currently must match database name in PredixMobilityConfiguration.defaultDatabaseName or the token
~
withUrl
URL of replication gateway
onComplete
Closure, indicating success or failure of setting up replication, consisting of:
success
true does NOT mean replication has completed successfully, or that the URL provided was valid or reachable, just that there were no internal errors in initializing the replication process.
error
nil, or containing the error that occurred if success is false
firstSyncComplete
Optional closure that, if provided, will be called when the initial replication process has completed successfully.
-
Associates given filter name and filter parameters with push (client to server) replication
Declaration
Swift
public static func assignFilterToPushReplication(filterName: String, parameters: [String: Any]?, onComplete: @escaping (_ success: Bool, _ error: Error?) -> Void)
Parameters
filterName
name of filter. This filter must be defined in PredixMobilityConfiguration.pushReplicationFilterDefinitions
parameters
Dictionary of filter parameters passed to filter function.
onComplete
Closure called when assignment of filter is complete consisting of:
success
will be false if filterName is not defined in PredixMobilityConfiguration.pushReplicationFilterDefinitions, otherwise true.
error
nil, or containing the error that occurred if success is false
-
Adds or removes given channel names to pull (server to client) replication
Declaration
Swift
public static func updatePullReplicationChannels(channelsToAdd: [String]?, channelsToRemove: [String]?, onComplete: @escaping (_ success: Bool, _ error: Error?) -> Void)
Parameters
channelsToAdd
String array of channels to add. Pull replication will be restricted to only these channels
channelsToRemove
String array of channels to remove. Removing all channels will allow replication of all data to which the user has access.
onComplete
Closure called when assignment of channels is complete consisting of:
success
will only be false if both channelsToAdd and channelsToRemove is nil or an empty array.
error
nil, or containing the error that occurred if success is false
-
Removes all channel names for pull (server to client) replication
Declaration
Swift
public static func removeAllPullReplicationChannels(onComplete: @escaping (_ success: Bool, _ error: Error?) -> Void)
Parameters
onComplete
Closure called when assignment of channels is complete consisting of:
success
true if the channels were removed successfully
error
nil, or containing the error that occurred if success is false
-
Runs a query on a defined view
var parameters = QueryByKeyList() parameters.keyList = ["a", "b", "c"] DatabaseManager.runQuery(on: "~", for: "MyView", with: parameters) { queryResultList in // do something with query results }
See Also:
QueryResults
QueryByKeyList
Declaration
Swift
public static func runQuery(on database: String, for viewName: String, with parameters: QueryParameters, completionHandler: @escaping (_ queryResultList: QueryResultList) -> Void)
Parameters
database
Name of database where the view is defined, or
~
for the default database.viewName
Name of view. See
PredixMobilityConfiguration.appendDataViewDefinition
parameters
QueryParameters
-based protocol implementer defining the query to run.completionHandler
Completion handler which will be called when the query has completed.
queryResultList
Results of the query
-
Stops notifications for a query established with a
monitorWithNotification
parameterSee Also:
Declaration
Swift
public static func stopQueryObserver(for notification: Notification.Name)
Parameters
notification
Notification.Name of the notifications sent when the query results are updated
-
Stops notifications for all current queries established with a
monitorWithNotification
parameterSee Also:
Declaration
Swift
public static func stopAllQueryObservers()
-
Returns a document from the database.
Note, that if a document with the provided document id does not exist, the success return value in the onComplete closure will still be true, but the returned document dictionary will be nil.
Declaration
Swift
public static func getDocument(withId documentId: String, fromDatabase database: String = "~", onComplete: @escaping (_ success: Bool, _ error: Error?, _ document: [AnyHashable : Any]?) -> Void)
Parameters
withId
Document Id of document to retrieve
fromDatabase
Optional name of database. If included must match PredixMobilityConfiguration.defaultDatabaseName or be token
~
onComplete
Closure that will be called when the document has been retrieved consisting of:
success
true if no error occurred retrieving document
error
nil, or the error that occurred if success is false
document
the contents of the retrieved document, or nil if no document with the given Id could be found or an error occurred
-
Returns multiple documents from the database.
Declaration
Swift
public static func getDocuments(withIds documentIds: [String], fromDatabase database: String = "~", onComplete: @escaping (_ success: Bool, _ error: Error?, _ documents: [[AnyHashable : Any]]?) -> Void)
Parameters
withIds
Array of document Ids to retrieve
fromDatabase
name of database.
onComplete
Closure that will be called when the document has been retrieved consisting of:
success
true if no error occurred retrieving document
error
nil, or the error that occurred if success is false
documents
Array of the contents of the retrieved documents, or empty if no documents with the given Ids could be found or nil if an error occurred
-
Creates a document in the database
Declaration
Swift
public static func createDocument(from documentDictionary: [String : Any], withId documentId: String?, inDatabase database: String = "~", onComplete: @escaping (_ success: Bool, _ error: Error?, _ document: [AnyHashable : Any]?) -> Void)
Parameters
from
[String : Any] document properties from which to create the document
withId
Optional String Document Id of document to create. If nil, a unique id will be automatically generated.
inDatabase
Optional String name of database. If included must match PredixMobilityConfiguration.defaultDatabaseName or be token
~
onComplete
taskReturnDictionaryBlock closure that will be called when the document has been created consisting of:
success
true if no error occurred creating the document
error
nil, or the error that occurred if success is false
document
contents of the created document, otherwise nil if an error occurred
-
Updates a document in the database
Declaration
Swift
public static func updateDocument(from documentDictionary: [String : Any], withId documentId: String, inDatabase database: String = "~", onComplete: @escaping (_ success: Bool, _ error: Error?, _ document: [AnyHashable : Any]?) -> Void)
Parameters
from
[String : Any] document properties to add/update in the document
withId
String Document Id of document to update.
inDatabase
Optional String name of database. If included must match PredixMobilityConfiguration.defaultDatabaseName or be token
~
onComplete
taskReturnDictionaryBlock closure that will be called when the document has been updated consisting of:
success
true if no error occurred updating the document
error
nil, or containing the error that occurred if success is false
document
contents of the updated document, otherwise nil if an error occurred
-
Deletes a document from the database
Declaration
Swift
public static func deleteDocument(withId documentId: String, fromDatabase database: String = "~", onComplete: @escaping (_ success: Bool, _ error: Error?) -> Void)
Parameters
withId
String Document Id of document to delete.
fromDatabase
Optional String name of database. If included must match PredixMobilityConfiguration.defaultDatabaseName or be token
~
onComplete
taskCompleteBlock closure that will be called when the document has been deleted consisting of:
success
true if the document was deleted successfully
error
nil, or containing the error that occurred if success is false
-
Returns a list of commands. This list is not filtered in any way, this simply returns all commands that exist.
Declaration
Swift
public static func getCommands(completionHandler: @escaping (_ commands: [CommandRecord], _ error: Error?) -> Void)
Parameters
completionHandler
Closure that will be called when the commands has been retrieved consisting of
commands
an array of
CommandDocument
s that of the retrieved commands.error
nil, or an
Error
containing details of the error that occured. -
Returns a command for the given ID. If the command does not exist it will return nil.
Declaration
Swift
public static func getCommand(id commandId: String, completionHandler: @escaping (_ command: CommandRecord?, _ error: Error?) -> Void)
Parameters
id
The command id of the command you want to retrieve
completionHandler
closure that will be called when the command has been retrieved consisting of
command
CommandDocument
that contains all the components of the command if no errors were encounterederror
nil, or an
Error
containing details of the error that occured. -
Deletes a command for the given ID
WARNING: Deleting a command from the client does not guarantee it won’t be processed by the server. If a command was created and syncronized to the server before the command was deleted from the client then the command will be processed by the server.
If you need to cancel a command on the server you should build logic into your commands to allow them to be canceled on the server side.
Declaration
Swift
public static func deleteCommand(id commandId: String, completionHandler: @escaping (_ error: Error?) -> Void)
Parameters
id
The command id of the command you want to delete
completionHandler
closure that will be called when the command has been deleted consisting of
error
nil, or an
Error
containing details of the error that occured. -
Updates a command
Declaration
Swift
public static func updateCommand(id commandId: String, command: CommandContents, completionHandler: @escaping (_ command: CommandRecord?, _ error: Error?) -> Void)
Parameters
id
The command id of the command you want to update
command
CommandDocument
that contains the updated command details if no errors were encounteredcompletionHandler
closure that will be called when the command has been updated consisting of
command
CommandDocument
that contains the updated command details if no errors were encounterederror
nil, or an
Error
containing details of the error that occured. -
Returns a list of commands for related documents.
Declaration
Swift
public static func getCommands(relatedTo documentIds: [String], completionHandler: @escaping (_ commands: [CommandRecord], _ error: Error?) -> Void )
Parameters
documentIDs
An Array of document Ids for which you want to retrieve related commands.
completionHandler
closure that will be called when related commands retrived successfully or unsuccessfully consisting of
commands
an array of
CommandDocument
s if retrieved for givendocumentID
.error
nil, or an
Error
containing details of the error that occurred.