Managing Sessions
-
By default, all sessions are automatically saved to file and accessible from the users Drafts in the
LibraryControllerand thesavedSessionsarray.The
SessionManageris responsible for automatically restoring saved sessions when the application starts. ThesavedSessionsarray will be populated and theDidRestoreSavedSessionsNotificationwill be called when the session manager has finished restoring the sessions. If you are accessing thesavedSessionsarray 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 thesavedSessionsarray 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
sessionIDbeforehand. 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
sessionIDand retrieving the session from thesavedSessionsarray. 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
userInfoon 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:
See morefor session in SessionManager.shared.savedSessions { if let myInternalID = session.userInfo?["my_internal_id"] as? Int, myInternalID == 2 { // Retrieved session! } }Declaration
Swift
@objcMembers public class SessionManager : NSObject
View on GitHub
Managing Sessions Reference