Reputation: 11
I’m trying to write a fairly simple UIDocument based app and I want the data for each document to be a parent directory containing some image files. To get started I chose to use Apple’s ShapeEdit sample as that seems to handle lots of the complexity of having files in iCloud. It does only use a single plist file for it’s document data though.
As a first step I got the ShapeEdit app up and running on my iPad and everything works as expected. I have the app and iCloud Drive app running in split view on my iPad so I can see the files ShapeEdit is creating. As expected each time I add a new document a new file appears in iCloud Drive.
To migrate the ShapeEdit app to use more files I moved over to using NSFileWrapper. When ShapeEdit creates a new document it copies a template file. I changed this to just create a new folder using
try fileManager.createDirectoryAtURL(writeIntent.URL, withIntermediateDirectories: false, attributes: nil)
I then modified the UIDocument code to have it read from a NSFileWrapper like this
override func loadFromContents(contents: AnyObject, ofType typeName: String?) throws {
guard let data = contents as? NSFileWrapper else {
fatalError("Cannot handle contents of type \(contents.dynamicType).")
}
fileWrapper = data
I also hobbled some of the other functions in the document to return a simple image for thumbnails, just to get it running and set LSTypeIsPackage to true. Finally I left the shape selection UI there to trigger a document update with
updateChangeCount(.Done)
Again this all works fine. Creating a new document creates a new Directory named Untitled.shapeFile and it shows up in iCloud. Now for odd thing number one. If I open that document and tap on the shape selection UI to trigger a change and wait a few seconds another Untitled.shapeFile directory shows in iCloud. In fact every time I do this I get another one. These seem to only be local to the iPad though as iCloud on my mac only has one copy. Deleting any of these directories within the iCloud Drive app causes all of them to be deleted.
For the next step I added little bit of code to the UIDocument loadFromContents() function
fileWrapper?.addRegularFileWithContents(UIImagePNGRepresentation(image!)!, preferredFilename: "MYImage.png")
so every time the document is loaded it adds another image file to the parent directory.
To begin with this does exactly as I hoped. Open the doc, tap the shape select UI see the MyImage.png file turn up in iCloud Drive inside Untitled.shapeFile. Keep closing and opening and I get more MyImage.png files with some junk on the front which, I assume, is iCloud’s way of preventing files with the same name so all is well there. Except for strange thing number two. Shortly after the MYImage.png file shows in iCloud Drive another one pops up called MyImage 2.png. If I keep closing and opening all the versions of MyImage also get a copy with a 2. My edited version of ShapeEdit never adds any image files with this name format. These extra images do show up on the mac as well so they aren't the same strange phantom copies as the main document directory.
So two things
1) Why do I get multiple copies of the Untitled.shapeFile doc which seem to actually be the same thing?
2) Why do I get extra copies of my image file with a 2 added that my app never had anything to do with?
Any suggestions welcome before this drives me crackers.
Upvotes: 1
Views: 402