Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Implements saving changes made by the observed context #643

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,15 @@
- (void) MR_stopObservingContext:(NSManagedObjectContext *)otherContext;
- (void) MR_observeContextOnMainThread:(NSManagedObjectContext *)otherContext;

/*
Observes the 'NSManagedObjectContextDidSaveNotification' notification of the given context and perform a save action on the current managed context object.

If the current object it's MR's rootContext, it will persist the changes to the persistant store.

@param otherContext The MOC object that the current context should observe.
*/
- (void) MR_observeContextAndSaveSelf:(NSManagedObjectContext *)otherContext;

- (void) MR_observeiCloudChangesInCoordinator:(NSPersistentStoreCoordinator *)coordinator;
- (void) MR_stopObservingiCloudChangesInCoordinator:(NSPersistentStoreCoordinator *)coordinator;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@

#import "NSManagedObjectContext+MagicalObserving.h"
#import "NSManagedObjectContext+MagicalRecord.h"
#import "NSManagedObjectContext+MagicalSaves.h"
#import "MagicalRecord.h"
#import "MagicalRecord+iCloud.h"

Expand Down Expand Up @@ -46,6 +47,30 @@ - (void) MR_stopObservingContext:(NSManagedObjectContext *)otherContext
object:otherContext];
}


- (void) MR_observeContextAndSaveSelf:(NSManagedObjectContext *)otherContext
{
NSNotificationCenter *notificationCenter = [NSNotificationCenter defaultCenter];
[notificationCenter addObserver:self
selector:@selector(MR_mergeChangesFromNotificationAndSaveSelfOnly:)
name:NSManagedObjectContextDidSaveNotification
object:otherContext];
}

- (void) MR_mergeChangesFromNotificationAndSaveSelfOnly:(NSNotification *)notification
{
MRLog(@"Merging changes to %@context%@",
self == [NSManagedObjectContext MR_defaultContext] ? @"*** DEFAULT *** " : @"",
([NSThread isMainThread] ? @" *** on Main Thread ***" : @"Background Thread"));

[self mergeChangesFromContextDidSaveNotification:notification];

//If the observer it's the default context, let's merge the changes to its parent
//or save directly to the persistant store (for the case of MR's root context)
[self MR_saveOnlySelfAndWait];
}


#pragma mark - Context iCloud Merge Helpers

- (void) MR_mergeChangesFromiCloud:(NSNotification *)notification;
Expand Down
6 changes: 3 additions & 3 deletions MagicalRecord/MagicalRecordVersion.h
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
// Do not edit
#define MAGICAL_RECORD_DISPLAY_VERSION @"2.2develop"
#define MAGICAL_RECORD_VERSION 595
#define MAGICAL_RECORD_BUILD @"1a09221"
// Updated on Wed Jan 1 12:39:31 EST 2014
#define MAGICAL_RECORD_VERSION 608
#define MAGICAL_RECORD_BUILD @"eb72053"
// Updated on Wed Jan 8 17:51:28 CLST 2014
17 changes: 17 additions & 0 deletions Tests/Core/NSManagedObjectContextHelperTests.m
Original file line number Diff line number Diff line change
Expand Up @@ -48,5 +48,22 @@ - (void) testThatSavedObjectsHavePermanentIDs
XCTAssertFalse([[entity objectID] isTemporaryID], @"Entity should not have a temporary ID after saving");
}

- (void) testCanObserveContextAndSaveChanges
{
NSManagedObjectContext *context = [NSManagedObjectContext MR_contextWithStoreCoordinator:[NSPersistentStoreCoordinator MR_defaultStoreCoordinator]];

NSManagedObject *testEntity = [SingleEntityWithNoRelationships MR_createInContext:context];

XCTAssertTrue([context hasChanges], @"The context should have the test entity marked as changes.");
XCTAssertFalse([testEntity isDeleted], @"The test entity shouldn't be deleted.");

[[NSManagedObjectContext MR_defaultContext] MR_observeContextAndSaveSelf:context];

[context MR_saveOnlySelfAndWait];

XCTAssertFalse([context hasChanges], @"The local context shouldn't have changes unsaved.");
XCTAssertFalse([[NSManagedObjectContext MR_defaultContext] hasChanges], @"The observer shouldn't have unsaved changes.");
}


@end