Add file coordination for the OPML file

This commit is contained in:
Maurice Parker 2019-09-13 10:48:04 -05:00
parent 4d118d89db
commit 762e230200
1 changed files with 36 additions and 18 deletions

View File

@ -22,12 +22,12 @@ final class OPMLFile: NSObject, NSFilePresenter {
} }
private var isLoading = false private var isLoading = false
private let filename: String private let fileURL: URL
private let account: Account private let account: Account
private let operationQueue: OperationQueue private let operationQueue: OperationQueue
var presentedItemURL: URL? { var presentedItemURL: URL? {
return URL(fileURLWithPath: filename) return fileURL
} }
var presentedItemOperationQueue: OperationQueue { var presentedItemOperationQueue: OperationQueue {
@ -35,7 +35,7 @@ final class OPMLFile: NSObject, NSFilePresenter {
} }
init(filename: String, account: Account) { init(filename: String, account: Account) {
self.filename = filename self.fileURL = URL(fileURLWithPath: filename)
self.account = account self.account = account
operationQueue = OperationQueue() operationQueue = OperationQueue()
operationQueue.maxConcurrentOperationCount = 1 operationQueue.maxConcurrentOperationCount = 1
@ -83,11 +83,20 @@ private extension OPMLFile {
func save() { func save() {
let opmlDocumentString = opmlDocument() let opmlDocumentString = opmlDocument()
do {
let url = URL(fileURLWithPath: filename) let errorPointer: NSErrorPointer = nil
try opmlDocumentString.write(to: url, atomically: true, encoding: .utf8) let fileCoordinator = NSFileCoordinator(filePresenter: self)
} catch let error as NSError {
os_log(.error, log: log, "Save to disk failed: %@.", error.localizedDescription) fileCoordinator.coordinate(writingItemAt: fileURL, options: .forReplacing, error: errorPointer, byAccessor: { writeURL in
do {
try opmlDocumentString.write(to: writeURL, atomically: true, encoding: .utf8)
} catch let error as NSError {
os_log(.error, log: log, "OPML save to disk failed: %@.", error.localizedDescription)
}
})
if let error = errorPointer?.pointee {
os_log(.error, log: log, "OPML save to disk coordination failed: %@.", error.localizedDescription)
} }
} }
@ -102,22 +111,31 @@ private extension OPMLFile {
} }
func parsedOPMLItems() -> [RSOPMLItem]? { func parsedOPMLItems() -> [RSOPMLItem]? {
var fileData: Data? = nil
let errorPointer: NSErrorPointer = nil
let fileCoordinator = NSFileCoordinator(filePresenter: self)
let opmlFileURL = URL(fileURLWithPath: filename) fileCoordinator.coordinate(readingItemAt: fileURL, options: [], error: errorPointer, byAccessor: { readURL in
var fileData: Data? do {
do { fileData = try Data(contentsOf: readURL)
fileData = try Data(contentsOf: opmlFileURL) } catch {
} catch { // Commented out because its not an error on first run.
// Commented out because its not an error on first run. // TODO: make it so we know if its first run or not.
// TODO: make it so we know if its first run or not. //NSApplication.shared.presentError(error)
//NSApplication.shared.presentError(error) os_log(.error, log: log, "OPML read from disk failed: %@.", error.localizedDescription)
return nil }
})
if let error = errorPointer?.pointee {
os_log(.error, log: log, "OPML read from disk coordination failed: %@.", error.localizedDescription)
} }
guard let opmlData = fileData else { guard let opmlData = fileData else {
return nil return nil
} }
let parserData = ParserData(url: opmlFileURL.absoluteString, data: opmlData) let parserData = ParserData(url: fileURL.absoluteString, data: opmlData)
var opmlDocument: RSOPMLDocument? var opmlDocument: RSOPMLDocument?
do { do {