autosavesInPlace causes New Document save to fail

无人久伴 提交于 2020-01-17 08:43:21

问题


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.

  • 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, // unclassified save error - something we depend on returned an error

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.

Im getting an exception backtrace of

frame #0: 0x00007fff988143c5 libobjc.A.dylibobjc_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

any ideas?


回答1:


Its not possible for a un-wrappered core data file

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

So we need to wrapper the file in a bundle/folder which is non-trivial using the NSPersistentDocument framework.

Heres waiting for NSManagedDocument (thats a wishing well API)



来源:https://stackoverflow.com/questions/14011042/autosavesinplace-causes-new-document-save-to-fail

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!