UIManagedDocuemnt will not open

My application (Xcode 9.2, Swift 4) uses UIManagedDocument as the core stack of Core Data. Everything worked fine for several months, but recently, I noticed several cases where the application will not load for existing users, because the main init init does not end. This usually happens after the application crashes (I think, but not sure).

I was able to recreate the problem in the debugger and narrowed the problem down to the following scenario:

The application starts β†’ the main data is called to start β†’ the UIManagedDocument object init'd β†’ check the status of doc == closed β†’ the open () call on doc β†’ open never ends - the callback closure is never called.

I have subclassed UIManagedDocument so that I can override configurePersistentStoreCoordinator() to check if it has reached this point, but it is not. A subclass for handleError() never called.

The open() process never reaches this point. What I see if I pause the debugger is that a couple of threads are blocked on the mutex / semaphore associated with the open procedure: enter image description here enter image description here

The second thread (11) seems to handle some kind of file conflict, but I can't figure out what and why. When I check a documentState just before opening a file, I see its value [.normal, .closed]

This code for initializing a document is pretty straight forward and works as expected for most use and use cases:

 class MyDataManager { static var sharedInstance = MyDataManager() var managedDoc : UIManagedDocument! var docUrl : URL! var managedObjContext : NSManagedObjectContext { return managedDoc.managedObjectContext } func configureCoreData(forUser: String, completion: @escaping (Bool)->Void) { let dir = UserProfile.profile.getDocumentsDirectory() docUrl = dir.appendingPathComponent(forUser + GlobalDataDocUrl, isDirectory: true) managedDoc = UIManagedDocument(fileURL: docUrl) //allow the UIManagedDoc to perform lieghtweight migration of the DB in case of small changes in the model managedDoc.persistentStoreOptions = [ NSMigratePersistentStoresAutomaticallyOption: true, NSInferMappingModelAutomaticallyOption: true ] switch (self.managedDoc.documentState) { case UIDocumentState.normal: DDLogInfo("ManagedDocument is ready \(self.docUrl)") case UIDocumentState.closed: DDLogInfo("ManagedDocument is closed - will open it") if FileManager.default.fileExists(atPath: self.docUrl.path) { self.managedDoc.open() { [unowned self] (success) in DDLogInfo("ManagedDocument is open result=\(success)") completion(success) } } else{ self.managedDoc.save(to: self.managedDoc.fileURL, for: .forCreating) { [unowned self] (success) in DDLogInfo("ManagedDocument created result=\(success) ") completion(success) } } case UIDocumentState.editingDisabled: fallthrough case UIDocumentState.inConflict: fallthrough case UIDocumentState.progressAvailable: fallthrough case UIDocumentState.savingError: fallthrough default: DDLogWarn("ManagedDocument status is \(self.managedDoc.documentState.rawValue)") } } } 

Again, the closure managedDoc.open() to managedDoc.open() will never be called. It seems that the file was left in some bad condition and could not be opened. BTW, if I copy the application container from the device to my Mac and open the SQLLite repository, I see that everything is there as expected.

0
ios swift core-data
source share

No one has answered this question yet.

See similar questions:

6
UIManagedDocument saveToURL completeHandler is not called - error message: "The reader is not allowed to access the URL."

or similar:

eleven
Why should I ever use an idle me?
10
Quick close [weak self] and asynchronous tasks
3
Can create, but not open, iCloud-enabled UIManagedDocument
2
UIDocument open / close behavior
2
Strategy to launch a task - and avoid race conditions - from the application delegate
2
Core Data Swift crash on failure of universal function in optimized builds
2
iOS 5.1 openWithCompletionHandler does not succeed
0
Initializing a new UIDocument with some data
0
UIManagedDocument - validation of the main data object
0
UIManagedDocument and iCloud: "The resulting ubiquity name is already in use."

All Articles