0

Am unfamiliar with UserDefaults and wanted to give it a shot, so I set up storing all of my data in a user defaults class (as shown)

import Foundation
import Combine

class UserSettings: ObservableObject {
    @Published var mon: [UserClass] {
        didSet {
            UserDefaults.standard.set(mon, forKey: "mon")
        }
    }
    @Published var tue: [UserClass] {
        didSet {
            UserDefaults.standard.set(tue, forKey: "tue")
        }
    }
    @Published var wed: [UserClass] {
        didSet {
            UserDefaults.standard.set(wed, forKey: "wed")
        }
    }
    @Published var thu: [UserClass] {
        didSet {
            UserDefaults.standard.set(thu, forKey: "thu")
        }
    }
    @Published var fri: [UserClass] {
        didSet {
            UserDefaults.standard.set(fri, forKey: "fri")
        }
    }
    @Published var homeworks: [String] {
        didSet {
            UserDefaults.standard.set(homeworks, forKey: "homeworks")
        }
    }
    @Published var lastSet: String {
        didSet {
            UserDefaults.standard.set(lastSet, forKey: "lastSet")
        }
    }
    
    init() {
        self.mon = UserDefaults.standard.object(forKey: "mon") as? [UserClass] ?? []
        self.tue = UserDefaults.standard.object(forKey: "tue") as? [UserClass] ?? []
        self.wed = UserDefaults.standard.object(forKey: "wed") as? [UserClass] ?? []
        self.thu = UserDefaults.standard.object(forKey: "thu") as? [UserClass] ?? []
        self.fri = UserDefaults.standard.object(forKey: "fri") as? [UserClass] ?? []
        self.homeworks = UserDefaults.standard.object(forKey: "homeworks") as? [String] ?? []
        self.lastSet = UserDefaults.standard.object(forKey: "lastSet") as? String ?? ""
        print("homeworks \(homeworks)")
    }
}


struct UserClass: Hashable, Codable {
    let start: String
    let end: String
    let name: String
}

func encodeClass(classes: [UserClass], key: String) {
    do {
        // Create JSON Encoder
        let encoder = JSONEncoder()

        // Encode Note
        let data = try encoder.encode(classes)
        print(data)
        // Write/Set Data
        UserDefaults.standard.set(data, forKey: key)

    } catch {
        print("Unable to Encode Array of Notes (\(error))")
    }
}

func decodeClass(key: String) -> [UserClass] {
    var classes: [UserClass] = []
    if let data = UserDefaults.standard.data(forKey: key) {
        do {
            // Create JSON Decoder
            let decoder = JSONDecoder()

            // Decode Note
            classes = try decoder.decode([UserClass].self, from: data)
        } catch {
            print("Unable to Decode Notes (\(error))")
        }
    }
    return classes
}

But when I went to read my data on the iOS app and the WatchOS app, it's like they each have an independent copy of the data that doesn't affect one another. As in, I'll have an array of homework string's persistently saved on the watch, but the phone version will still say the array is empty. How do I get them to sync up as one?

Btw on my ContentViews I read the stored data through just using an ObservedObject like so

@ObservedObject var userSettings = UserSettings()
Dávid Pásztor
  • 51,403
  • 9
  • 85
  • 116

1 Answers1

0

UserDefaults is, as the name suggests, there to store little information about the user. An example would be App settings the user chose. Maybe user information like username or alike. It has limitations regarding space and classes that can be saved there. It is lokal storage on the device. Storing all your Apps data in there is not the idea. For that use CoreData (Which is also local storage).

For the synchronisation between devices. I'd suggest going with something like iCloud which makes it easy for the apple ecosystem to exchange data.

Something which should help you to set it up:

iOS Data Storage Guidelines

Designing for Core Data in iCloud

There are other options. Firebase for example makes it super easy to hook your App up with a database. It takes care of everything. Login, local storage management, synchronisation, storage updates etc.

Simon
  • 1,754
  • 14
  • 32