eonist / my-swift-projects

An overview of my most relevant open-source projects on GitHub
263 stars 22 forks source link

File Watcher / Auto-save issue #2

Open fracturedpixel opened 8 years ago

fracturedpixel commented 8 years ago

FileWatcher event closure is getting called in a few cases where I do not want it to be called for my use case. Monitoring a swift file in Xcode, if changes made, and switch to a different application without actually saving, the closure is called when the Auto-Save is fired off. Is there a way to look for actual saves only, and ignore the auto-saves?

eonist commented 8 years ago

Wow. I'm impressed you got it working. This is how I use it:

let url:String = "~/Desktop/ElCapitan/advance/table/table.css"
StyleManager.addStylesByURL(url,true)
        fileWatcher = FileWatcher([url.tildePath])
        fileWatcher!.event = { event in
            //Swift.print(self)
            Swift.print(event.description)
            if(event.fileChange && event.path == url.tildePath) {
                StyleManager.addStylesByURL(url,true)
                ElementModifier.refreshSkin(self)
                ElementModifier.floatChildren(self)
            }
        }
        fileWatcher!.start()

What this code snippet does is that it watches an URL, and when you save the .css file and it has a change. Then the everything inside the closure is fired off. Now that being said, Maybe XCode Auto-Saves on app switch. In which case maybe you can assert if there actually was a change in the file, by comparing the file to a snapshot of the file you are monitoring.

Also, The file watching API apple provides is a bit tricky to use. Some things works, other things doesn't work. Well you can always get it working but it requires a bit of digging around to get working.

eonist commented 8 years ago

Here is the transcript of a conversation I had with a fellow GitHubber when i Made FileWatcher: https://dl.dropboxusercontent.com/u/2559476/Event%20for%20file%20change%20%C2%B7%20Issue%20%231%20%C2%B7%20gurinderhans_SwiftFSWatcher.pdf Maybe it can yield more context in how my FileWatcher lib works.

eonist commented 8 years ago

You can also turn of auto-saving in XCode: http://stackoverflow.com/questions/15468313/how-to-disable-auto-save-in-xcode-4-6

Maybe the content of the file is the same, but the modified date changes everytime you switch apps?, which in turn triggers the event.fileChange event type. There is also the event.modified event type, I can't Recall the difference between the fileChange and the modified flag.

If the modified flag works then I can add that as a note in the Code Documentation.