File exists on main thread but background thread says it does not (iOS)

1.1k views Asked by At

I am exporting an AVMutableComposition video to a temporary location and then successfully transferring it into the Camera Roll. Upon exporting, I check on the main thread if the returned Camera Roll URL stored as let videoPath exists with:

if FileManager.default.fileExists(atPath: videoPath) {
   print("FILE AVAILABLE")
} else {
   print("FILE NOT AVAILABLE")
}

It does. So I place it in an AVPlayer and it plays all good. So then I want to upload it to my web server so I cast that same URL as contents of a Data object in a background thread and before doing so I run the same FileManager.default.fileExists and this time the console says FILE NOT AVAILABLE which obviously makes the Data try constructor fail.

Before calling the background thread function, I even check on the main thread if the file exists and it does but then in the background it says it does not.

Now what is the strangest part and why I came to SO is because after this initial file check fails, I move to a different view which can run that same background function and on that second time (all subsequent times after the first failure) the console shows FILE AVAILABLE and does its thing.

What is going on here?

1

There are 1 answers

0
simplexity On

Ok if anyone else has this issue, I solved it through the following manner:

Instead of checking for the videoPath and creating the Data object in the background thread (and risking it not being able to detect it for reasons beyond me), I just created the Data object on the main thread upon export and simply pass the created Data object to the function for uploading. Simple and took care of the problem!

Sometimes the best solution is not to figure out the system, but to beat it.