JohnSundell / Files

A nicer way to handle files & folders in Swift
MIT License
2.53k stars 182 forks source link

Object-oriented implementations #68

Closed ghost closed 6 years ago

ghost commented 6 years ago

Hi,

There seems to be a danger passing File around, by reference.

If I have the following

let file = try folder.file(named: "name.pdf")

And I pass it into the following function.

func store(_ file: File) throws

Moving forward, if I interact with this same File reference, it may no longer have a valid path. The store function above may perform moveItem, for example.

Idea 1

Perhaps we could create an alias?

Idea 2

Might it be worth making a distinction between a File that you know exists and a PotentialFile that hasn't been confirmed to exist? You see this kind of 'evaluate at last minute' mentality throughout Swift.

The same kind of mentality as

I suppose we would have to handle a change in state, in both directions.

Idea 3

Perhaps we could harness this API?

FileReferenceURL

Check Resource Is Reachable

ghost commented 6 years ago

Actually, could we just update the path on the File instance, from within the implementation of move.

clayellis commented 6 years ago

@rob-nash Not sure what you mean. Operations on File and Folder that would result in their underlying file system representation changing do in fact update their internal path (see: FileSystem.Item.move(to:)). And since File and Folder are classes, when passed by reference, a change in one code path would affect the reference in another.

Can you write a test showing the behavior you're trying to design around?

ghost commented 6 years ago

Just wrote a test and I couldn't reproduce the issue. Started looking at other reason why this might be happening and it turns out I'm running code on the wrong thread.

Thanks for helping me think 'outside of the box'.

Sorry for wasting time.

clayellis commented 6 years ago

Never a waste! Glad to help 👍