diff --git a/Tusker/DraftsManager.swift b/Tusker/DraftsManager.swift index 65ca778d2c..95878030a3 100644 --- a/Tusker/DraftsManager.swift +++ b/Tusker/DraftsManager.swift @@ -16,9 +16,11 @@ class DraftsManager: Codable { private static var archiveURL = DraftsManager.documentsDirectory.appendingPathComponent("drafts").appendingPathExtension("plist") static func save() { - let encoder = PropertyListEncoder() - let data = try? encoder.encode(shared) - try? data?.write(to: archiveURL, options: .noFileProtection) + DispatchQueue.global(qos: .userInitiated).async { + let encoder = PropertyListEncoder() + let data = try? encoder.encode(shared) + try? data?.write(to: archiveURL, options: .noFileProtection) + } } static func load() -> DraftsManager { @@ -37,8 +39,10 @@ class DraftsManager: Codable { return drafts.sorted(by: { $0.lastModified > $1.lastModified }) } - func create(text: String, contentWarning: String?, attachments: [DraftAttachment]) { - drafts.append(Draft(text: text, contentWarning: contentWarning, lastModified: Date(), attachments: attachments)) + func create(text: String, contentWarning: String?, attachments: [DraftAttachment]) -> Draft { + let draft = Draft(text: text, contentWarning: contentWarning, lastModified: Date(), attachments: attachments) + drafts.append(draft) + return draft } func remove(_ draft: Draft) { diff --git a/Tusker/Screens/Compose/ComposeViewController.swift b/Tusker/Screens/Compose/ComposeViewController.swift index 5fdf3762cd..b7694407b6 100644 --- a/Tusker/Screens/Compose/ComposeViewController.swift +++ b/Tusker/Screens/Compose/ComposeViewController.swift @@ -338,8 +338,9 @@ class ComposeViewController: UIViewController { if let currentDraft = self.currentDraft { currentDraft.update(text: self.statusTextView.text, contentWarning: cw, attachments: attachments) } else { - DraftsManager.shared.create(text: self.statusTextView.text, contentWarning: cw, attachments: attachments) + self.currentDraft = DraftsManager.shared.create(text: self.statusTextView.text, contentWarning: cw, attachments: attachments) } + DraftsManager.save() } @objc func close() {