Callback is getting triggered twice in SwiftUI project
Hi, I'm using this library in a brand new vanilla SwiftUI project and anytime I make a change to a file, the callback is getting triggered twice:
import FileWatcher
class ContentScreenViewModel: ObservableObject {
let filewatcher = FileWatcher(["/Users/aryamansharda/Documents/test.md"])
init() {
filewatcher.start() // start monitoring
filewatcher.callback = { event in
print("Something happened here: " + event.path)
}
}
}
struct ContentView: View {
@StateObject var viewModel = ContentScreenViewModel()
var body: some View {
VStack {
Text("Hello, world!")
}
.padding()
}
}
This is the entirety of the program's code and any time I make a change to test.md, I see this in the Debugger:
Something happened here: /Users/aryamansharda/Documents/test.md
Something happened here: /Users/aryamansharda/Documents/test.md
When I use event.description in the print statement, it's showing that the file was renamed even though only its contents were changed:
The file /Users/aryamansharda/Documents/test.md was renamed
The file /Users/aryamansharda/Documents/test.md was created
For other files, both lines mention renamed:
The file /Users/aryamansharda/Documents/calc/.git/HEAD was renamed
The file /Users/aryamansharda/Documents/calc/.git/HEAD was renamed
Is this expected?
Does it also happen if you try to run the example project?
Hi!
Yes, it does.
Here's the output from the example project:
Something happened here: /Users/aryamansharda/Documents/bowman/.git/HEAD
event.description: The file /Users/aryamansharda/Documents/bowman/.git/HEAD was renamed
event.flags: 67584
Something happened here: /Users/aryamansharda/Documents/bowman/.git/HEAD
event.description: The file /Users/aryamansharda/Documents/bowman/.git/HEAD was renamed
event.flags: 67584
The same thing happens for a simple text modification to a Markdown file:
Something happened here: /Users/aryamansharda/Documents/test.md
event.description: The file /Users/aryamansharda/Documents/test.md was renamed
event.flags: 67584
Something happened here: /Users/aryamansharda/Documents/test.md
event.description: The file /Users/aryamansharda/Documents/test.md was created
event.flags: 87296
Looks like you're testing this with git. Git tends to do a lot of strange things to files. Try just deleting things and renaming things manually. and see if it happens. My theory is that this is git being git 😸
Yeah, I figured .git was over-complicating things, but simple edits to a basic text file also result in the same issue.
In this case, I'm just opening the file, adding an extra character, and then saving it via Vim:
Something happened here: /Users/aryamansharda/Documents/test.md
event.description: The file /Users/aryamansharda/Documents/test.md was renamed
event.flags: 67584
Something happened here: /Users/aryamansharda/Documents/test.md
event.description: The file /Users/aryamansharda/Documents/test.md was created
event.flags: 87296
Try not to use vim either. Try to just do it in finder. These editors do extra steps because reasons. 😅
You can also try things like copy / delete / create new file etc. To try and trouble shoot. Something might have changed in the latest macOS as well. So we might have to adjust some events in the lib if it has etc. FileWatching is finicky business, because apple API is like 10-20 years old. 😅
Another tip, which might help OP, I have noticed duplicate callbacks in my project with .DS_Store in the path. It's trivial to filter them out.
If I recall. It's because macOS makes no distinction between creation and rename or something like that. And when the OS spawns a new file or update a file. Two calls are made in quick succession. And we cant distinguish because the OS api is incomplete. Or something like this. There might be more precise info on this in code comments or in the wiki of this project which has comments from when it was made.