Skip to content

Commit 663e392

Browse files
committed
Merge pull request #225 from pperepelkin/to-one-rels-fix
Fix to one relationship entity table join for entity type
2 parents 05268eb + 1b42699 commit 663e392

File tree

7 files changed

+33
-9
lines changed

7 files changed

+33
-9
lines changed

Incremental Store/EncryptedStore.m

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -414,6 +414,7 @@ - (NSIncrementalStoreNode *)newValuesForObjectWithID:(NSManagedObjectID *)object
414414

415415
// enumerate properties
416416
NSDictionary *properties = [entity propertiesByName];
417+
__block NSUInteger tableAliasIndex = 0;
417418
[properties enumerateKeysAndObjectsUsingBlock:^(NSString *key, NSPropertyDescription *obj, BOOL *stop) {
418419
if ([obj isKindOfClass:[NSAttributeDescription class]]) {
419420
[columns addObject:[NSString stringWithFormat:@"%@.%@", table, key]];
@@ -434,18 +435,15 @@ - (NSIncrementalStoreNode *)newValuesForObjectWithID:(NSManagedObjectID *)object
434435
if ([self entityNeedsEntityTypeColumn:destinationEntity]) {
435436
// Get the destination table for the type look up
436437
NSString *destinationTable = [self tableNameForEntity:destinationEntity];
437-
NSString *destinationAlias = [NSString stringWithFormat:@"T%lul",(unsigned long)destinationEntity.hash];
438+
NSString *destinationAlias = [NSString stringWithFormat:@"t%lu", (unsigned long)tableAliasIndex];
439+
tableAliasIndex++;
438440

439441
// Add teh type column to the query
440442
NSString *typeColumn = [NSString stringWithFormat:@"%@.__entityType", destinationAlias];
441443
[columns addObject:typeColumn];
442444

443-
NSString *join = [NSString stringWithFormat:@" INNER JOIN %@ as %@ ON %@.__objectid=%@.%@", destinationTable, destinationAlias, destinationAlias, table, column];
444-
445-
// this part handles optional relationship
446-
if (relationship.optional) {
447-
join = [join stringByAppendingFormat:@" OR %@.%@ IS NULL", table, column];
448-
}
445+
// Use LEFT JOIN as a relationship can be optional
446+
NSString *join = [NSString stringWithFormat:@" LEFT JOIN %@ as %@ ON %@.__objectid=%@.%@", destinationTable, destinationAlias, destinationAlias, table, column];
449447

450448
[typeJoins addObject:join];
451449

exampleProjects/IncrementalStore/ClassModel.xcdatamodeld/ClassModel.xcdatamodel/contents

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
<relationship name="manyToManyInverse" optional="YES" toMany="YES" deletionRule="Cascade" destinationEntity="Root" inverseName="manyToMany" inverseEntity="Root" syncable="YES"/>
1414
<relationship name="oneToManyInverse" optional="YES" maxCount="1" deletionRule="Nullify" destinationEntity="Root" inverseName="oneToMany" inverseEntity="Root" syncable="YES"/>
1515
<relationship name="oneToOneInverse" optional="YES" maxCount="1" deletionRule="Nullify" destinationEntity="Root" inverseName="oneToOne" inverseEntity="Root" syncable="YES"/>
16+
<relationship name="oneToOneNilInverse" optional="YES" maxCount="1" deletionRule="Nullify" destinationEntity="Root" inverseName="oneToOneNil" inverseEntity="Root" syncable="YES"/>
1617
</entity>
1718
<entity name="Root" representedClassName="ISDRoot" syncable="YES">
1819
<attribute name="name" optional="YES" attributeType="String" syncable="YES"/>
@@ -21,13 +22,14 @@
2122
<relationship name="multipleOneToManyChildB" optional="YES" toMany="YES" deletionRule="Nullify" destinationEntity="ChildB" inverseName="multipleOneToMany" inverseEntity="ChildB" syncable="YES"/>
2223
<relationship name="oneToMany" optional="YES" toMany="YES" deletionRule="Cascade" destinationEntity="Parent" inverseName="oneToManyInverse" inverseEntity="Parent" syncable="YES"/>
2324
<relationship name="oneToOne" optional="YES" maxCount="1" deletionRule="Cascade" destinationEntity="Parent" inverseName="oneToOneInverse" inverseEntity="Parent" syncable="YES"/>
25+
<relationship name="oneToOneNil" optional="YES" maxCount="1" deletionRule="Nullify" destinationEntity="Parent" inverseName="oneToOneNilInverse" inverseEntity="Parent" syncable="YES"/>
2426
<relationship name="recursiveManyToMany" optional="YES" toMany="YES" deletionRule="Nullify" destinationEntity="Root" inverseName="recursiveManyToManyInverse" inverseEntity="Root" syncable="YES"/>
2527
<relationship name="recursiveManyToManyInverse" optional="YES" toMany="YES" deletionRule="Nullify" destinationEntity="Root" inverseName="recursiveManyToMany" inverseEntity="Root" syncable="YES"/>
2628
</entity>
2729
<elements>
2830
<element name="ChildA" positionX="-119" positionY="144" width="128" height="75"/>
2931
<element name="ChildB" positionX="79" positionY="144" width="128" height="75"/>
30-
<element name="Parent" positionX="-20" positionY="8" width="128" height="103"/>
31-
<element name="Root" positionX="-236" positionY="8" width="128" height="165"/>
32+
<element name="Parent" positionX="-20" positionY="8" width="128" height="120"/>
33+
<element name="Root" positionX="-236" positionY="8" width="128" height="180"/>
3234
</elements>
3335
</model>

exampleProjects/IncrementalStore/ClassModels/ISDParent.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
@property (nonatomic, retain) NSString * name;
1717
@property (nonatomic, retain) ISDRoot *oneToManyInverse;
1818
@property (nonatomic, retain) ISDRoot *oneToOneInverse;
19+
@property (nonatomic, retain) ISDRoot *oneToOneNilInverse;
1920
@property (nonatomic, retain) NSSet *manyToManyInverse;
2021
@end
2122

exampleProjects/IncrementalStore/ClassModels/ISDParent.m

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ @implementation ISDParent
1515
@dynamic name;
1616
@dynamic oneToManyInverse;
1717
@dynamic oneToOneInverse;
18+
@dynamic oneToOneNilInverse;
1819
@dynamic manyToManyInverse;
1920

2021
@end

exampleProjects/IncrementalStore/ClassModels/ISDRoot.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
@property (nonatomic, retain) NSString * name;
1717
@property (nonatomic, retain) NSSet *oneToMany;
1818
@property (nonatomic, retain) ISDParent *oneToOne;
19+
@property (nonatomic, retain) ISDParent *oneToOneNil;
1920
@property (nonatomic, retain) NSSet *manyToMany;
2021
@property (nonatomic, retain) NSSet *multipleOneToManyChildA;
2122
@property (nonatomic, retain) NSSet *multipleOneToManyChildB;

exampleProjects/IncrementalStore/ClassModels/ISDRoot.m

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ @implementation ISDRoot
1515
@dynamic name;
1616
@dynamic oneToMany;
1717
@dynamic oneToOne;
18+
@dynamic oneToOneNil;
1819
@dynamic manyToMany;
1920
@dynamic multipleOneToManyChildA;
2021
@dynamic multipleOneToManyChildB;

exampleProjects/IncrementalStore/Tests/RelationTests.m

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -309,6 +309,20 @@ -(void)testFetchingOneToOneFromDatabase
309309
[self checkOneToOneWithChildA:YES childB:NO];
310310
}
311311

312+
-(void)testFetchingOneToOneNilFromCache
313+
{
314+
[self checkOneToOneNil];
315+
}
316+
317+
-(void)testFetchingOneToOneNilFromDatabase
318+
{
319+
// Make sure we're loading directly from DB
320+
[self resetCoordinator];
321+
[self createCoordinator];
322+
323+
[self checkOneToOneNil];
324+
}
325+
312326
-(void)testFetchingManyToManyFromCache
313327
{
314328
[self checkManyToManyWithChildACount:2 childBCount:3];
@@ -381,6 +395,12 @@ -(void)checkOneToOneWithChildA:(BOOL)childA childB:(BOOL)childB
381395
XCTAssertFalse([child isKindOfClass:[ISDChildA class]], @"One-to-one child is of wrong class, got: %@, expecting: %@", NSStringFromClass([child class]), NSStringFromClass([ISDChildB class]));
382396
}
383397
}
398+
399+
-(void)checkOneToOneNil
400+
{
401+
ISDRoot *fetchedRoot = [self fetchRootObject];
402+
XCTAssert(fetchedRoot.oneToOneNil == nil, @"We didn't set it, should be nil");
403+
}
384404

385405
/// Checks that the root object has the correct number of many-to-many relational ChildA and ChildB objects
386406
-(void)checkManyToManyWithChildACount:(NSUInteger)childACount childBCount:(NSUInteger)childBCount

0 commit comments

Comments
 (0)