Reputation: 787
Q: Is there any way to acquire unique identifier of NSDocument in Document-based™ application which is persistent during app restarts?
Note1: there definitely should be some kind of id which is not just fileURL, but something else because app works well with autosaved and even not yet saved documents.
Note2: for some reasons I do not want to generate and persist this id to document's content file.
Upvotes: 4
Views: 328
Reputation: 787
I ended up saving data into coder in a method
encodeRestorableStateWithCoder
during document closing and restoring that data in restoreDocumentWindowWithIdentifier
when document is re-opened.
- (void) encodeRestorableStateWithCoder: (NSCoder *) coder {
[super encodeRestorableStateWithCoder: coder];
[coder encodeObject: @{@"color":@"red"} forKey: @"OBJECT_KEY"];
}
- (void) restoreDocumentWindowWithIdentifier: (NSString *) identifier
state: (NSCoder *) state
completionHandler: (void (^) (NSWindow *, NSError *)) completionHandler {
NSDictionary * restoredInfo = [state decodeObjectForKey: @"OBJECT_KEY"];
[super restoreDocumentWindowWithIdentifier: identifier state: state completionHandler: completionHandler];
}
I think it is possible to save a generated UID there, but this storage might not be very reliable.
Upvotes: 0
Reputation: 16660
Even I did not try it, I think that extended attributes can do the job:
uuid_t uuid;
[[NSUUID new] getUUIDBytes:uuid];
int result = setxattr( path, "com.yourcompany.yourapp.yourattr", &uuid, sizeof(uuid), 0, 0);
Since extended attributes are nor part of the file itself neither a standard to all file systems, they can be lost, when the file is copied on storage with a file system that does not support such attributes.
Upvotes: 3