【问题标题】:NSFileCoordinator error when using UIManagedDocument in iOS 5.0 simulator在 iOS 5.0 模拟器中使用 UIManagedDocument 时出现 NSFileCoordinator 错误
【发布时间】:2012-01-10 21:45:32
【问题描述】:

我在 iOS 5.0 中使用 UIManagedDocument,在模拟器上运行应用程序,在 OSX 10.6 下使用 XCode 4.2。有问题的代码如下所示:

if (![[NSFileManager defaultManager] fileExistsAtPath:[self.photoDatabase.fileURL path]]) {
    // does not exist on disk, so create it
    [self.photoDatabase saveToURL:self.photoDatabase.fileURL forSaveOperation:UIDocumentSaveForCreating completionHandler:^(BOOL success) {
        [self setupFetchedResultsController];
        [self fetchFlickrDataIntoDocument:self.photoDatabase];

    }];
} else if (self.photoDatabase.documentState == UIDocumentStateClosed) {
    // exists on disk, but we need to open it
    // *** the following line generates the message ***
    [self.photoDatabase openWithCompletionHandler:^(BOOL success) {
        //[self setupFetchedResultsController];
        }];
} else if (self.photoDatabase.documentState == UIDocumentStateNormal) {
    // already open and ready to use
    [self setupFetchedResultsController];
}

运行标记的行会在日志中创建以下消息:

2012-01-10 22:33:17.109 Photomania[5149:4803] NSFileCoordinator: A surprising server error was signaled. Details: Connection invalid

消息发送后,UIManagedDocument 可能工作也可能不工作——我还没有找到决定这一点的情况。

我很确定代码是正确的,因为它实际上是斯坦福 CS193p 课程中的代码示例之一。整个示例可以在他们的网站上下载 http://www.stanford.edu/class/cs193p/cgi-bin/drupal/ 直接链接到代码: http://www.stanford.edu/class/cs193p/cgi-bin/drupal/system/files/sample_code/Photomania_0.zip

此外,代码在设备本身上运行良好,不会产生“令人惊讶”的消息,并且运行之后的所有代码都很好。

我没有在 Google 上找到任何东西,在 Apple 开发者页面上也没有。重新启动模拟器或 XCode,或重新安装它们都不会改变行为。

有什么想法吗?

【问题讨论】:

  • 你有什么办法吗? cs193p 项目仅适用于 iPhone。你的是通用的吗?你认为这重要吗?
  • 我没有,而且它现在大部分时间都在工作,所以目前它不是高优先级。我自己的项目是通用的,但是,我在 cs193p 项目上看到了与从他们的网页下载的相同的项目。
  • 请注意,由于我已将所有项目升级到 iOS 5.0,因此我无法再对此进行测试,并且该错误不再发生。

标签: ios core-data ios-simulator uimanageddocument nsfilecoordinator


【解决方案1】:

我只能说这种情况发生在我身上好几次了。对我来说,在我更新我的 dataModel 之后我很懒惰,到目前为止,每次我收到这个错误都是因为我改变了我的数据模型。通常,我需要做的就是从模拟器中删除我的应用程序并重新运行它,结果总是很好。希望这对那里的人有所帮助。

【讨论】:

    【解决方案2】:

    我想我已经找到了答案。看起来 UIManagedDocument 的自动保存仅在模拟器上几秒钟后才会启动。

    所以我在模拟器上最小化了应用程序,按下主页按钮,然后单击图标再次最大化它。然后我在模拟器中终止了应用程序。

    当我重新启动应用程序时,数据库已加载。错误仍然出现 - 因为文档处于“关闭”状态(这是正常的 - 这就是 CS193P 要求调用 openWithCompletionHandler 的原因),但我在启动时的数据被保留了。不幸的是,我必须在终止应用程序之前执行最小化/最大化例程,否则更改将在下次启动时被丢弃。

    您能否确认这是您能够重现的行为?至少出于测试目的,这应该是一个足够好的技巧。

    【讨论】:

      【解决方案3】:

      尝试升级到最新的 iOS 5.1。我不认为带有 iCloud 的 UIManagedDocument 在 5.0 中工作可靠。这是我的经验。

      【讨论】:

        【解决方案4】:

        喜欢斯坦福 iTunes 课程。但是,我认为使用 UIManagedDocument 的示例代码是错误的。事实上,他在演示中指出,他这样做只是因为他想立即获取信息。在代码 cmets 中,他说不要使用自动保存功能,因为如果应用退出,数据将不会被保存。但是,UIManagedDocument 在退出前保存所有必要的内容。它具有用于退出/多任务处理/等的所有相关处理程序,以确保保存数据。

        所以,如果您使用该代码作为示例,这里有一个版本应该可以工作,并且不使用 saveToURL(我没有 flickr 帐户,所以我实际上并没有运行它 - 但这就是该课程旨在工作)。如果它不起作用,请告诉我。

        - (void)fetchFlickrDataIntoDocument:(UIManagedDocument *)document
        {
            NSManagedObjectContext *ctx = [[NSManagedObjectContext alloc] initWithConcurrencyType: NSPrivateQueueConcurrencyType];
            ctx.parentContext = document.managedObjectContext;
            [ctx performBlock:^{
                NSArray *photos = [FlickrFetcher recentGeoreferencedPhotos];
                for (NSDictionary *flickrInfo in photos) {
                    [Photo photoWithFlickrInfo:flickrInfo inManagedObjectContext:ctx];
                    // Push changes to document MOC
                    [ctx save:0]; // propagates changes to parent MOC
                    // and tell the document it is dirty and needs to be saved
                    // It will be saved when the document decides its time to save
                    // but it *will* be saved.
                    [document updateChangeCount:UIDocumentChangeDone]
                }
            }];
        }
        

        【讨论】:

          【解决方案5】:

          当文档文件 url 的最后一个路径组件是 @"Database" 时仍然有错误。添加扩展 @"Database.db" 似乎已修复它,现在一切正常。不过也升级到了 Lion。

          NSURL *url = [[[NSFileManager defaultManager] URLsForDirectory:NSDocumentDirectory inDomains:NSUserDomainMask] lastObject];
          url = [url URLByAppendingPathComponent:@"Database.db"];  
          

          【讨论】:

            猜你喜欢
            • 2016-05-22
            • 1970-01-01
            • 2012-06-23
            • 2011-12-23
            • 2021-12-22
            • 1970-01-01
            • 2013-09-30
            • 1970-01-01
            • 2021-09-05
            相关资源
            最近更新 更多