UIDocument not saving to file despite indicating success

前端 未结 3 618
佛祖请我去吃肉
佛祖请我去吃肉 2021-02-05 04:45

I\'m trying to open, modify, and save a file in iCloud Drive using UIDocument. When I call save(to:for:completionHandler:) with the file location and u

3条回答
  •  生来不讨喜
    2021-02-05 04:51

    The way the initial file generation works for me is:

        let doc = YourUIDocumentClass(fileURL: fileURL) 
        doc.save(to: fileURL, for: .forCreating) { success in
            ...            
        }
    

    Then modify the file and then do:

        doc.save(to: fileURL, for: .forOverwriting)  { success in
            ...            
        }
    

    when done. And subsequent accesses to the file are done by:

        doc.open() { success in
            ...
        }
    
        doc.close() { success in
            ...            
        }
    

    You might also need to do a:

        doc.updateChangeCount(.done)
    

    while the file is open to tell the document there are unsaved changes. Just setting this will cause a save after a few seconds. You don't even need the close to do that.

    The ... means that you either have to nest all these or make sure there is enough time between them so they are completed.

提交回复
热议问题