Question

I got the following error [context save:&error]. How should I fix?

unrecognized selector sent to class * -[NSKeyedArchiver dealloc]: warning: NSKeyedArchiver deallocated without having had -finishEncoding called on it. * Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '+[TempObject encodeWithCoder:]: unrecognized selector sent to class'

Here the code I'm using.

NSManagedObjectContext *context = [self.fetchedResultsController managedObjectContext];
PostRequest *postRequestObject = (PostRequest *)[NSEntityDescription   insertNewObjectForEntityForName:@"PostRequest" inManagedObjectContext:context];

DummyClass *request = OtherClassObject;
postRequestObject.request = request;
// request is attribute with type transformable
NSLog(@"requestpost data -> %@",postRequestObject.request);

NSError *error = nil;
if (![context save:&error]) {
    // Replace this implementation with code to handle the error appropriately.
    NSLog(@"Unresolved error %@, %@", error, [error userInfo]);
    abort();
}

#pragma mark - private method
- (NSFetchedResultsController *)fetchedResultsController
{
    if (_fetchedResultsController != nil)
    {
        return _fetchedResultsController;
    }

    NSFetchRequest *fetchRequest = [[[NSFetchRequest alloc] init] autorelease];
    // Edit the entity name as appropriate.
    NSEntityDescription *entity = [NSEntityDescription entityForName:@"PostRequest" inManagedObjectContext:self.managedObjectContext];
    [fetchRequest setEntity:entity];

    [fetchRequest setFetchBatchSize:80];

    // Edit the sort key as appropriate.
    NSSortDescriptor *sortDescriptor = [[NSSortDescriptor alloc] initWithKey:@"status" ascending:NO];
    NSArray *sortDescriptors = @[sortDescriptor];
    [sortDescriptor autorelease];

    [fetchRequest setSortDescriptors:sortDescriptors];

    NSFetchedResultsController *aFetchedResultsController = [[[NSFetchedResultsController alloc] initWithFetchRequest:fetchRequest managedObjectContext:self.managedObjectContext sectionNameKeyPath:nil cacheName:@"root"] autorelease];
    aFetchedResultsController.delegate = self;
    self.fetchedResultsController = aFetchedResultsController;

    NSError *error = nil;
    if (![self.fetchedResultsController performFetch:&error]) {
        NSLog(@"Unresolved error %@, %@", error, [error userInfo]);
        abort();
    }

    return _fetchedResultsController;
}
Was it helpful?

Solution

The error message says that you have a class named TempObject, that you're trying to call encodeWithCoder: on it, and that this class does not implement that method. That's a crashing bug, as you're seeing.

It seems that you have a transformable Core Data attribute somewhere, and that you're assigning an instance of TempObject to this attribute. I don't know where that is. Your code snippet mentions a transformable attribute, but you don't use TempObject in the code. I can't tell if this code is not actually the cause of the crash or if you changed the name by hand for some reason.

Anyway, when you have a transformable attribute, you must do one of two things:

  1. Make your class conform to NSCoding. You said in a comment that you did, but your error message says that you didn't-- or at least, that you didn't finish the job. NSCoding defines two methods, initWithCoder: and encodeWithCoder:, and your class must implement both of them.

  2. Create a custom NSValueTransformer subclass that can convert your object to/from NSData, and configure your Core Data model to use this. Apple provides a couple of really good (but simple) examples of how to do this.

Anything you assign to a transformable attribute must do one of these. Which one is up to you.

It seems that you're trying to do #1 but haven't finished the job. Make sure that your class conforms to NSCoding.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top