锁定单个 NSPersistentDocument
我的应用程序当前使用 CoreData 作为后端来存储到存储在 ~/Library/Application Support/MYAPP/MyDataFile.sqlite
中的单个 SQL 数据文件。我知道这是一种不寻常的情况,但是“锁定”此文件的最佳方法是什么,这样如果用户决定(出于任何愚蠢的原因)运行我的应用程序的第二个副本,Core Data 就不会惊慌失措?我应该使用一些老派的东西,比如在某个地方写一个锁文件并检查它,还是有更好的可可方法来做到这一点?
My application currently uses CoreData as a backend to store to a single SQL data file stored in ~/Library/Application Support/MYAPP/MyDataFile.sqlite
. I know it's an unusual situation, but what is the best way to "lock" this file so that if the user decides (for whatever silly reason) to run a second copy of my app, Core Data won't freak out? Should I use something old school like writing a lockfile somewhere and checking for that, or is there a nicer more Cocoa way of doing this?
作为锁定 SQLite 文件的替代方法,您可以尝试使用 LSMultipleInstancesProhibited 禁止您的用户运行两个应用程序实例。
编辑:缺点是它还会阻止多个用户(快速用户切换)同时使用您的应用程序,尽管他们不共享核心数据存储。
As an alternative to locking the SQLite file, you could try using LSMultipleInstancesProhibited to disallow your users running two application instances.
Edit: the downside is it will also prevent multiple users (fast user switching) from using your application concurrently, although they do not share the core data store.