Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Critical UIDocument Failure on Entering/Exiting Application #31

Closed
bgfriend0 opened this issue Jan 29, 2014 · 6 comments
Closed

Critical UIDocument Failure on Entering/Exiting Application #31

bgfriend0 opened this issue Jan 29, 2014 · 6 comments

Comments

@bgfriend0
Copy link
Contributor

My coworker and I spent the last few days slamming our heads through walls debugging a subtle issue. Finally solved it.

Problem:

Short: iOS is passing messages to the UIDocuments iCloudDocumentSync uses AFTER the UIDocuments have been deallocated.

Long: iCloudDocumentSync uses UIDocuments (iCloudDocument) to wrap data conveniently for developers. Unfortunately, UIDocuments are somewhat heavy weight in that they have state which the system relies on to determine what messages to send to the UIDocument. It turns out that when the app becomes active again after coming back from another screen, iOS sends a (setInConflict:) message to all UIDocuments it thinks are open, but the UIDocuments have already been deallocated (since they are only used as wrappers). The bug was hard to pin down (try searching for "setInConflict" on Google... there's one result.)

Solution:

You have two choices. Either make a conspicuous caution in the iCloudDocumentSync documentation that ANY UIDocument that is passed out from iCloudDocumentSync (e.g., in the completion handler for the retrieveDocument method) should either be retained by the developer or be closed with [document closeWithCompletionHandler:]. I personally chose to close my UIDocuments.

The other option is to close the UIDocument inside iCloudDocumentSync so the developer doesn't need to know anything about closing UIDocuments.

@Sam-Spencer
Copy link
Member

Great bug fix / bug spotting! I'll take a look at it and start working on a solution (option 2). Although I do have a question: Are you using ARC in classes that are related to iCloudDocumentSync? In a previous pull request you noted that you were using MRC - just wondering if this issue also appears with ARC?

@bgfriend0
Copy link
Contributor Author

Excellent point that I didn't think of. This is an older project--some classes are ARC, some are not. Obviously I'm compiling iCloudDocumentSync with ARC. The class that is actually touching the UIDocument is NOT ARC-enabled. So you may want to do some testing to see if you can reproduce this issue with ARC enabled. Regardless, though, it's probably a good idea to make some provision for closing the UIDocument when you know it will no longer be used.

@bgfriend0
Copy link
Contributor Author

Another thought: Since you are exposing a UIDocument to the developer, if you are going to close it inside iCloudDocumentSync automatically, you may want to document that fact so developers familiar with UIDocument (1) know the document is being closed automatically and (2) don't depend on the UIDocument remaining open. For example, what if they get the UIDocument in the completion handler to retrieveDocument and initiate an async call that depends on that UIDocument remaining open? Chances are, by the time that call comes back, iCloudDocumentSync will have closed the document.

@JackJackBauer
Copy link
Contributor

bgfriend0 you just saved my day! I was looking for these annoying crashes for SO long - and it seems this was it! I now close the retrieved UIDocuments manually and am still crossing my fingers.
Thank you so much! :-)

@bgfriend0
Copy link
Contributor Author

Right on. :)

@Sam-Spencer
Copy link
Member

@bgfriend0 Could you confirm that this is still an issue on the latest commit of the feature branch?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

3 participants