3

I am running into a weird problem. I am sure that I did something to a file somewhere else in my code and it didn't close properly or something, but now it is in a state where it reports as being closed, but when I call OpenWithCompletionHandler it never returns. See below:

   //if the file is closed, open it and then set up the controller
    if (file.documentState == UIDocumentStateClosed){
        //---- this code executes        
        [file openWithCompletionHandler:^(BOOL success){
           // ---- this code NEVER executes
        }];
    }

Any ideas?

4

2 に答える 2

0

私は同じ問題を抱えていました。

viewDidLoad 内でドキュメントを開こうとしていますか?

コードを別のメソッドに移動してみてください。それは私にとって問題を解決しました。

ViewController.h で

@property (nonatomic,strong) NSManagedObjectContext *managedObjectContext;
@property (nonatomic,strong) UIManagedDocument *document;

ViewController.m で

@synthesize managedObjectContext = _managedObjectContext;
@synthesize document = _document;

- (void)viewDidLoad
{
    [super viewDidLoad];

    // Do not try to open the document here
    // Call another method instead :D
    if (!_managedObjectContext) {
        [self createContext];
    }
}

- (void)createContext
{
    NSFileManager *fileManager = [NSFileManager defaultManager];
    NSURL *url = [[fileManager URLsForDirectory:NSDocumentDirectory inDomains:NSUserDomainMask] lastObject];
    url = [url URLByAppendingPathComponent:@"Database"];

    self.document = [[UIManagedDocument alloc] initWithFileURL:url];

    // FILE DOES NOT EXIST - Let's create a new one    
    if (![[NSFileManager defaultManager] fileExistsAtPath:[url path]]) {
        [self.document saveToURL:url forSaveOperation:UIDocumentSaveForCreating completionHandler:^(BOOL success) {
            if (success) {
                self.managedObjectContext = self.document.managedObjectContext;
            } else {
                NSLog(@"ERROR: Cannot create new document");
            }
        }];

    // FILE IS CLOSED - Let's open it
    } else if (self.document.documentState == UIDocumentStateClosed) {
        [self.document openWithCompletionHandler:^(BOOL success) {
            if (success) {
                self.managedObjectContext = self.document.managedObjectContext;
            } else {
                NSLog(@"File is closed and it wont open!");
            }
        }];

    // FILE EXISTS AND IS OPENED - Yay!
    } else {
        self.managedObjectContext = self.document.managedObjectContext;
    }
}
于 2013-11-17T16:57:25.463 に答える
0

Bug in iPhone Simulator 5.1 with Xcode 4.5 using UIManagedDocument を参照してください。

私の解決策は報告されたものと同じですが、「iPhone 5.0 Simulator」が実行ターゲットとして利用できるように、アプリの展開ターゲットを iOS 5.0 に下げる必要がありました。この問題は、XCode 4.5.2 で iPhone 5.1 シミュレーターを使用しようとした場合にのみ発生しました。5.0 と 6.0 の両方のシミュレーターが動作します。

于 2013-01-13T20:47:57.517 に答える