Managing Sessions

  • 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!
        }
    }
    
    See more

    Declaration

    Swift

    @objcMembers
    public class SessionManager : NSObject