0

My app, when run, creates a json document (using a UIDocument subclass) in its document directory. And then, when opening up the UIDocumentPickerViewController to select a file, if the app has written a new file, the behaviour is as expected.

However, if I run the app again (and overwrite the last created file), the delegate method didPickDocumentsAt doesn't get called, unless I browse around for a few seconds.

What am I missing here?

@IBAction func showDocumentPicker() {
    let documentPicker = UIDocumentPickerViewController(documentTypes: [kUTTypeJSON as String], in: .import)
    documentPicker.allowsMultipleSelection = false
    documentPicker.delegate = self
    self.present(documentPicker, animated: true, completion: nil)
} //this function is in the initial definition of the class and is connected to a UIBarButton


extension BudgetExportViewController: UIDocumentPickerDelegate {
    func documentPicker(_ controller: UIDocumentPickerViewController, didPickDocumentsAt urls: [URL]) {
        print("selected document: \(urls.first)")
        print("555555555555555555555555555555")
        //document = BudgetExportDocument(fileURL: urls.first!)
//                CFURLStartAccessingSecurityScopedResource(urls.first! as CFURL)
//                let documentData = try? Data.init(contentsOf: urls.first!)
//                let json = try? JSONDecoder().decode(BudgetExportData.self, from: documentData!)
//                budgetThisMonth = json
//                print("Budgetthismonth")
//                print(budgetThisMonth)
//                CFURLStopAccessingSecurityScopedResource(urls.first! as CFURL)
    }
}
  • Dont you need to assign the picker to a property so that it doesn’t get released? I’m wondering if there’s enough time for it to call its delegate before it gets deallocated. – Thomas Deniau Aug 20 '18 at 08:33
  • It's working now and it does call its delegate method every time. I guess it doesn't get deallocated, but I've only tried it with 10 seconds tops of browsing through the picker (and the minimum was when the file to be selected was already on screen after the view controller was loaded). Still, I can't figure out why overwriting a file made it not call its delegate method for a few seconds. I'll make it a property if I run into this issue in the future. Thanks for the suggestion! – Tudor Croitoru Domi Aug 21 '18 at 14:08

1 Answers1

1

Apparently, the problem was the fact that I was overwriting the same file so many times. Now, if a file with the same name exists, it won't write over it and the UIDocumentPickerViewController behaviour is as expected.