BMLTSession

class BMLTSession : NSObject, URLSessionDataDelegate, BMLTCommunicatorDataSourceProtocol

This manages an interactive session connection with the server.

For whatever reason, Cocoa/Swift won’t let me derive from NSURLSession, so this class aggregates it, as opposed to extends it (not a big deal, probably better design in the long run anyway).

The semantic administration requires a constant session. Authentication is done once during the session, then the server maintains the authentication for the duration of the session. For this reason, we need to maintain a consistent session throughout our administration duties. This class is instantiated once by the App Delegate, and holds the session open.

This class maintains a dictionary of completion blocks that it uses to forward server responses to the users of this class. User instances need to define a completion block/callback, in the RequestCompletionBlock format. When they call a URL with this class, they provide this completion block. When the class instance gets the response, it forwards the data as an NSData instance to the completion block.

  • This class allows us to tag tasks with a completion block, allowing better autonomy.

    See more

    Declaration

    Swift

    class BMLTSessionTaskData
  • This is our session.

    Declaration

    Swift

    var mySession: URLSession!
  • This will be set to true if the URL call is expected to fail (suppresses error report).

    Declaration

    Swift

    var suppressErrors: Bool
  • This is true if this is an SSL session

    Declaration

    Swift

    var isSSL: Bool
  • We are strictly linear. No concurrent I/O. We have just one job, and we do it through completion.

    Declaration

    Swift

    var myCurrentTask: BMLTSession.BMLTSessionTaskData!
  • Make sure we clean up after ourselves.

    Declaration

    Swift

    deinit
  • Pretty much exactly what it says on the tin.

    Declaration

    Swift

    func disconnectSession()
  • Declares the connection status.

    Declaration

    Swift

    func isSessionConnected() -> Bool

    Return Value

    a Bool, true if the session is currently connected.

  • Called when a session throws a nutty.

    Declaration

    Swift

    func urlSession(_ inSession: URLSession, didBecomeInvalidWithError inError: Error?)

    Parameters

    inSession

    The NSURLSession that controls this task.

    error

    The error returned.

  • Called as the task receives data.

    Declaration

    Swift

    func urlSession(_ inSession: URLSession, dataTask: URLSessionDataTask, didReceive: Data)

    Parameters

    inSession

    The NSURLSession that controls this task.

    dataTask

    The task that triggered this callback.

    didReceive

    the received data piece.

  • Called when a task has completed. We use this call to extract a JSON object from the response, then pass that object to the next handler.

    Declaration

    Swift

    func urlSession(_ inSession: URLSession, task: URLSessionTask, didCompleteWithError error: Error?)

    Parameters

    inSession

    The NSURLSession that controls this task.

    task

    The task that triggered this callback.

    error

    Any error that occurred.

  • Calls a URL

    Declaration

    Swift

    func callURI(_ inCommunicator: BMLTCommunicator, inURIAsAString: String!, inCompletionBlock: BMLTCommunicator.RequestCompletionBlock!) -> Bool

    Parameters

    inCommunicator

    The communicator instance calling this.

    inURIAsAString

    This contains a string, with the URI.

    inCompletionBlock

    This is the completion block supplied by the caller. It is to be called upon receipt of data.

    inIsTest

    If true, then we don’t report task errors (the call is expected to fail).

    Return Value

    a Bool. True, if the call was successfully initiated.

  • This is a call that is made to validate an SSL security challenge (session variant).

    Declaration

    Swift

    func urlSession(_ session: URLSession, didReceive challenge: URLAuthenticationChallenge, completionHandler: @escaping (URLSession.AuthChallengeDisposition, URLCredential?) -> Void)

    Parameters

    session

    The session thatt’s running the connection being challenged.

    challenge

    The challenge type

    completionHandler

    The handler we need to call with our response.

  • This is a call that is made to validate an SSL security challenge (task variant).

    Declaration

    Swift

    func urlSession(_ session: URLSession, task: URLSessionTask, didReceive challenge: URLAuthenticationChallenge, completionHandler: @escaping (URLSession.AuthChallengeDisposition, URLCredential?) -> Void)

    Parameters

    session

    The session thatt’s running the connection being challenged.

    task

    The actual URL task that resulted in this challenge.

    challenge

    The challenge type

    completionHandler

    The handler we need to call with our response.