From 189de3fb5eb91fe312c1cec90bf6fe1ae3b46955 Mon Sep 17 00:00:00 2001 From: Fran Date: Sun, 25 May 2014 12:22:48 +0200 Subject: [PATCH 1/5] Added two unit tests to check the thread where the block is executed --- Tests/Core/MagicalRecord+ActionsTests.m | 23 +++++++++++++++++++++++ 1 file changed, 23 insertions(+) diff --git a/Tests/Core/MagicalRecord+ActionsTests.m b/Tests/Core/MagicalRecord+ActionsTests.m index 719a515d2..44e8f7603 100644 --- a/Tests/Core/MagicalRecord+ActionsTests.m +++ b/Tests/Core/MagicalRecord+ActionsTests.m @@ -121,6 +121,15 @@ - (void)testSaveActionDoesNotAssignEntitiesWithoutAContextToAContext expect([objectId isTemporaryID]).to.beTruthy; } +- (void)testSynchronousSaveActionPerformedOnBackgroundQueue +{ + MagicalRecordStack *currentStack = self.stack; + + [currentStack saveWithBlockAndWait:^(NSManagedObjectContext *localContext) { + expect([NSThread currentThread]).toNot.equal([NSThread mainThread]); + }]; +} + #pragma mark - Asynchronous Saves - (void)testAsynchronousSaveActionSaves @@ -167,12 +176,14 @@ - (void)testAsynchronousSaveActionMakesInsertedEntitiesAvailableInTheDefaultCont NSManagedObject *inserted = [SingleEntityWithNoRelationships MR_createEntityInContext:localContext]; expect([inserted hasChanges]).to.beTruthy(); + expect([NSThread currentThread]).toNot.equal([NSThread mainThread]); [localContext obtainPermanentIDsForObjects:@[inserted] error:nil]; objectId = [inserted objectID]; } completion:^(BOOL success, NSError *error) { saveSuccessState = success; fetchedObject = [currentContext objectWithID:objectId]; + expect([NSThread currentThread]).to.equal([NSThread mainThread]); }]; expect(saveSuccessState).will.beTruthy(); @@ -215,4 +226,16 @@ - (void)testAsynchronousSaveActionMakesUpdatesToEntitiesAvailableToTheDefaultCon expect([fetchedObject valueForKey:kTestAttributeKey]).will.beFalsy(); } +- (void)testtAsynchronousSaveActionPerformedOnBackgroundQueue +{ + MagicalRecordStack *currentStack = self.stack; + + [currentStack saveWithBlock:^(NSManagedObjectContext *localContext) { + expect([NSThread currentThread]).toNot.equal([NSThread mainThread]); + + } completion:^(BOOL success, NSError *error) { + expect([NSThread currentThread]).to.equal([NSThread mainThread]); + }]; +} + @end From 7c6da352ba67c43d5da329786a66843453e69871 Mon Sep 17 00:00:00 2001 From: Fran Date: Sun, 25 May 2014 12:26:58 +0200 Subject: [PATCH 2/5] Removed unnecessary checks --- Tests/Core/MagicalRecord+ActionsTests.m | 2 -- 1 file changed, 2 deletions(-) diff --git a/Tests/Core/MagicalRecord+ActionsTests.m b/Tests/Core/MagicalRecord+ActionsTests.m index 44e8f7603..a66cc935c 100644 --- a/Tests/Core/MagicalRecord+ActionsTests.m +++ b/Tests/Core/MagicalRecord+ActionsTests.m @@ -176,14 +176,12 @@ - (void)testAsynchronousSaveActionMakesInsertedEntitiesAvailableInTheDefaultCont NSManagedObject *inserted = [SingleEntityWithNoRelationships MR_createEntityInContext:localContext]; expect([inserted hasChanges]).to.beTruthy(); - expect([NSThread currentThread]).toNot.equal([NSThread mainThread]); [localContext obtainPermanentIDsForObjects:@[inserted] error:nil]; objectId = [inserted objectID]; } completion:^(BOOL success, NSError *error) { saveSuccessState = success; fetchedObject = [currentContext objectWithID:objectId]; - expect([NSThread currentThread]).to.equal([NSThread mainThread]); }]; expect(saveSuccessState).will.beTruthy(); From b6019f471e12ccb057c0468df5428007b49f007c Mon Sep 17 00:00:00 2001 From: Fran Date: Sun, 25 May 2014 18:00:09 +0200 Subject: [PATCH 3/5] Removed wrong unit test --- Tests/Core/MagicalRecord+ActionsTests.m | 9 --------- 1 file changed, 9 deletions(-) diff --git a/Tests/Core/MagicalRecord+ActionsTests.m b/Tests/Core/MagicalRecord+ActionsTests.m index a66cc935c..6fdfefc4c 100644 --- a/Tests/Core/MagicalRecord+ActionsTests.m +++ b/Tests/Core/MagicalRecord+ActionsTests.m @@ -121,15 +121,6 @@ - (void)testSaveActionDoesNotAssignEntitiesWithoutAContextToAContext expect([objectId isTemporaryID]).to.beTruthy; } -- (void)testSynchronousSaveActionPerformedOnBackgroundQueue -{ - MagicalRecordStack *currentStack = self.stack; - - [currentStack saveWithBlockAndWait:^(NSManagedObjectContext *localContext) { - expect([NSThread currentThread]).toNot.equal([NSThread mainThread]); - }]; -} - #pragma mark - Asynchronous Saves - (void)testAsynchronousSaveActionSaves From 11fce5f525ffbc8f4204c71b0800dadb72d646ab Mon Sep 17 00:00:00 2001 From: Fran Date: Sun, 25 May 2014 18:49:08 +0200 Subject: [PATCH 4/5] Correction in saveWithBlock documentation for sync methods --- Library/MagicalRecordStack/MagicalRecordStack+Actions.h | 4 ++-- Tests/Core/MagicalRecord+ActionsTests.m | 2 +- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/Library/MagicalRecordStack/MagicalRecordStack+Actions.h b/Library/MagicalRecordStack/MagicalRecordStack+Actions.h index 6469e1451..cfc4fb967 100644 --- a/Library/MagicalRecordStack/MagicalRecordStack+Actions.h +++ b/Library/MagicalRecordStack/MagicalRecordStack+Actions.h @@ -26,7 +26,7 @@ dispatch_queue_t MR_saveQueue(void); /** * Synchronously saves the default managed object context (if there is one) and any parent contexts. * - * @param block Make changes to CoreData objects in this block using the passed in localContext. The block will be performed on a background queue, and once complete, the context will be saved. + * @param block Make changes to CoreData objects in this block using the passed in localContext. The block will not be performed on a background queue, and once complete, the context will be saved. * * @return Success state of the save operation */ @@ -37,7 +37,7 @@ dispatch_queue_t MR_saveQueue(void); * * Synchronously saves the default managed object context (if there is one) and any parent contexts. * - * @param block Make changes to CoreData objects in this block using the passed in localContext. The block will be performed on a background queue, and once complete, the context will be saved. + * @param block Make changes to CoreData objects in this block using the passed in localContext. The block will not be performed on a background queue, and once complete, the context will be saved. * @param error Pass in an NSError by reference to receive any errors encountered during the save. * * @return Whether the save was successful diff --git a/Tests/Core/MagicalRecord+ActionsTests.m b/Tests/Core/MagicalRecord+ActionsTests.m index 6fdfefc4c..f770fae34 100644 --- a/Tests/Core/MagicalRecord+ActionsTests.m +++ b/Tests/Core/MagicalRecord+ActionsTests.m @@ -215,7 +215,7 @@ - (void)testAsynchronousSaveActionMakesUpdatesToEntitiesAvailableToTheDefaultCon expect([fetchedObject valueForKey:kTestAttributeKey]).will.beFalsy(); } -- (void)testtAsynchronousSaveActionPerformedOnBackgroundQueue +- (void)testAsynchronousSaveActionPerformedOnBackgroundQueue { MagicalRecordStack *currentStack = self.stack; From 5c882f45b24fe40bd9e8f2d4ea78150d9f93d180 Mon Sep 17 00:00:00 2001 From: Fran Date: Sun, 25 May 2014 18:58:14 +0200 Subject: [PATCH 5/5] Improved description of the documentation for saveWitBlockAndWait --- Library/MagicalRecordStack/MagicalRecordStack+Actions.h | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/Library/MagicalRecordStack/MagicalRecordStack+Actions.h b/Library/MagicalRecordStack/MagicalRecordStack+Actions.h index cfc4fb967..79fe240fb 100644 --- a/Library/MagicalRecordStack/MagicalRecordStack+Actions.h +++ b/Library/MagicalRecordStack/MagicalRecordStack+Actions.h @@ -26,7 +26,7 @@ dispatch_queue_t MR_saveQueue(void); /** * Synchronously saves the default managed object context (if there is one) and any parent contexts. * - * @param block Make changes to CoreData objects in this block using the passed in localContext. The block will not be performed on a background queue, and once complete, the context will be saved. + * @param block Make changes to CoreData objects in this block using the passed in localContext. The block itself is not guaranteed to run on any particular background thread/queue, and once complete, the context will be saved. * * @return Success state of the save operation */ @@ -37,7 +37,7 @@ dispatch_queue_t MR_saveQueue(void); * * Synchronously saves the default managed object context (if there is one) and any parent contexts. * - * @param block Make changes to CoreData objects in this block using the passed in localContext. The block will not be performed on a background queue, and once complete, the context will be saved. + * @param block Make changes to CoreData objects in this block using the passed in localContext. The block itself is not guaranteed to run on any particular background thread/queue, the context will be saved. * @param error Pass in an NSError by reference to receive any errors encountered during the save. * * @return Whether the save was successful