KISS

Keep It Simple Stupid

CoreData: "incomprehensible archive" while fetching data

| comments

Automated testing (unit/integration/functional testing) is a really great way to verify your app works as expected. Also, having confidence that your refactoring won’t break anything is totally cool too. I’ve been doing some tests with CoreData entities and stumbled on a weird exception when simply fetching them.

Sample code:

1
2
3
4
5
6
NSFetchRequest *request = [NSFetchRequest new];
request.entity = entity;
request.fetchLimit = 0;
NSError *error = nil;
XCTAssertNotNil([moc executeFetchRequest:request error:&error],
                @"Failed to fetch entities: %@", error);

Here’s the exception I got in the test:

1
2
3
4
5
AwesomeProject/AwesomeProjectTests/EntityTests.m:42:
error: -[EntityTests testEntityObjectsShouldBeFetched] :
(([moc executeFetchRequest:request error:&error]) != nil) failed:
throwing "*** -[NSKeyedUnarchiver initForReadingWithData:]: incomprehensible archive (0x5b, 0x63, 0x6f, 0x6f, 0x6c, 0x5d, 0x20, 0x21)" -
Failed to fetch entities: (null)

No error, just exception.

I couldn’t find an answer on the internets. After a while I setup a breakpoint on All Exceptions: Debug / Breakpoints / Create Exception Breakpoint. Seeing -[_NSKeyedUnarchiveFromDataTransformer transformedValue:] in the stack trace hinted me that one of the entities has a Transformable attribute with a certain class that wasn’t included in the test target. Adding it fixed the problem. Or better yet, create a mock class that simply returns the input value to minimize the application logic in the tests.

Comments