Search code examples
objective-cxcodemacosarchivingnscoding

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, 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."

Solution

  • 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.