Archiving / Unarchiving results in initForReadingWithData incomprehensible archive
I've implemented an save on applicationWillTerminate
and load on applicationWillFinishLoading
.
There is a complete object tree, all implement the NSCoding
protocol and I've check the types I enter.
One of the classes also stores an NSMutableData
to the NSKeyedArchive
, which I suspect might break unarchiving occasionally. Weirdly enough, sometimes it works and sometimes it doesn't. I suspect some content in the NSMutableData
will break the archiving.
I use encodeObject on all objects开发者_StackOverflow, except for the bools and int where I use the correct corresponding method (encodeBool:forKey:
and encodeInt:forKey:
)
To be more clear: the code really does work, sometimes is it able to rebuild a rather complete object graph, just not all the time.
The error message I get is:
initForReadingWithData incomprehensible archive 0x62, 0x70, 0x6c, 0x69, 0x73, 0x74, 0x30, 0x30
Added: code which is failing, it an NSMutableData
of 10+ MB
- (void)encodeWithCoder:(NSCoder*)encoder {
[encoder encodeObject:self.encodedMessage forKey:@"EncodedMessage"]; //NSData
[encoder encodeObject:self.data forKey:@"Data"]; //NSMutableData (10+MB)
[encoder encodeObject:self.header forKey:@"Header"]; //NSString
[encoder encodeObject:self.fileName forKey:@"FileName"]; //NSString
[encoder encodeInt:self.dataStartIndex forKey:@"DataStartIndex"]; //int
[encoder encodeInt:self.dataEndIndex forKey:@"DataEndIndex"]; //int
}
- (id)initWithCoder:(NSCoder*)decoder {
if (self = [super init]) {
self.encodedMessage = [decoder decodeObjectForKey:@"EncodedMessage"]; //NSData
self.data = [decoder decodeObjectForKey:@"Data"]; //NSMutableData
self.header = [decoder decodeObjectForKey:@"Header"]; //NSString
self.fileName = [decoder decodeObjectForKey:@"FileName"]; //NSString
self.dataStartIndex = [decoder decodeIntForKey:@"DataStartIndex"]; //int
self.dataEndIndex = [decoder decodeIntForKey:@"DataEndIndex"]; //int
}
return self;
}
When I remove the self.data encoding and decoding it always seem to work. It also fails with smaller sized self.data. Doesn't seem size but content issue?
Tried to open the file when I did write the nsmutabledata to it, the propertly list editor displays the error:
"Conversion of string failed. The string is empty."
plutil also gives this error:
"$ plutil -lint nzbvortex.state nzbvortex.state: Conversion of string failed. The string is empty."
FWIW I have also come across this problem and here is what I've found.
The bytes reported 0x62, 0x70, 0x6c, etc., are part of the magic string "bplist" at the start of a binary property list, which NSKeyedArchiver uses by default.
A binary property list stores metadata in a trailer (i.e. at the end of the data). So if it gets truncated, the entire plist becomes unreadable.
If you want to check whether that's what has happened to you, you can use NSPropertyListReader_binary1 from Cocotron (http://code.google.com/p/cocotron/source/browse/Foundation/NSPropertyList/) to see how the file format works.
Hope this helps someone!
It seems that store more than around 230000 bytes via an NSMutableArray will cause the NSKeyedArchiver to create a broken plist file.
220000 works, 250000 didn't. Did search for the exact amount that is allowed.
For the bool and int, there are two methods: encodeBool:forKey:
and encodeInt:forKey:
(taken from the NSKeyedArchiver reference).
For the NSMutableData, you should archive them with encodeObjectForKey:
and unarchive them with decodeObjectForKey:
.
You can refer to this useful guide for more cases.
精彩评论