1

I would like to maintain my var removedIDs = [String]() even when I exit a viewController. I have checked off "Use Storyboard ID" for all Restoration IDs in my StoryBoard. Yet when I navigate away from my viewController, I still lose the contents of removedIDs.

In my AppDelegate, I have written:

func application(_ application: UIApplication, shouldSaveApplicationState coder: NSCoder) -> Bool {
    return true
}

func application(_ application: UIApplication, shouldRestoreApplicationState coder: NSCoder) -> Bool {
    return true
} 

And in my MainTextView, the controller that holds removedIds, I have the extension:

extension MainTextView {
override func encodeRestorableState(with coder: NSCoder) {
    super.encodeRestorableState(with: coder)
    coder.encode(removedIDs, forKey: "removed")
}

override func decodeRestorableState(with coder: NSCoder) {
    func decodeRestorableState(with coder: NSCoder) {
        super.decodeRestorableState(with: coder)
        removedIDs = coder.decodeObject(forKey: "removed") as? [String] ?? []
    }
  }
}

I might add that the contents of removedIDs is filled through the following report function:

 func tableView(_ tableView: UITableView, editActionsForRowAt indexPath: IndexPath) -> [UITableViewRowAction]? {
    let more = UITableViewRowAction(style: .default, title: "Report") { action, index in
            self.removedIDs!.append(self.comments[indexPath.row].snap)

What piece of the restoration state process am I missing to allow Xcode to hold my IDs?

EverythingEnds
  • 77
  • 1
  • 11
  • Can i ask why you dont just store your application data in a local database? – Scriptable Aug 07 '19 at 10:27
  • Im confused so you want you're data after killing the app too, or only while running the app ? – Shivam Gaur Aug 07 '19 at 10:28
  • @ShivamGaur I would like the data to remain after killing the app – EverythingEnds Aug 07 '19 at 10:30
  • @Scriptable This is a good alternative. I will try that if this solution does not work. I would prefer not to need to store this data in my backend – EverythingEnds Aug 07 '19 at 10:32
  • 2
    @EverythingEnds ok so this depends now, if you only need to store removeID's then go with UserDefaults , as UserDefaults is used for storing property list types , BUT ! if you're project has requirements of storing more data or will have in future cases , then go with local DB's. For your help go through links/https://stackoverflow.com/questions/45668340/best-practice-for-storing-temporary-data-in-swift , https://stackoverflow.com/questions/51219869/best-method-to-store-data-for-an-ios-app – Shivam Gaur Aug 07 '19 at 10:37

1 Answers1

2

What you are trying to do is to save application state, while you really need to save some application data. To do that you can use UserDefaults.

For example something like this:

var removedIds: [String]? {
    get { return UserDefaults.standard.value(forKey: "removedIds") as? [String] }
    set {
        if newValue != nil {
            UserDefaults.standard.set(newValue, forKey: "removedIds")
        }
        else {
            UserDefaults.standard.removeObject(forKey: "removedIds")
        }
    }
}

public func add(removedId: String) {

    guard var list = removedIds else { // Nothing saved yet
        removedIds = [removedId] // Save array with 1 item
        return
    }

    list.append(removedId) // Add new item
    removedIds = list // Save
}

And then you can:

  1. Add an item to the list of stored IDs:

    add(removedId: self.posts[indexPath.row].id)
    
  2. You can also overwrite list:

    removedIds = [self.posts[indexPath.row].id]
    
  3. Get list of previously saved removed ids:

    var x = removedIds
    
  4. Removed all IDs from storage:

    removedIds = nil
    
timbre timbre
  • 12,648
  • 10
  • 46
  • 77
  • I tried adding to my IDs: `add(removedIDs: self.comments[indexPath.row].snap)` and got `Use of unresolved identifier 'add'; did you mean 'rand'?` – EverythingEnds Aug 07 '19 at 17:12
  • 1
    @EverythingEnds `add` is a function I defined in example. You can call it by name from the same class, but of course you need to call it with appropriate qualifiers if you call it from another class – timbre timbre Aug 07 '19 at 20:45
  • Thanks. It works perfectly. I can't believe there is such a simple solution. – EverythingEnds Aug 07 '19 at 20:52