FileWatcher icon indicating copy to clipboard operation
FileWatcher copied to clipboard

Callback is getting triggered twice in SwiftUI project

Open aryamansharda opened this issue 2 years ago • 10 comments

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?

aryamansharda avatar Jul 07 '23 05:07 aryamansharda

Does it also happen if you try to run the example project?

eonist avatar Jul 07 '23 16:07 eonist

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

aryamansharda avatar Jul 07 '23 16:07 aryamansharda

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 😸

eonist avatar Jul 07 '23 16:07 eonist

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

aryamansharda avatar Jul 07 '23 17:07 aryamansharda

Try not to use vim either. Try to just do it in finder. These editors do extra steps because reasons. 😅

eonist avatar Jul 07 '23 21:07 eonist

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. 😅

eonist avatar Jul 07 '23 21:07 eonist

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.

borisyurkevich avatar Dec 05 '23 17:12 borisyurkevich

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.

eonist avatar Dec 06 '23 08:12 eonist