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()
let errorPointer: NSErrorPointer = nil
let fileCoordinator = NSFileCoordinator(filePresenter: self)
fileCoordinator.coordinate(writingItemAt: fileURL, options: .forReplacing, error: errorPointer, byAccessor: { writeURL in
do { do {
let url = URL(fileURLWithPath: filename) try opmlDocumentString.write(to: writeURL, atomically: true, encoding: .utf8)
try opmlDocumentString.write(to: url, atomically: true, encoding: .utf8)
} catch let error as NSError { } catch let error as NSError {
os_log(.error, log: log, "Save to disk failed: %@.", error.localizedDescription) 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)
} }
} }
@ -103,21 +112,30 @@ private extension OPMLFile {
func parsedOPMLItems() -> [RSOPMLItem]? { func parsedOPMLItems() -> [RSOPMLItem]? {
let opmlFileURL = URL(fileURLWithPath: filename) var fileData: Data? = nil
var fileData: Data? let errorPointer: NSErrorPointer = nil
let fileCoordinator = NSFileCoordinator(filePresenter: self)
fileCoordinator.coordinate(readingItemAt: fileURL, options: [], error: errorPointer, byAccessor: { readURL in
do { do {
fileData = try Data(contentsOf: opmlFileURL) fileData = try Data(contentsOf: readURL)
} 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)
return nil os_log(.error, log: log, "OPML read from disk failed: %@.", error.localizedDescription)
} }
})
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 {