eladb / Parse-NSCoding

iOS library for serializing Parse objects
MIT License
69 stars 25 forks source link

NSUnknownKeyException [<NSCache 0x10f305700> valueForUndefinedKey:] #6

Closed trymbill closed 10 years ago

trymbill commented 10 years ago

I'm trying to save and return a cached (NSCache) subclassed PFObject called Transfer. When storing it (to debug this) I'm trying to fetch it from the cache right after to no avail, like this:

Transfer *transfer = subclassedPFObject;
NSLog(@"Transfer: %@", transfer);
[self.cache setObject:transfer forKey:[NSString stringWithFormat:@"transfer_%@", [transfer objectId]]];
NSLog(@"Cached: %@", [self.cache valueForKey:[NSString stringWithFormat:@"transfer_%@", [transfer objectId]]]);

It saves the cache OK, and I can verify that PFObject+NSCoding is being used to serialize the object. But when I try to return it from the cache, I get this error:

*** Terminating app due to uncaught exception 'NSUnknownKeyException', reason: '[<NSCache 0x10f305700> valueForUndefinedKey:]: this class is not key value coding-compliant for the key transfer_eCEyjTqJUc.'

Here's how the object looks like when it gets saved to the cache:

Transfer: <Transfer:eCEyjTqJUc:(null)> {
    amount = 567;
    completedAt = "2014-03-27 06:15:21 +0000";
    from = "<PFUser:SoM3IdF0rUs3R>";
    isCharge = 0;
    isComplete = 1;
    likes =     (
        SoM3IdF0rUs3R
    );
    reason = Some text;
    to = "<PFUser:SoM3IdF0rUs3R>";
}

It doesn't seem to be firing (id)initWithCoder:(NSCoder*)aDecoder at all when fetching the object from the cache. It seems to be failing before it even reaches that method.

Am I missing something here? Any clues as to why it isn't able to fetch the object from the cache?

PS. Thanks for an awesome pod!

martinrybak commented 10 years ago

You may have already done this, but can you verify:

  1. That they key transfer_eCEyjTqJUc exists in the cache.
  2. That there is an object in the cache for that key.
  3. What is the type of the object in cache...NSStringFromClass([object class])
  4. That the object in the cache responds to NSCoding...[object respondsToSelector:@selector(initWithCoding:)]
trymbill commented 10 years ago

I may have dun goofed :) I was trying to fetch the object as a value, using valueForKey: instead of objectForKey:. I probably should've gone to sleep earlier yesterday :+1:

Confirming that this was my mistake, works fine when I try to fetch the object from the cache :)

martinrybak commented 10 years ago

It's happened to us all :)