autosavesInPlace导致新文档保存失败 [英] autosavesInPlace causes New Document save to fail
问题描述
我有一个基于NSPersistentDocument的应用程序,当 autosavesInPlace
设置为返回 YES
时,它无法保存新文档,返回否
,问题消失。
I have a NSPersistentDocument based app which fails to save a new document when autosavesInPlace
is set to return YES
, return NO
and the problem disappears.
- 我创建一个新文档
- 进行一些更改
- 保存它,从而运行
NSSaveAsOperation
,文档名称和URL更改,并且一切似乎都很好,但是下一次保存将抛出非常具有描述性的
- I create a new document
- Make some changes
- Save it , thus running
NSSaveAsOperation
, the name of the document and the URL changes and all appears to be well but the next save will throw a very descriptive
NSPersistentStoreSaveError = 134030,/ /未分类的保存错误-我们所依赖的错误返回了错误
仅当文档尝试在<$ c之后运行保存时才会发生$ c> NSSaveAsOperation 。任何其他保存类型都可以像对现有文档所做的更改一样正常工作。有趣的效果是,如果我不更改名称或位置,我也不会遇到此问题。
This only happens when the document attempts to run a save after a NSSaveAsOperation
. Any other save type will work fine as in changes to existing doc. Interesting effect is that if i dont change name or location i dont get this issue either.
我正在获取
frame#0:0x00007fff988143c5 libobjc的异常回溯。 A.dylib
objc_exception_throw
-[NSPersistentStore(_NSInternalMethods)_preflightCrossCheck] + 697
框架#1:0x00007fff94c5f5f9 CoreData
框架#2:0x00007fff94c3198b CoreData $ c>-[NSPersistentStoreCoordinator executeRequest:withContext:error:] + 603
框架#3:0x00007fff94c5aa98 CoreData -[NSManagedObjectContext保存:] + 456
框架#4:0x00007fff91baa101 AppKit-[NSPersistentDocument writeToURL:ofType:forSaveOperation:originalContentsURL:error:] + 3743
-[ZZZZDocument writeToURL:ofType:forSaveOperation:originalContentsURL:错误:] + 135 at ZZZZDocument.m:209
帧#5:0x0000000100002de7 ZZZZ
帧#6:0x00007fff91baabc7 AppKit-[NSPersistentDocument writeSafelyToURL:ofType:forSaveOperation:error:] + 611
-[ZZZZDoc注释writeSafelyToURL:ofType:forSaveOperation:error:] + 115 at ZZZZDocument.m:223
帧#7:0x0000000100002ea3 ZZZZ
frame #0: 0x00007fff988143c5 libobjc.A.dylib
objc_exception_throw frame #1: 0x00007fff94c5f5f9 CoreData
-[NSPersistentStore(_NSInternalMethods) _preflightCrossCheck] + 697 frame #2: 0x00007fff94c3198b CoreData-[NSPersistentStoreCoordinator executeRequest:withContext:error:] + 603 frame #3: 0x00007fff94c5aa98 CoreData
-[NSManagedObjectContext save:] + 456 frame #4: 0x00007fff91baa101 AppKit-[NSPersistentDocument writeToURL:ofType:forSaveOperation:originalContentsURL:error:] + 3743 frame #5: 0x0000000100002de7 ZZZZ
-[ZZZZDocument writeToURL:ofType:forSaveOperation:originalContentsURL:error:] + 135 at ZZZZDocument.m:209 frame #6: 0x00007fff91baabc7 AppKit-[NSPersistentDocument writeSafelyToURL:ofType:forSaveOperation:error:] + 611 frame #7: 0x0000000100002ea3 ZZZZ
-[ZZZZDocument writeSafelyToURL:ofType:forSaveOperation:error:] + 115 at ZZZZDocument.m:223
有什么想法吗?
推荐答案
未包装的核心数据文件不可能
Its not possible for a un-wrappered core data file
捕获 NSSaveAsOperation
并在该持久性存储上进行迁移,以获取该枚举 ...- journal
文件的构造将无法在沙箱外部创建它。
In the event you attempt to trap NSSaveAsOperation
and do a migrate on the persistent store for that enum the construction of the ...-journal
file will fail to create as its outside the sandbox.
-(void)saveToURL:(NSURL *)url ofType:(NSString *)typeName forSaveOperation:(NSSaveOperationType)saveOperation completionHandler:(void (^)(NSError *))completionHandler
{
NSLog(@" save op = %ld to %@ ",saveOperation,url);
NSURL *targeturl = url;
if(saveOperation == NSSaveAsOperation)
{
//migrate pstore
NSPersistentStore *store = [self.managedObjectContext.persistentStoreCoordinator.persistentStores lastObject];
if (store)
{
NSMutableDictionary *opts = [NSMutableDictionary dictionary];
[opts setValue:[NSNumber numberWithBool:YES] forKey:NSInferMappingModelAutomaticallyOption];
[opts setValue:[NSNumber numberWithBool:YES] forKey:NSMigratePersistentStoresAutomaticallyOption];
NSError *error = NULL;
NSPersistentStore *newstore = [self.managedObjectContext.persistentStoreCoordinator migratePersistentStore:store toURL:url options:opts withType:store.type error:&error];
if(newstore == nil)
{
NSLog(@"migration error %@",[error localizedDescription]);
}
self.fileURL = url;
}
}
[super saveToURL:targeturl ofType:typeName forSaveOperation:saveOperation completionHandler:completionHandler];
}
因此我们需要将文件包装在非使用 NSPersistentDocument
框架很简单。
So we need to wrapper the file in a bundle/folder which is non-trivial using the NSPersistentDocument
framework.
在这里等待 NSManagedDocument
(那就是希望的API)
Heres waiting for NSManagedDocument
(thats a wishing well API)
这篇关于autosavesInPlace导致新文档保存失败的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!