0

When I save a file to iCloud, when the app shuts down, the file is not there anymore.

Here is my code before the app shuts down with the print results in comments beside the print statements:

    FileManager.default.createFile(atPath: saveToURL.path, contents: dataImage, attributes: nil)

    print("??? \(saveToURL.path) imagePicker file exists=", FileManager.default.fileExists(atPath: saveToURL.path)) // ??? /private/var/mobile/Library/Mobile Documents/iCloud~us~gnolaum~TrialVisionBoard/12BF0AB3-C0EB-4D13-A37E-7853839225BD.jpg cellForRowAt file exists= false

Notice that when the code checks if the file exists after the file is created, it checks out true.

Following is the code after the app starts again:

     print("??? \(mediaURL.path) cellForRowAt file exists=", FileManager.default.fileExists(atPath: mediaURL.path)) // ??? /private/var/mobile/Library/Mobile Documents/iCloud~us~gnolaum~TrialVisionBoard/12BF0AB3-C0EB-4D13-A37E-7853839225BD.jpg cellForRowAt file exists= false

Notice that the url path is identical to the path before the app shut down but the file after the app restarts does not exist.

I suspect I need to set the correct attributes for the file I create. Anyone know what exactly I need to do to get the file to stay saved in iCloud?

ADDITIONAL CONTENT:

I noticed that I don't have this problem when I run the project on iPhone 5 Simulator. The problem occurs when I run the project on my iPhone 5 device.

daniel
  • 1,446
  • 3
  • 29
  • 65
  • 1
    Check this https://stackoverflow.com/a/33912666/1644934 Or you can consider using CloudKit or third part library. – Vincent Sit Jul 22 '18 at 05:15
  • @Vincent Like I explained, I saved to iCloud. I am using CloudKit. I should have shown the following line of code: "var saveToURL = FileManager.default.url(forUbiquityContainerIdentifier: nil)!" to show that saveToURL is a url to iCloud. – daniel Jul 23 '18 at 05:29
  • @Vincent By the way, that line of code comes before the other lines of code and has other lines of code to add to it to make a complete url before the you get to the code in the original post. – daniel Jul 23 '18 at 05:41
  • I don't have much experience with iCloud, I can only give you some advice. If there is no good answer here, I suggest you check out the code for some related open source libraries. – Vincent Sit Jul 23 '18 at 09:04
  • @Vincent Ok. Thanks. I actually ran into some confusing things. Sometimes my code works. Sometimes it doesn't, depending on which Simulator or device I'm using. I don't know what factors determine the differences. – daniel Jul 23 '18 at 15:23

1 Answers1

0

I totally missed the problem altogether. I found I needed to take an entirely different approach to dealing with iCloud storage as this documentation instructs:

https://developer.apple.com/library/archive/documentation/FileManagement/Conceptual/FileSystemProgrammingGuide/iCloud/iCloud.html#//apple_ref/doc/uid/TP40010672-CH12-SW51

daniel
  • 1,446
  • 3
  • 29
  • 65