mirror of
https://github.com/zhigang1992/MagicalRecord.git
synced 2026-01-12 17:32:18 +08:00
Resolved: Weird issue with assigning a Managed Object to another via relationship with two different instances of the same EntityDescription
This commit is contained in:
@@ -89,13 +89,14 @@ NSString * const kMagicalRecordImportRelationshipTypeKey = @"type";
|
||||
// NSDictionary *subentities = [originalDestinationEntity subentitiesByName];
|
||||
|
||||
NSEntityDescription *destinationEntity = originalDestinationEntity;
|
||||
NSDictionary *relationshipUserInfo = [relationshipInfo userInfo];
|
||||
NSString *mappedEntityName = [relationshipUserInfo valueForKey:kMagicalRecordImportRelationshipTypeKey];
|
||||
|
||||
if (mappedEntityName)
|
||||
{
|
||||
destinationEntity = [NSEntityDescription entityForName:mappedEntityName inManagedObjectContext:[self managedObjectContext]];
|
||||
}
|
||||
// NSDictionary *relationshipUserInfo = [relationshipInfo userInfo];
|
||||
// NSString *mappedEntityName = [relationshipUserInfo valueForKey:kMagicalRecordImportRelationshipTypeKey];
|
||||
//
|
||||
// if (mappedEntityName)
|
||||
// {
|
||||
// destinationEntity = [NSEntityDescription entityForName:mappedEntityName inManagedObjectContext:[self managedObjectContext]];
|
||||
// }
|
||||
// else if ([originalDestinationEntity isAbstract] && [subentities count])
|
||||
// {
|
||||
// }
|
||||
@@ -120,10 +121,15 @@ NSString * const kMagicalRecordImportRelationshipTypeKey = @"type";
|
||||
{
|
||||
id lookupValue = [singleRelatedObjectData valueForKey:lookupKey];
|
||||
|
||||
Class managedObjectClass = NSClassFromString([destinationEntity managedObjectClassName]);
|
||||
id existingObject = lookupValue ? [managedObjectClass findFirstByAttribute:primaryKeyName withValue:lookupValue inContext:[self managedObjectContext]] : nil;
|
||||
|
||||
return existingObject ?: [self MR_createInstanceForEntity:destinationEntity withDictionary:singleRelatedObjectData];
|
||||
if (lookupValue)
|
||||
{
|
||||
Class managedObjectClass = NSClassFromString([destinationEntity managedObjectClassName]);
|
||||
NSFetchRequest *request = [managedObjectClass requestFirstByAttribute:primaryKeyName withValue:lookupValue inContext:[self managedObjectContext]];
|
||||
[request setEntity:destinationEntity];
|
||||
|
||||
id existingObject = [managedObjectClass executeFetchRequestAndReturnFirstObject:request inContext:[self managedObjectContext]];
|
||||
return existingObject ?: [self MR_createInstanceForEntity:destinationEntity withDictionary:singleRelatedObjectData];
|
||||
}
|
||||
}
|
||||
return [self MR_createInstanceForEntity:destinationEntity withDictionary:singleRelatedObjectData];
|
||||
}
|
||||
@@ -131,8 +137,8 @@ NSString * const kMagicalRecordImportRelationshipTypeKey = @"type";
|
||||
- (void) MR_addObject:(NSManagedObject *)relatedObject forRelationship:(NSRelationshipDescription *)relationshipInfo
|
||||
{
|
||||
NSAssert2(relatedObject != nil, @"Cannot add nil to %@ for attribute %@", NSStringFromClass([self class]), [relationshipInfo name]);
|
||||
NSAssert([[relatedObject entity] isEqual:[relationshipInfo destinationEntity]], @"related object not defined with same entity as destination");
|
||||
|
||||
NSAssert2([relatedObject entity] == [relationshipInfo destinationEntity], @"related object entity %@ not same as destination entity %@", [relatedObject entity], [relationshipInfo destinationEntity]);
|
||||
|
||||
//add related object to set
|
||||
NSString *addRelationMessageFormat = [relationshipInfo isToMany] ? @"add%@Object:" : @"set%@:";
|
||||
NSString *addRelatedObjectToSetMessage = [NSString stringWithFormat:addRelationMessageFormat, attributeNameFromString([relationshipInfo name])];
|
||||
@@ -209,4 +215,9 @@ NSString * const kMagicalRecordImportRelationshipTypeKey = @"type";
|
||||
return [self MR_importFromDictionary:data inContext:[NSManagedObjectContext defaultContext]];
|
||||
}
|
||||
|
||||
+ (id) MR_updateFromDictionary:(NSDictionary *)data
|
||||
{
|
||||
return [self MR_importFromDictionary:data inContext:[NSManagedObjectContext defaultContext]];
|
||||
}
|
||||
|
||||
@end
|
||||
|
||||
@@ -16,6 +16,10 @@
|
||||
|
||||
+ (NSArray *) executeFetchRequest:(NSFetchRequest *)request;
|
||||
+ (NSArray *) executeFetchRequest:(NSFetchRequest *)request inContext:(NSManagedObjectContext *)context;
|
||||
+ (id) executeFetchRequestAndReturnFirstObject:(NSFetchRequest *)request;
|
||||
+ (id) executeFetchRequestAndReturnFirstObject:(NSFetchRequest *)request inContext:(NSManagedObjectContext *)context;
|
||||
|
||||
|
||||
+ (NSFetchRequest *)createFetchRequest;
|
||||
+ (NSFetchRequest *)createFetchRequestInContext:(NSManagedObjectContext *)context;
|
||||
+ (NSEntityDescription *)entityDescription;
|
||||
|
||||
@@ -23,7 +23,7 @@
|
||||
NSManagedObjectContext *context = [NSManagedObjectContext defaultContext];
|
||||
|
||||
MappedEntity *testMappedEntity = [MappedEntity createInContext:context];
|
||||
testMappedEntity.testMappedEntityIDValue = 42;
|
||||
testMappedEntity.mappedEntityIDValue = 42;
|
||||
testMappedEntity.sampleAttribute = @"This attribute created as part of the test case setup";
|
||||
|
||||
[context save];
|
||||
@@ -48,7 +48,9 @@
|
||||
|
||||
- (void) testImportMappedEntityViaToOneRelationship
|
||||
{
|
||||
id testRelatedEntity = testEntity.mappedEntity;
|
||||
|
||||
assertThat(testRelatedEntity, is(notNilValue()));
|
||||
}
|
||||
|
||||
@end
|
||||
|
||||
@@ -57,7 +57,7 @@
|
||||
assertThat([[testRelationship userInfo] valueForKey:kMagicalRecordImportRelationshipMapKey], is(equalTo(@"someRandomAttributeName")));
|
||||
|
||||
assertThat(testRelatedEntity, is(notNilValue()));
|
||||
assertThat([testRelatedEntity sampleAttribute], is(containsString(@"test case")));
|
||||
assertThat([testRelatedEntity sampleAttribute], is(containsString(@"sample json file")));
|
||||
}
|
||||
|
||||
- (void) testImportMappedEntityUsingPrimaryRelationshipKey
|
||||
@@ -69,9 +69,9 @@
|
||||
NSRelationshipDescription *testRelationship = [[mappedEntity propertiesByName] valueForKey:@"mappedEntity"];
|
||||
assertThat([[testRelationship userInfo] valueForKey:kMagicalRecordImportRelationshipPrimaryKey], is(equalTo(@"testMappedEntityID")));
|
||||
|
||||
// assertThat(testRelatedEntity, is(equalTo(testMappedEntity)));
|
||||
|
||||
assertThat([testRelatedEntity testMappedEntityID], is(equalToInteger(42)));
|
||||
assertThat([testRelatedEntity sampleAttribute], containsString(@"test case setup"));
|
||||
assertThat([testRelatedEntity sampleAttribute], containsString(@"sample json file"));
|
||||
}
|
||||
|
||||
@end
|
||||
|
||||
Reference in New Issue
Block a user