ParseLiveQuery

public final class ParseLiveQuery : NSObject

The ParseLiveQuery class enables two-way communication to a Parse Live Query Server.

In most cases, you should not call this class directly as a LiveQuery can be indirectly created from Query using:

// If "Message" is a "ParseObject"
let myQuery = Message.query("from" == "parse")
guard let subscription = myQuery.subscribe else {
    "Error subscribing..."
    return
}
subscription.handleSubscribe { subscribedQuery, isNew in

    //Handle the subscription however you like.
    if isNew {
        print("Successfully subscribed to new query \(subscribedQuery)")
    } else {
        print("Successfully updated subscription to new query \(subscribedQuery)")
    }
}

The above creates a ParseLiveQuery using either the liveQueryServerURL (if it has been set) or serverURL when using ParseSwift.initialize. All additional queries will be created in the same way. The times you will want to initialize a new ParseLiveQuery instance are:

  1. If you want to become a ParseLiveQueryDelegate to respond to authentification challenges and/or receive metrics and error messages for a ParseLiveQueryclient.
  2. You have specific LiveQueries that need to subscribe to a server that have a different url than the default.
  3. You want to change the default url for all LiveQuery connections when the app is already running. Initializing new instances will create a new task/connection to the ParseLiveQuery server. When an instance is deinitialized it will automatically close it’s connection gracefully.
  • Have all ParseLiveQuery authentication challenges delegated to you. There can only be one of these for all ParseLiveQuery connections. The default is to delegate to the authentication call block passed to ParseSwift.initialize or if there is not one, delegate to the OS. Conforms to ParseLiveQueryDelegate.

    Declaration

    Swift

    public weak var authenticationDelegate: ParseLiveQueryDelegate? { get set }
  • Have ParseLiveQuery connection metrics, errors, etc delegated to you. A delegate can be assigned to individual connections. Conforms to ParseLiveQueryDelegate.

    Declaration

    Swift

    public weak var receiveDelegate: ParseLiveQueryDelegate?
  • True if the connection to the url is up and available. False otherwise.

    Declaration

    Swift

    public internal(set) var isSocketEstablished: Bool { get set }
  • True if this client is connected. False otherwise.

    Declaration

    Swift

    public internal(set) var isConnected: Bool { get set }
  • True if this client is connecting. False otherwise.

    Declaration

    Swift

    public internal(set) var isConnecting: Bool { get set }
  • Declaration

    Swift

    public init(serverURL: URL? = nil, isDefault: Bool = false, notificationQueue: DispatchQueue = .main) throws

    Parameters

    serverURL

    The URL of the ParseLiveQuery Server to connect to. Defaults to nil in which case, it will use the URL passed in ParseSwift.initialize(...liveQueryServerURL: URL). If no URL was passed, this assumes the current Parse Server URL is also the LiveQuery server.

    isDefault

    Set this ParseLiveQuery client as the default client for all LiveQuery connections. Defaults value of false.

    notificationQueue

    The queue to return to for all delegate notifications. Default value of .main.

Connection - Async/Await

  • Manually establish a connection to the ParseLiveQuery Server.

    Throws

    An error of type ParseError.

    Declaration

    Swift

    public func open(isUserWantsToConnect: Bool = true) async throws

    Parameters

    isUserWantsToConnect

    Specifies if the user is calling this function. Defaults to true.

    Return Value

    An instance of the logged in ParseUser.

  • sendPing() Asynchronous

    Sends a ping frame from the client side. Returns when a pong is received from the server endpoint.

    Throws

    An error of type ParseError.

    Declaration

    Swift

    public func sendPing() async throws

Connection - Combine

  • Manually establish a connection to the ParseLiveQuery Server. Publishes when established.

    Declaration

    Swift

    public func openPublisher(isUserWantsToConnect: Bool = true) -> Future<Void, Error>

    Parameters

    isUserWantsToConnect

    Specifies if the user is calling this function. Defaults to true.

    Return Value

    A publisher that eventually produces a single value and then finishes or fails.

  • Sends a ping frame from the client side. Publishes when a pong is received from the server endpoint.

    Declaration

    Swift

    public func sendPingPublisher() -> Future<Void, Error>

    Return Value

    A publisher that eventually produces a single value and then finishes or fails.

Client Intents

  • Current LiveQuery client.

    Declaration

    Swift

    public private(set) static var client: ParseLiveQuery? { get }
  • The default ParseLiveQuery client for all LiveQuery connections.

    Declaration

    Swift

    public class var defaultClient: ParseLiveQuery? { get }
  • Set a specific ParseLiveQuery client to be the default for all ParseLiveQuery connections.

    Declaration

    Swift

    public class func setDefault(_ client: ParseLiveQuery)

    Parameters

    client

    The client to set as the default.

  • Get the default ParseLiveQuery client for all LiveQuery connections.

    Warning

    This will be deprecated in ParseSwift 5.0.0 in favor of defaultClient.

    Declaration

    Swift

    public class func getDefault() -> ParseLiveQuery?

    Return Value

    The default ParseLiveQuery client.

  • Check if a query has an active subscription on this ParseLiveQuery client.

    Throws

    An error of type ParseError.

    Declaration

    Swift

    public func isSubscribed<T>(_ query: Query<T>) throws -> Bool where T : ParseObject

    Parameters

    query

    Query to verify.

    Return Value

    true if subscribed. false otherwise.

  • Check if a query has a pending subscription on this ParseLiveQuery client.

    Throws

    An error of type ParseError.

    Declaration

    Swift

    public func isPendingSubscription<T>(_ query: Query<T>) throws -> Bool where T : ParseObject

    Parameters

    query

    Query to verify.

    Return Value

    true if query is a pending subscription. false otherwise.

  • Remove a pending subscription on this ParseLiveQuery client.

    Throws

    An error of type ParseError.

    Declaration

    Swift

    public func removePendingSubscription<T>(_ query: Query<T>) throws where T : ParseObject

    Parameters

    query

    Query to remove.

Connection

  • Manually establish a connection to the ParseLiveQuery Server.

    Declaration

    Swift

    public func open(isUserWantsToConnect: Bool = true, completion: @escaping (Error?) -> Void)

    Parameters

    isUserWantsToConnect

    Specifies if the user is calling this function. Defaults to true.

    completion

    Returns nil if successful, an Error otherwise.

  • Manually disconnect from the ParseLiveQuery Server.

    Declaration

    Swift

    public func close()
  • Manually disconnect all sessions and subscriptions from the ParseLiveQuery Server.

    Declaration

    Swift

    public func closeAll()
  • Sends a ping frame from the client side, with a closure to receive the pong from the server endpoint.

    Declaration

    Swift

    public func sendPing(pongReceiveHandler: @escaping (Error?) -> Void)

    Parameters

    pongReceiveHandler

    A closure called by the task when it receives the pong from the server. The closure receives an Error that indicates a lost connection or other problem, or nil if no error occurred.