So to kick things off I decided to move the item data loading to a separate function in ItemStore
like so
@objc func loadData() throws {
let data = try Data(contentsOf: itemArchiveURL)
let unarchiver = PropertyListDecoder()
let items = try unarchiver.decode([Item].self, from: data)
allItems = items
}
and then I made the ItemsViewController
“listen” for a “willEnterForeground” notification by doing
let notificationCenter = NotificationCenter.default
notificationCenter.addObserver(self, selector: #selector(reloadAllData), name: UIScene.willEnterForegroundNotification, object: nil)
along with the corresponding function
@objc func reloadAllData() throws {
try itemStore.loadData()
tableView.reloadData()
}
Is this all that’s needed for this challenge?
I think the only instance where this fails is in the split app view. I assume for that, instead of saving when entering background, I would have to trigger the save on each add/edit/remove actions which would mean I would need to create a separate mutator method in ItemStore to modify the individual items. The tricky part would be knowing WHEN to load the data on the scene that I’m not currently taking any action on. My best guess would be to wait for the background to be tapped before updating the data in the other scene. A bit jank but I feel like splitview probably wasn’t the goal of this challenge.
I did see that others set the set of items as a static variable therefore seemingly bypassing any need for most of this work. That looks like one of the simplest solutions to make this work for split-view imo (would still have to save on individual actions instead of listening for the didEnterBackgroundNotication
).