Make NSCoding implementation always encode properties to NSData, and de... #40
Make NSCoding implementation always encode properties to NSData, and de... #40
Conversation
…decode back into properties
id encodedObj = [element remoteDictionaryRepresentationWrapped:NO fromNesting:YES]; | ||
id encodedObj = [element dictionaryRepresentationWrapped:NO | ||
fromNesting:YES | ||
remoteOnly:YES]; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Weird... the colons are matching up in xCode, for me. Have you ever heard this happen before?
@@ -607,6 +615,7 @@ - (id) initWithCoder:(NSCoder *)aDecoder | |||
{ | |||
self.remoteID = [aDecoder decodeObjectForKey:@"remoteID"]; | |||
remoteAttributes = [aDecoder decodeObjectForKey:@"remoteAttributes"]; | |||
[self setPropertiesUsingRemoteDictionary:remoteAttributes]; | |||
self.remoteDestroyOnNesting = [aDecoder decodeBoolForKey:@"remoteDestroyOnNesting"]; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
My tabbing isn't like this here, either! Very confused..
Hm. I'm a bit unclear on the reasoning behind this PR. Could you clarify the issue and what exactly this does please? Why the need for a remote and non-remote dictionary representation? And by the way, Your idea to run |
The thing that caused me to run into this was I was trying to cache these objects as As for the |
Ah, I see now. In that case, that should certainly be in your subclass's implementation. This kind of thing can't just be the default behavior. (But rather than this sort of hacky-approach, which again, risks not having up-to-date information, I still recommend that you manually encode/decode each one of your subclass's properties, and also make sure you call Don't think I'll merge this one, thanks though. |
Also, make sure NSKeyedArchiver is right for your use-case. You're probably 1000x better off using CoreData, something I discovered far too late in the game. |
Just so we're on the same page, isn't this pulll request causing you to
|
Yes, that is correct. NSCoding is structured such that each subclass saves each of its own properties that are relevant, and then calls So, like I've said above, you need to go in your NSRRemoteObejct subclass and override the following methods: - (id) initWithCoder:(NSCoder *)aDecoder
{
if (self = [super initWithCoder:aDecoder]) //note super
{
// Restore your properties here
self.myProperty = [aDecoder decodeBoolForKey:@"myProperty"];
}
return self;
}
- (void) encodeWithCoder:(NSCoder *)aCoder
{
[super encodeWithCoder:aCoder]; //note super
// Archive all your properties here
[aCoder encodeObject:self.myProperty forKey:@"myProperty"];
} |
I see. I guess, to me, it seems useful to abstract the way properties are
|
Not just my opinion :) From the NSCoding protocol:
|
Sure. They aren't explicitly saying you can't abstract it out, however. Under this logic, each subclass should also archive their |
Not at all, how would that be unpredictable?
Each level of the hierarchy is responsible for the instance variables at its own level. How could NSRRemoteObject possibly know about the properties subclasses below it may have? Those objects may have custom ways of encoding different data types, that you can't just assume your superclass will handle properly. Think about this for not just NSRails, that has mass-property setters, but any hierarchy of objects. |
NSRemoteObject already taps into its subclasses properties in many
|
Cool, no problem. Yeah, NSRails is unique in that way, but I just want to stress that this is how NSCoding is done, and it would be pretty radical to break this really strong foundation-level architecture. If you have a billion properties and would rather not go through doing this, I understand that. There are other ways, like CoreData, or even extending the Autogen script to generate this code automatically for you might not be a bad idea. |
I've been looking into core data. Unfortunately I came into a project late
|
Mark Larsen sent you an invitation Twitter helps you stay connected with what's happening right now and with the people and organizations you care about.
https://twitter.com/i/61c75b6e-676c-44d0-813b-49aa4a257843 This message was sent by Twitter on behalf of Twitter users who entered your email address to invite you to Twitter. Need help? |
encodeWithCoder
would useremoteAttributes
, which, in some rare scenarios (manually changing properties on an object) would not be accurate.More importantly,
initWithCoder
would set theremoteAttributes
but not use them to set the properties of the object.I've included tests, and they should have good coverage!
Open to critique on general coding style or anything else!