Re: NSKeyedArchiver finishEncoding takes forever
Re: NSKeyedArchiver finishEncoding takes forever
- Subject: Re: NSKeyedArchiver finishEncoding takes forever
- From: "Adam R. Maxwell" <email@hidden>
- Date: Sat, 21 Jun 2008 18:17:49 -0700
On Jun 18, 2008, at 7:08 PM, Markus Spoettl wrote:
On Jun 18, 2008, at 5:04 PM, Adam R. Maxwell wrote:
It's not recommended, but have you tried using old-style archiving
as another approach? It used to be considerably faster than keyed
archiving under some circumstances.
I have thought about trying this until I read it's deprecated since
10.2. Also they don't play well with changes to the data structure,
both very good reasons to stay away.
Apple still requires NSArchiver for NSPortCoder (Distributed Objects),
so it can't be completely deprecated. I agree that it doesn't play
well with changes to the data structure, but you'd only have to
implement it for the object with doubles as ivars, and still use
NSKeyedArchiver for the rest (example follows).
Not that you should do this, now that you've found another solution; I
was mainly curious to see if you saw any performance benefit from it,
and too lazy to write a test case and profile it for myself :).
--
adam
#import <Foundation/Foundation.h>
@interface TestObject : NSObject <NSCoding>
{
double a;
double b;
}
@end
@implementation TestObject
- (id)init
{
static double x = 0;
self = [super init];
if (self) {
a = x++;
b = x++;
}
return self;
}
- (NSString *)description
{
return [NSString stringWithFormat:@"%@, a = %.3f, b = %.3f",
[super description], a, b];
}
- (id)initWithCoder:(NSCoder *)coder
{
self = [super init];
if (self) {
[coder decodeValueOfObjCType:@encode(double) at:&a];
[coder decodeValueOfObjCType:@encode(double) at:&b];
}
return self;
}
- (void)encodeWithCoder:(NSCoder *)coder
{
[coder encodeValueOfObjCType:@encode(double) at:&a];
[coder encodeValueOfObjCType:@encode(double) at:&b];
}
@end
@interface ParentObject: NSObject <NSCoding>
{
TestObject *child;
}
@end
@implementation ParentObject
- (id)init
{
self = [super init];
if (self) {
child = [TestObject new];
}
return self;
}
- (void)dealloc
{
[child release];
[super dealloc];
}
- (id)initWithCoder:(NSCoder *)coder
{
self = [super init];
if (self)
child = [[coder decodeObjectForKey:@"child"] retain];
return self;
}
- (void)encodeWithCoder:(NSCoder *)coder
{
[coder encodeObject:child forKey:@"child"];
}
- (NSString *)description
{
return [NSString stringWithFormat:@"%@: child = %@", [super
description], child];
}
@end
int main (int argc, char const *argv[])
{
NSAutoreleasePool *pool = [NSAutoreleasePool new];
NSMutableArray *a = [NSMutableArray array];
int i;
for (i = 0; i < 10; i++) {
ParentObject *obj = [ParentObject new];
[a addObject:obj];
[obj release];
}
NSLog(@"1: %@", a);
NSData *d = [NSKeyedArchiver archivedDataWithRootObject:a];
NSLog(@"archived to %d bytes", [d length]);
NSArray *b = [NSKeyedUnarchiver unarchiveObjectWithData:d];
NSLog(@"2: %@", b);
[pool release];
return 0;
}
_______________________________________________
Cocoa-dev mailing list (email@hidden)
Please do not post admin requests or moderator comments to the list.
Contact the moderators at cocoa-dev-admins(at)lists.apple.com
Help/Unsubscribe/Update your Subscription:
This email sent to email@hidden