Fixed LocalFileMonitor dir not refreshing, Fixed FTP connection refused error by retrying

This commit is contained in:
Amir Abbas
2018-04-25 13:23:15 +04:30
parent 4202f5e1bd
commit f2cd571d7a
2 changed files with 22 additions and 3 deletions
+19 -2
View File
@@ -790,10 +790,27 @@ public class FileProviderStreamTask: URLSessionTask, StreamDelegate {
}
}
private var _retriesForInputStream: Int = 0
private var _retriesForOutputStream: Int = 0
open func stream(_ aStream: Stream, handle eventCode: Stream.Event) {
if eventCode.contains(.errorOccurred) {
self._error = aStream.streamError
streamDelegate?.urlSession?(_underlyingSession, task: self, didCompleteWithError: error)
let retries: Int
if aStream == self.inputStream {
retries = _retriesForInputStream
_retriesForInputStream += 1
} else if aStream == self.outputStream {
retries = _retriesForOutputStream
_retriesForOutputStream += 1
} else {
return
}
if retries < 3 {
aStream.open()
} else {
self._error = aStream.streamError
streamDelegate?.urlSession?(_underlyingSession, task: self, didCompleteWithError: error)
}
}
if aStream == self.inputStream && eventCode.contains(.hasBytesAvailable) {
+3 -1
View File
@@ -101,7 +101,9 @@ public final class LocalFileMonitor {
public init(url: URL, handler: @escaping ()->Void) {
self.url = url
descriptor = open((url.absoluteURL as NSURL).fileSystemRepresentation, O_EVTONLY)
source = DispatchSource.makeFileSystemObjectSource(fileDescriptor: descriptor, eventMask: [.write, .extend, .attrib, .delete, .rename], queue: qq)
let event: DispatchSource.FileSystemEvent = url.fileIsDirectory ? [.write] : .all
source = DispatchSource.makeFileSystemObjectSource(fileDescriptor: descriptor, eventMask: event, queue: qq)
// Folder monitoring is recursive and deep. Monitoring a root folder may be very costly
// We have a 0.2 second delay to ensure we wont call handler 1000s times when there is
// a huge file operation. This ensures app will work smoothly while this 250 milisec won't