SessionManager

@objcMembers
public class SessionManager : NSObject

By default, all sessions are automatically saved to file and accessible from the users Drafts in the LibraryController and the savedSessions array.

The SessionManager is responsible for automatically restoring saved sessions when the application starts. The savedSessions array will be populated and the DidRestoreSavedSessionsNotification will be called when the session manager has finished restoring the sessions. If you are accessing the savedSessions array and displaying its contents you should refresh your UI when the notification is posted.

You must call PixelSDK.setup() from your App Delegate to guarantee the savedSessions array is populated when the application starts, otherwise we will attempt to populate it at a later point.

Retrieving a Session

If you want to retrieve a specific session after your application restarts, simply save its sessionID beforehand. You can save it in user defaults or your own data structure. For example with user defaults:

let savedSessionID = session.sessionID
UserDefaults.standard.set(savedSessionID, forKey: "mySessionID")

And then at a later point (after a restart, etc.) you can retrieve the session using the ID you saved:

if let savedSessionID = UserDefaults.standard.object(forKey: "mySessionID") as? Int,
    let session = SessionManager.shared.savedSessions.first { $0.sessionID == savedSessionID } {
    // Retrieved session!
}

All sessions are automatically saved, so you only need to worry about saving the sessionID and retrieving the session from the savedSessions array. If you destroy the session or delete the session from your drafts, you will no longer be able to retrieve the session.

Another option for retrieving a session is to simply set some identifying userInfo on the session beforehand.

For example:

session.userInfo = ["my_internal_id": 2]

And then at a later point (after a restart, etc.) you can retrieve the session by finding the session with your user info:

for session in SessionManager.shared.savedSessions {
    if let myInternalID = session.userInfo?["my_internal_id"] as? Int,
        myInternalID == 2 {
        // Retrieved session!
    }
}

Getting the Shared SessionManager

  • Use this to access the shared instance of the SessionManager.

    For example to access the saved sessions:

    print("Saved Sessions: \(SessionManager.shared.savedSessions)")
    

    Declaration

    Swift

    public static let shared: SessionManager

Properties

  • This array contains all sessions that have been saved. The contents of this array will be displayed as the Drafts section of the LibraryController. If a session is deleted from Drafts or destroyed it will be removed from this array.

    For example to access the saved sessions:

    print("Saved Sessions: \(SessionManager.shared.savedSessions)")
    

    Declaration

    Swift

    public internal(set) var savedSessions: [Session] { get }
  • This will be true after the savedSessions array has been populated. This will be set to true shortly after the application starts.

    Default value: false

    Declaration

    Swift

    public private(set) var didRestoreSavedSessions: Bool { get }

Notifications

  • This notification will be posted to the default NotificationCenter from the main thread after the savedSessions array has been populated. This will happen shortly after the application starts.

    Declaration

    Swift

    public static let DidRestoreSavedSessionsNotification: Notification.Name