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
forDatabaseName of database to start replicating. Currently must match database name in PredixMobilityConfiguration.defaultDatabaseName or the token
~
withUrlURL of replication gateway
onCompleteClosure, indicating success or failure of setting up replication, consisting of:
successtrue 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.
errornil, or containing the error that occurred if success is false
firstSyncCompleteOptional 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
filterNamename of filter. This filter must be defined in PredixMobilityConfiguration.pushReplicationFilterDefinitions
parametersDictionary of filter parameters passed to filter function.
onCompleteClosure called when assignment of filter is complete consisting of:
successwill be false if filterName is not defined in PredixMobilityConfiguration.pushReplicationFilterDefinitions, otherwise true.
errornil, 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
channelsToAddString array of channels to add. Pull replication will be restricted to only these channels
channelsToRemoveString array of channels to remove. Removing all channels will allow replication of all data to which the user has access.
onCompleteClosure called when assignment of channels is complete consisting of:
successwill only be false if both channelsToAdd and channelsToRemove is nil or an empty array.
errornil, 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
onCompleteClosure called when assignment of channels is complete consisting of:
successtrue if the channels were removed successfully
errornil, 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:
QueryResultsQueryByKeyList
Declaration
Swift
public static func runQuery(on database: String, for viewName: String, with parameters: QueryParameters, completionHandler: @escaping (_ queryResultList: QueryResultList) -> Void)Parameters
databaseName of database where the view is defined, or
~
for the default database.viewNameName of view. See
PredixMobilityConfiguration.appendDataViewDefinitionparametersQueryParameters-based protocol implementer defining the query to run.completionHandlerCompletion handler which will be called when the query has completed.
queryResultListResults of the query
-
Stops notifications for a query established with a
monitorWithNotificationparameterSee Also:
Declaration
Swift
public static func stopQueryObserver(for notification: Notification.Name)Parameters
notificationNotification.Name of the notifications sent when the query results are updated
-
Stops notifications for all current queries established with a
monitorWithNotificationparameterSee 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
withIdDocument Id of document to retrieve
fromDatabaseOptional name of database. If included must match PredixMobilityConfiguration.defaultDatabaseName or be token
~
onCompleteClosure that will be called when the document has been retrieved consisting of:
successtrue if no error occurred retrieving document
errornil, or the error that occurred if success is false
documentthe 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
withIdsArray of document Ids to retrieve
fromDatabasename of database.
onCompleteClosure that will be called when the document has been retrieved consisting of:
successtrue if no error occurred retrieving document
errornil, or the error that occurred if success is false
documentsArray 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
withIdOptional String Document Id of document to create. If nil, a unique id will be automatically generated.
inDatabaseOptional String name of database. If included must match PredixMobilityConfiguration.defaultDatabaseName or be token
~
onCompletetaskReturnDictionaryBlock closure that will be called when the document has been created consisting of:
successtrue if no error occurred creating the document
errornil, or the error that occurred if success is false
documentcontents 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
withIdString Document Id of document to update.
inDatabaseOptional String name of database. If included must match PredixMobilityConfiguration.defaultDatabaseName or be token
~
onCompletetaskReturnDictionaryBlock closure that will be called when the document has been updated consisting of:
successtrue if no error occurred updating the document
errornil, or containing the error that occurred if success is false
documentcontents 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
withIdString Document Id of document to delete.
fromDatabaseOptional String name of database. If included must match PredixMobilityConfiguration.defaultDatabaseName or be token
~
onCompletetaskCompleteBlock closure that will be called when the document has been deleted consisting of:
successtrue if the document was deleted successfully
errornil, 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
completionHandlerClosure that will be called when the commands has been retrieved consisting of
commandsan array of
CommandDocuments that of the retrieved commands.errornil, or an
Errorcontaining 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
idThe command id of the command you want to retrieve
completionHandlerclosure that will be called when the command has been retrieved consisting of
commandCommandDocumentthat contains all the components of the command if no errors were encounterederrornil, or an
Errorcontaining 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
idThe command id of the command you want to delete
completionHandlerclosure that will be called when the command has been deleted consisting of
errornil, or an
Errorcontaining 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
idThe command id of the command you want to update
commandCommandDocumentthat contains the updated command details if no errors were encounteredcompletionHandlerclosure that will be called when the command has been updated consisting of
commandCommandDocumentthat contains the updated command details if no errors were encounterederrornil, or an
Errorcontaining 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
documentIDsAn Array of document Ids for which you want to retrieve related commands.
completionHandlerclosure that will be called when related commands retrived successfully or unsuccessfully consisting of
commandsan array of
CommandDocuments if retrieved for givendocumentID.errornil, or an
Errorcontaining details of the error that occurred.
View on GitHub
DatabaseManager Class Reference