Monitorer un fichier avec GCD

Bonjour à  tous !

J'essaie de monitorer un fichier en Swift et jusque là  j'ai réussi à  moitié.

 

L'idée est que l'utilisateur ouvre un fichier en input et une action est réalisée à  l'ouverture. Chaque fois que le fichier va être modifié ou supprimé une action s'en suivra.

 

J'ai ce code ci (glané de-ci de-là  sur SO et GH) :



func startWatchingFile(url: NSURL) {
let fd: CInt
let queue = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0)
if let path = url.path {
if NSFileManager.defaultManager().fileExistsAtPath(path) {
fd = open(url.path!.fileSystemRepresentation(), O_EVTONLY)
self.source = dispatch_source_create(DISPATCH_SOURCE_TYPE_VNODE, UInt(fd), DISPATCH_VNODE_DELETE | DISPATCH_VNODE_WRITE, queue)
dispatch_source_set_event_handler(self.source) {
dispatch_async(dispatch_get_main_queue()) {
NSNotificationCenter.defaultCenter().postNotificationName(kFileChangedNotification, object: nil)
}
}
dispatch_source_set_cancel_handler(self.source) {
close(fd)
}

dispatch_resume(self.source)
}
isWatching = true
}
}

L'ennui c'est que ça marche du tonnerre mais seulement... une fois. A la première modification après chargement l'action est bien exécutée. Le soft va par contre se foutre royalement des modifications ultérieures après avoir géré la première...


 


Qu'est-ce que j'ai oublié ? Là , comme ça, je vois pas...


Mots clés:

Réponses

  • Joanna CarterJoanna Carter Membre, Modérateur
    Il me semble que ce sujet http://stackoverflow.com/questions/11355144/file-monitoring-using-grand-central-dispatch pourrait répondre à  ta question ?
  • PyrohPyroh Membre
    mars 2015 modifié #3

    Il me semble que ce sujet http://stackoverflow.com/questions/11355144/file-monitoring-using-grand-central-dispatch pourrait répondre à  ta question ?

    Indirectement oui ! 
    En fait il s'avère que certains éditeurs de texte suppriment le fichier avant de le sauver à  nouveau il faut alors veiller à  prendre ça en compte.
     
    Ce qui donne ce code qui fonctionne : 
    func startWatchingFile(url: NSURL) {
    let fd: CInt
    let queue = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0)
    if let path = url.path {
    if NSFileManager.defaultManager().fileExistsAtPath(path) {
    fd = open(url.path!.fileSystemRepresentation(), O_EVTONLY)
    self.source = dispatch_source_create(DISPATCH_SOURCE_TYPE_VNODE, UInt(fd), DISPATCH_VNODE_DELETE | DISPATCH_VNODE_WRITE, queue)
    dispatch_source_set_event_handler(source) { [unowned self] in
    let flags = dispatch_source_get_data(self.source)
    if flags & DISPATCH_VNODE_DELETE == DISPATCH_VNODE_DELETE {
    dispatch_source_cancel(self.source)
    self.startWatchingFile(self.mdFileURL)
    }
    dispatch_async(dispatch_get_main_queue()) { [unowned self] in
    self.markdownFileChanged()
    }
    }
    dispatch_source_set_cancel_handler(source) {
    close(fd)
    }

    dispatch_resume(self.source)
    }
    isWatching = true
    }
    }
    Merci pour le lien Joanna !
Connectez-vous ou Inscrivez-vous pour répondre.