【问题标题】:UIManagedDocument OpenWithCompletionHandler never returnsUIManagedDocument OpenWithCompletionHandler 永远不会返回
【发布时间】:2012-09-08 12:06:41
【问题描述】:

我遇到了一个奇怪的问题。我确信我在代码中的其他地方对文件做了一些事情并且它没有正确关闭或其他什么,但现在它处于报告为已关闭的状态,但是当我调用 OpenWithCompletionHandler 它永远不会返回。见下文:

   //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
        }];
    }

有什么想法吗?

【问题讨论】:

  • 你有没有发现问题出在哪里?升级到 6.0 SDK 后,我无法让 openWithCompletionHandler 在 iOS 5.1 中工作。它永远不会回来。
  • 不。我已经有几周没有看到这个问题了,但我现在也在 iOS6 上。
  • 我遇到了同样的问题...使用 XCode 4.5.2 (4G2008a),使用 iOS Simulator 6.0 但使用 iOS 5.1 操作系统... openWithCompletionHandler 永远不会在关闭的数据库上触发。我有一个 HUD,它在完成触发时会降低,因此在模拟器中显然很痛苦,因为 HUD 一直在运行。

标签: ios uimanageddocument


【解决方案1】:

我遇到了同样的问题。

您是否尝试在 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;
    }
}

【讨论】:

    【解决方案2】:

    Bug in iPhone Simulator 5.1 with Xcode 4.5 using UIManagedDocument

    我的解决方案与报告的相同,但我必须将我的应用程序的部署目标降低到 iOS 5.0,以便“iPhone 5.0 模拟器”可用作运行目标。我只看到这个问题试图将 iPhone 5.1 模拟器与 XCode 4.5.2 一起使用,5.0 和 6.0 模拟器都可以工作。

    【讨论】:

      猜你喜欢
      • 2019-11-15
      • 2011-07-25
      • 1970-01-01
      • 2012-10-02
      • 2013-02-18
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多