How to save FamilyActivitySelection in FirebaseFirestore

Hi, I'm developing an app like Opal which uses Screen Time API. So the user should be able to create modes to activate it later, like an alarm. There's a list of durations and mode names and there should be selected apps, so whenever the user turns the switch on, app restricting should be started. How can I save the selected apps in FirebaseFirestore, and then get that data to restrict the apps whenever the user turns the switch on? I've tried to save it as Strings, but I can't convert it later to ApplicationToken to insert it to Set<ApplicationToken>.

Still wondering if someone has figured out a solution to this.

Hi, you can try saving it to local UserDefaults or you can try to utilize the following solution to store in the FireStore.

let encoder = PropertyListEncoder()
let decoder = PropertyListDecoder()

//When saving selection
UserDefaults.standard.set(try? self.encoder.encode(newValue), forKey: DefaultKeys.familyControlSelections)

//When retrieving Selection
guard let data = UserDefaults.standard.data(forKey: DefaultKeys.familyControlSelections) else {
                    
                    print("====> no data saved in the family controls user defaults")
                    return
 }
if let selections = try? decoder.decode(FamilyActivitySelection.self, from: data){
                        self.store.shield.applications = selections.applicationTokens.isEmpty ? nil : selections.applicationTokens
                        self.store.shield.applicationCategories = ShieldSettings.ActivityCategoryPolicy.specific(selections.categoryTokens, except: Set())
                        self.store.shield.webDomains = selections.webDomainTokens
 }
How to save FamilyActivitySelection in FirebaseFirestore
 
 
Q