I'm using CoreData
to manage my offline storage in my app. The data of the offline storage is saved in an custom NSObject
as a transformable in the xcdatamodel
.
My current version in the app (v1.0) is storing the Navigation
class.
I needed to rename the Navigation
class to fight some name space problems in another target of my code base.
In Version 2.0 the app is crashing because when I'm searching the CoreData
store the NSKeyedUnarchiver
is failing because of the missing Navigation
class.
What's the best approach to migrate my current CoreData
store to fight this issue?
I tried something like this:
NSFetchRequest *request = [NSFetchRequest fetchRequestWithEntityName:@"OfflineStorage"];
request.fetchLimit = 1;
request.predicate = [NSPredicate predicateWithFormat:@"storageID = 1"];
NSError *error;
NSArray *result;
@try {
result = [context executeFetchRequest:request error:&error];
}
@catch (NSException *exception) {
if([exception.name isEqualToString:NSInvalidUnarchiveOperationException]) {
NSFetchRequest *deleteRequest = [request copy];
deleteRequest.resultType = NSManagedObjectIDResultType;
deleteRequest.includesPropertyValues = NO;
deleteRequest.propertiesToFetch = @[];
NSArray *deleteResult = [context executeFetchRequest:request error:&error];
if(deleteResult.count) {
[context performBlockAndWait:^{
[context deleteObject:deleteResult.firstObject];
[context save:nil];
}];
}
}
}
I though I would be smart to catch the exception and try only to fetch the ObjectID to delete the corrupt data in my store. But it's not working…