使用NSPersistentDocument读取和写入元数据的理想场所?

时间:2012-06-25 19:38:45

标签: cocoa core-data

在我的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时,如何最好地处理很多事情,文档似乎有点稀疏,所以欢迎任何建议。

感谢。

0 个答案:

没有答案