【问题标题】:Spotlight plugin reading NSDocument causing "deny file-write-data" error in consoleSpotlight 插件读取 NSDocument 导致控制台中出现“拒绝文件写入数据”错误
【发布时间】:2015-07-13 21:44:14
【问题描述】:

我有一个基于沙盒 NSDocument 的应用程序,它带有一个 Spotlight 插件来索引我的自定义文档。

在测试期间,我注意到 Spotlight 插件在为文档编制索引时记录了很多错误:

5/4/15 3:11:18.765 PM sandboxd[432]: ([579]) mdworker(579) deny file-write-data 
  /Users/test/Desktop/test.document 
  (import fstype:hfs fsflag:480D000 flags:240000005E diag:0 isXCode:0
    uti:com.test.document 
  plugin:/TestApp.app/Contents/Library/Spotlight/TestApp Spotlight Importer.mdimporter - 
  find suspect file using: sudo mdutil -t 407144)

插件似乎试图写入它索引的文件(尽管它只有只读访问权限)。

在我的 Spotlight 插件实现中,我没有做任何特别的事情来写入文档。我所做的只是初始化我的 NSDocument 子类以从文档中读取:

[[TTCustomDocument alloc] initWithContentsOfURL:url ofType:contentType error:outError];

这是堆栈跟踪:

Thread 4:
0   libsystem_kernel.dylib          0x00007fff9015ee92 __mac_syscall + 10
1   libsystem_sandbox.dylib         0x00007fff910140b0 sandbox_check + 206
2   AppKit                          0x00007fff8f75fc38 -[NSDocument _autosavingPossibilityConcern] + 213
3   AppKit                          0x00007fff8f75fb02 -[NSDocument _checkAutosavingPossibilityAndReturnError:] + 60
4   AppKit                          0x00007fff8f75f9cf -[NSDocument _checkAutosavingAndReturnError:] + 26
5   AppKit                          0x00007fff8f75f97e -[NSDocument _checkAutosavingAndUpdateLockedState] + 26
6   AppKit                          0x00007fff8f75e420 -[NSDocument _initWithContentsOfURL:ofType:error:] + 319
7   AppKit                          0x00007fff8f75e056 -[NSDocument initWithContentsOfURL:ofType:error:] + 230

看起来自动保存检查正在尝试写入文档。

对此我能做些什么吗?是否有某种只读模式来打开 NSDocument?

更新:

复制:

  1. 新建一个 Xcode 项目:“Cocoa document based app”
  2. 添加 Spotlight 插件
  3. NSDocument 实现和 Spotlight 插件的代码在这里:https://gist.github.com/anonymous/c4929586dfa11a473673

【问题讨论】:

    标签: cocoa nsdocument appstore-sandbox spotlight spotlight-plugin


    【解决方案1】:

    好的,那么-[TTCustomDocument initWithContentsOfURL:ofType:error:] 的代码或任何其他自定义初始化代码以及您的-readFromURL:ofType:error 和相关代码在哪里?您很可能从这些(初始化...和/或读取...)方法之一触发了某些东西,导致文档被标记为脏(具有未保存的更改),从而导致触发自动保存系统。这是您 TTCustomDocument 类中的不当行为。

    我的猜测是您正在触发向文档的撤消管理器注册更改的东西。如果它是核心数据文档并且您正在设置一些初始数据,则很容易做到;如果您以调用您自己的撤消感知漏斗方法之一的方式设置初始数据,也很容易做到。最简单的解决方法(文档推荐)是在更改之前调用 [self.undoManager disableUndoRegistration]; 和 `[self.undoManager enableUndoRegistration];在更改之后和返回之前。

    【讨论】:

    • 感谢您对此进行调查。我没有覆盖initWithContentsOfURL:ofType:error:。在init 我设置了一些内部对象。我在阅读内容时使用[self.undoManager disableUndoRegistration]。查看堆栈跟踪,我认为它会立即检查文档是否可以自动保存。我覆盖 + (BOOL)autosavesInPlace 以返回 YES。
    • 我使用基于 Xcode 文档应用程序模板的示例应用程序复制了这一点。仅当autosavesInPlace 返回 YES 时才会出现问题。我将提交一份错误报告,看看他们在 Apple 对此的看法。
    • 什么内部对象?你如何设置它们?这些是关键细节。
    • 我在问题中添加了一个带有相关代码的要点。
    猜你喜欢
    • 1970-01-01
    • 2021-02-17
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多