in AWSCore/Mantle/AWSMTLManagedObjectAdapter.m [238:349]
NSLocalizedDescriptionKey: NSLocalizedString(@"Could not deserialize managed object", @""),
NSLocalizedFailureReasonErrorKey: failureReason,
};
*error = [NSError errorWithDomain:AWSMTLManagedObjectAdapterErrorDomain code:AWSMTLManagedObjectAdapterErrorUnsupportedManagedObjectPropertyType userInfo:userInfo];
}
return NO;
}
};
if (!deserializeProperty(managedObjectProperties[managedObjectKey])) return nil;
}
return model;
}
+ (id)modelOfClass:(Class)modelClass fromManagedObject:(NSManagedObject *)managedObject error:(NSError **)error {
NSSet *propertyKeys = [modelClass propertyKeys];
for (NSString *mappedPropertyKey in [modelClass managedObjectKeysByPropertyKey]) {
if ([propertyKeys containsObject:mappedPropertyKey]) continue;
if (error != NULL) {
NSDictionary *userInfo = @{
NSLocalizedDescriptionKey: NSLocalizedString(@"Invalid entity attribute mapping", nil),
NSLocalizedFailureReasonErrorKey: [NSString stringWithFormat:NSLocalizedString(@"%1$@ could not be parsed because its entity attribute mapping contains illegal property keys.", nil), modelClass]
};
*error = [NSError errorWithDomain:AWSMTLManagedObjectAdapterErrorDomain code:AWSMTLManagedObjectAdapterErrorInvalidManagedObjectMapping userInfo:userInfo];
}
return nil;
}
CFMutableDictionaryRef processedObjects = CFDictionaryCreateMutable(NULL, 0, &kCFTypeDictionaryKeyCallBacks, &kCFTypeDictionaryValueCallBacks);
if (processedObjects == NULL) return nil;
@onExit {
CFRelease(processedObjects);
};
return [self modelOfClass:modelClass fromManagedObject:managedObject processedObjects:processedObjects error:error];
}
+ (id)modelOfClass:(Class)modelClass fromManagedObject:(NSManagedObject *)managedObject processedObjects:(CFMutableDictionaryRef)processedObjects error:(NSError **)error {
NSParameterAssert(modelClass != nil);
NSParameterAssert(processedObjects != nil);
if (managedObject == nil) return nil;
const void *existingModel = CFDictionaryGetValue(processedObjects, (__bridge void *)managedObject);
if (existingModel != NULL) {
return (__bridge id)existingModel;
}
if ([modelClass respondsToSelector:@selector(classForDeserializingManagedObject:)]) {
modelClass = [modelClass classForDeserializingManagedObject:managedObject];
if (modelClass == nil) {
if (error != NULL) {
NSDictionary *userInfo = @{
NSLocalizedDescriptionKey: NSLocalizedString(@"Could not deserialize managed object", @""),
NSLocalizedFailureReasonErrorKey: NSLocalizedString(@"No model class could be found to deserialize the object.", @"")
};
*error = [NSError errorWithDomain:AWSMTLManagedObjectAdapterErrorDomain code:AWSMTLManagedObjectAdapterErrorNoClassFound userInfo:userInfo];
}
return nil;
}
}
AWSMTLManagedObjectAdapter *adapter = [[self alloc] initWithModelClass:modelClass];
return [adapter modelFromManagedObject:managedObject processedObjects:processedObjects error:error];
}
- (id)managedObjectFromModel:(AWSMTLModel<AWSMTLManagedObjectSerializing> *)model insertingIntoContext:(NSManagedObjectContext *)context processedObjects:(CFMutableDictionaryRef)processedObjects error:(NSError * __autoreleasing *)error {
NSParameterAssert(model != nil);
NSParameterAssert(context != nil);
NSParameterAssert(processedObjects != nil);
NSString *entityName = [model.class managedObjectEntityName];
NSAssert(entityName != nil, @"%@ returned a nil +managedObjectEntityName", model.class);
Class entityDescriptionClass = NSClassFromString(@"NSEntityDescription");
NSAssert(entityDescriptionClass != nil, @"CoreData.framework must be linked to use MTLManagedObjectAdapter");
Class fetchRequestClass = NSClassFromString(@"NSFetchRequest");
NSAssert(fetchRequestClass != nil, @"CoreData.framework must be linked to use MTLManagedObjectAdapter");
// If a uniquing predicate is provided, perform a fetch request to guarantee a unique managed object.
__block NSManagedObject *managedObject = nil;
NSPredicate *uniquingPredicate = [self uniquingPredicateForModel:model];
if (uniquingPredicate != nil) {
__block NSError *fetchRequestError = nil;
__block BOOL encountedError = NO;
managedObject = performInContext(context, ^ id {
NSFetchRequest *fetchRequest = [[fetchRequestClass alloc] init];
fetchRequest.entity = [entityDescriptionClass entityForName:entityName inManagedObjectContext:context];
fetchRequest.predicate = uniquingPredicate;
fetchRequest.returnsObjectsAsFaults = NO;
fetchRequest.fetchLimit = 1;
NSArray *results = [context executeFetchRequest:fetchRequest error:&fetchRequestError];
if (results == nil) {
encountedError = YES;
if (error != NULL) {
NSString *failureReason = [NSString stringWithFormat:NSLocalizedString(@"Failed to fetch a managed object for uniqing predicate \"%@\".", @""), uniquingPredicate];
NSDictionary *userInfo = @{