Skip to content
This repository has been archived by the owner on Feb 13, 2019. It is now read-only.

Fix bug where document might not autosave when going to background #17

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 19 additions & 1 deletion Textor/Controller/DocumentViewController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -108,12 +108,15 @@ class DocumentViewController: UIViewController {

override func viewWillAppear(_ animated: Bool) {
super.viewWillAppear(animated)


NotificationCenter.default.addObserver(self, selector: #selector(saveDocument), name: .UIApplicationWillResignActive, object: nil)

}

override func viewDidDisappear(_ animated: Bool) {
super.viewDidDisappear(animated)

NotificationCenter.default.removeObserver(self)
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is not needed, observers are automatically removed nowadays.

Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Side note: it's not safe to assume that viewDidDisappear means the end of the view controller's life.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ah, didn't know that!


documentsClosed += 1

Expand Down Expand Up @@ -189,3 +192,18 @@ extension DocumentViewController: StoryboardIdentifiable {
}

}

extension DocumentViewController {
@objc func saveDocument() {
guard let document = self.document else {
return
}
let currentText = document.text ?? ""

document.text = self.textView.text

if currentText != self.textView.text {
document.save(to: document.fileURL, for: .forOverwriting, completionHandler: {success in print(success)})
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is save(to, forOverwriting the best approach here? Maybe document .updateChangeCount(.done) is better?

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah, I have a couple ideas on how to make this better. I'll try them out this weekend.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Including the one you recommend

}
}
}