Cocoa 使用NSPersistentDocument读取和写入元数据的理想位置?

Cocoa 使用NSPersistentDocument读取和写入元数据的理想位置?,cocoa,core-data,Cocoa,Core Data,在我的NSPersistentDocument子类中,我需要在保存文档时将一些元数据保存到文档中,当然,在打开文档时将其读回。我确实让它工作了,但我不确定这是否是正确的处理方式。(我的测试反映了一个新的、未保存的文档被保存,然后再次打开。新的、未保存的文档在第一次保存后才分配NSPersistentStore。) 以下是我的工作代码,以及与我的测试和如何处理此问题的思考过程相关的注释: - (BOOL)readFromURL:(NSURL *)absoluteURL ofType:(NSStri

在我的NSPersistentDocument子类中,我需要在保存文档时将一些元数据保存到文档中,当然,在打开文档时将其读回。我确实让它工作了,但我不确定这是否是正确的处理方式。(我的测试反映了一个新的、未保存的文档被保存,然后再次打开。新的、未保存的文档在第一次保存后才分配NSPersistentStore。)

以下是我的工作代码,以及与我的测试和如何处理此问题的思考过程相关的注释:

- (BOOL)readFromURL:(NSURL *)absoluteURL ofType:(NSString *)typeName error:(NSError **)outError
{

    // No persistent store available yet

    BOOL result = [super readFromURL:absoluteURL ofType:typeName error:outError];

    // Persistent store available, but metadata may be already altered by configurePersistentStoreCoordinatorForURL: 
    // if I were set the data there instead of in writeToURL:

    if (result == YES)
    {
        // Read metadata
        NSPersistentStore *persistentStore = [self.managedObjectContext.persistentStoreCoordinator persistentStoreForURL:absoluteURL];
        NSString * uuid = [persistentStore metadata][DefaultLocationUUIDKey];
        self.defaultLocationUUID = uuid;
    }

    return result;
}



- (BOOL)writeToURL:(NSURL *)absoluteURL ofType:(NSString *)typeName forSaveOperation:(NSSaveOperationType)saveOperation originalContentsURL:(NSURL *)absoluteOriginalContentsURL error:(NSError **)error
{

    // No persistent store available yet

    BOOL result = [super writeToURL:absoluteURL ofType:typeName forSaveOperation:saveOperation originalContentsURL:absoluteOriginalContentsURL error:error];

    // Persistent store available, but has already been saved by this point

    // Get mutable copy of existing metadata and update with new data
    NSPersistentStore *persistentStore = [self.managedObjectContext.persistentStoreCoordinator persistentStoreForURL:absoluteURL];
    NSMutableDictionary *metadata = [persistentStore.metadata mutableCopy];
    metadata[DefaultLocationUUIDKey] = self.defaultLocationUUID;

    // Use this method because it saves the metadata directly to the file without having to re-save the document, is this correct? It works.
    // Are their negative side effects to altering the metadata in this way while the file is open? I haven't tested all possibilities.
    NSError *metadataError = nil;
    [NSPersistentStoreCoordinator setMetadata:metadata forPersistentStoreOfType:typeName URL:absoluteURL error:&metadataError];

    return result;

}




// Called for both open and save, but not for new documents (which is fine, use initWithType:error:)
- (BOOL)configurePersistentStoreCoordinatorForURL:(NSURL *)url ofType:(NSString *)fileType modelConfiguration:(NSString *)configuration storeOptions:(NSDictionary *)storeOptions error:(NSError **)error
{

    // No persistent store available yet

    BOOL result = [super configurePersistentStoreCoordinatorForURL:url ofType:fileType modelConfiguration:configuration storeOptions:storeOptions error:error];

    // Persistent store available, but how to determine if opening existing or saving a new document?
    // Don't want to alter existing record unless saving a new document.

    return result;
}
在使用NSPersistentDocument时,有关如何最好地处理很多事情的文档似乎有点少,因此欢迎提供任何建议

谢谢