Skip to content

Commit

Permalink
Merge pull request #91 from hyperoslo/fix/sqlite-mode-erros
Browse files Browse the repository at this point in the history
Fix: SQLite mode errors
  • Loading branch information
3lvis committed Apr 27, 2015
2 parents 8cd2803 + 9fadc50 commit 2f50e2d
Show file tree
Hide file tree
Showing 3 changed files with 50 additions and 37 deletions.
4 changes: 1 addition & 3 deletions Source/NSManagedObject+Sync.m
Original file line number Diff line number Diff line change
Expand Up @@ -109,10 +109,8 @@ - (void)sync_processToManyRelationship:(NSRelationshipDescription *)relationship
NSString *destinationRemoteKey = [entity sync_remoteKey];
NSArray *childIDs = [children valueForKey:destinationRemoteKey];
NSString *destinationLocalKey = [entity sync_localKey];
if (childIDs.count == 1) {
if (childIDs) {
childPredicate = [NSPredicate predicateWithFormat:@"%K = %@", destinationLocalKey, [[children valueForKey:destinationRemoteKey] firstObject]];
} else {
childPredicate = [NSPredicate predicateWithFormat:@"ANY %K.%K = %@", relationshipName, destinationLocalKey, [children valueForKey:destinationRemoteKey]];
}
} else {
childPredicate = [NSPredicate predicateWithFormat:@"%K = %@", inverseEntityName, self];
Expand Down
4 changes: 2 additions & 2 deletions Source/Sync.m
Original file line number Diff line number Diff line change
Expand Up @@ -48,8 +48,8 @@ + (void)changes:(NSArray *)changes

NSError *error = nil;
NSManagedObject *safeParent = [parent sync_copyInContext:backgroundContext
error:&error];
NSPredicate *predicate = [NSPredicate predicateWithFormat:@"%K = %@", parent.entity.name, safeParent];
error:&error];
NSPredicate *predicate = [NSPredicate predicateWithFormat:@"%K = %@", [parent.entity.name lowercaseString], safeParent];

[self changes:changes
inEntityNamed:entityName
Expand Down
79 changes: 47 additions & 32 deletions Tests/Tests/Tests.m
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,25 @@ @implementation Tests

#pragma mark - Helpers

- (void)dropSQLiteFileForModelNamed:(NSString *)modelName {
NSURL *url = [[[NSFileManager defaultManager] URLsForDirectory:NSDocumentDirectory
inDomains:NSUserDomainMask] lastObject];
NSString *filePath = [NSString stringWithFormat:@"%@.sqlite", modelName];
NSURL *storeURL = [url URLByAppendingPathComponent:filePath];
NSFileManager *fileManager = [NSFileManager defaultManager];
NSError *error = nil;
if ([fileManager fileExistsAtPath:storeURL.path]) [fileManager removeItemAtURL:storeURL error:&error];

if (error) {
NSLog(@"error deleting sqlite file");
abort();
}
}

- (DATAStack *)dataStackWithModelName:(NSString *)modelName {
// Uncomment when using DATAStackSQLiteStoreType:
// [self dropSQLiteFileForModelNamed:modelName];

DATAStack *dataStack = [[DATAStack alloc] initWithModelName:modelName
bundle:[NSBundle bundleForClass:[self class]]
storeType:DATAStackInMemoryStoreType];
Expand Down Expand Up @@ -223,37 +241,34 @@ - (void)testObjectsForParent {
[backgroundContext save:&userError];
if (userError) NSLog(@"userError: %@", userError);

NSManagedObjectContext *mainContext = [dataStack mainContext];
[mainContext performBlockAndWait:^{
[dataStack persistWithCompletion:^{
NSFetchRequest *userRequest = [[NSFetchRequest alloc] initWithEntityName:@"User"];
userRequest.predicate = [NSPredicate predicateWithFormat:@"remoteID = %@", @6];
NSArray *users = [mainContext executeFetchRequest:userRequest error:nil];
if (users.count != 1) abort();

[Sync changes:objects
inEntityNamed:@"Note"
parent:[users firstObject]
dataStack:dataStack
completion:^(NSError *error) {
NSManagedObjectContext *mainContext = [dataStack mainContext];

NSError *userFetchError = nil;
NSFetchRequest *userRequest = [[NSFetchRequest alloc] initWithEntityName:@"User"];
userRequest.predicate = [NSPredicate predicateWithFormat:@"remoteID = %@", @6];
NSArray *users = [mainContext executeFetchRequest:userRequest error:&userFetchError];
if (userFetchError) NSLog(@"userFetchError: %@", userFetchError);
NSManagedObject *user = [users firstObject];
XCTAssertEqualObjects([user valueForKey:@"name"], @"Shawn Merrill");

NSError *notesError = nil;
NSFetchRequest *noteRequest = [[NSFetchRequest alloc] initWithEntityName:@"Note"];
noteRequest.predicate = [NSPredicate predicateWithFormat:@"user = %@", user];
NSInteger notesCount = [mainContext countForFetchRequest:noteRequest error:&notesError];
if (notesError) NSLog(@"notesError: %@", notesError);
XCTAssertEqual(notesCount, 5);
}];
}];
[dataStack persistWithCompletion:^{
NSFetchRequest *userRequest = [[NSFetchRequest alloc] initWithEntityName:@"User"];
userRequest.predicate = [NSPredicate predicateWithFormat:@"remoteID = %@", @6];
NSArray *users = [dataStack.mainContext executeFetchRequest:userRequest error:nil];
if (users.count != 1) abort();

[Sync changes:objects
inEntityNamed:@"Note"
parent:[users firstObject]
dataStack:dataStack
completion:^(NSError *error) {
NSManagedObjectContext *mainContext = [dataStack mainContext];

NSError *userFetchError = nil;
NSFetchRequest *userRequest = [[NSFetchRequest alloc] initWithEntityName:@"User"];
userRequest.predicate = [NSPredicate predicateWithFormat:@"remoteID = %@", @6];
NSArray *users = [mainContext executeFetchRequest:userRequest error:&userFetchError];
if (userFetchError) NSLog(@"userFetchError: %@", userFetchError);
NSManagedObject *user = [users firstObject];
XCTAssertEqualObjects([user valueForKey:@"name"], @"Shawn Merrill");

NSError *notesError = nil;
NSFetchRequest *noteRequest = [[NSFetchRequest alloc] initWithEntityName:@"Note"];
noteRequest.predicate = [NSPredicate predicateWithFormat:@"user = %@", user];
NSInteger notesCount = [mainContext countForFetchRequest:noteRequest error:&notesError];
if (notesError) NSLog(@"notesError: %@", notesError);
XCTAssertEqual(notesCount, 5);
}];
}];
}];
}
Expand Down Expand Up @@ -378,7 +393,7 @@ - (void)testCustomPrimaryKey {
NSFetchRequest *commentsRequest = [[NSFetchRequest alloc] initWithEntityName:@"Comment"];
NSInteger numberOfComments = [mainContext countForFetchRequest:commentsRequest error:&commentsError];
if (commentsError) NSLog(@"commentsError: %@", commentsError);
XCTAssertEqual(numberOfComments, 8);
XCTAssertEqual(numberOfComments, 7);

NSError *commentsFetchError = nil;
commentsRequest.predicate = [NSPredicate predicateWithFormat:@"body = %@", @"comment 1"];
Expand Down

0 comments on commit 2f50e2d

Please sign in to comment.