iOS Make sure documents are open before accessing

2019-03-06 04:24发布

When opening UIDocuments from a closed state, I understand that it is it done on another thread. My question is, if I have code on my main thread that will access the document (like performing a fetch request), how is it guaranteed that the document will be open before I try to access it if the opening of the closed document is done asynchronously?

2条回答
相关推荐>>
2楼-- · 2019-03-06 04:48

You should use documentState property of UIDocument class. For more detail refer this document documentState description.

It returns following enumerators to check:

enum {
    UIDocumentStateNormal          = 0,
    UIDocumentStateClosed          = 1 << 0,
    UIDocumentStateInConflict      = 1 << 1,
    UIDocumentStateSavingError     = 1 << 2,
    UIDocumentStateEditingDisabled = 1 << 3   }; typedef NSInteger UIDocumentState;

From Description: UIDocumentStateNormal is what you want, as:
The document is open, editing is enabled, and there are no conflicts or errors associated with it.

查看更多
在下西门庆
3楼-- · 2019-03-06 04:53

Instead of trying to block a thread, block your application's user from accessing the document until it is open. That's what the completion handler is for.

- (void)openWithCompletionHandler:(void (^)(BOOL success))completionHandler

When the completion handler is invoked simply dismiss whatever temporary user interface you display while the document is opening. This way no threads are blocked while the document is opening, and the application remains responsive (albeit useless).

查看更多
登录 后发表回答