Skip to content

Commit

Permalink
merging
Browse files Browse the repository at this point in the history
  • Loading branch information
casademora committed Nov 19, 2010
2 parents 53624d3 + b255dc4 commit 1083ca4
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 15 deletions.
1 change: 1 addition & 0 deletions ActiveRecordHelpers.h
Expand Up @@ -6,6 +6,7 @@
//

#import <Foundation/Foundation.h>
#import <CoreData/CoreData.h>

// enable to use caches for the fetchedResultsControllers (iOS only)
// #define STORE_USE_CACHE
Expand Down
53 changes: 38 additions & 15 deletions NSManagedObjectContext+ActiveRecord.m
Expand Up @@ -36,7 +36,7 @@ + (void) resetDefaultContext
{
dispatch_sync(dispatch_get_main_queue(), ^{
[[NSManagedObjectContext defaultContext] reset];
});
});
}

+ (NSManagedObjectContext *) contextForCurrentThread
Expand All @@ -61,38 +61,51 @@ + (NSManagedObjectContext *) contextForCurrentThread

- (void) observeContext:(NSManagedObjectContext *)otherContext
{
[[NSNotificationCenter defaultCenter] addObserver:self
selector:@selector(mergeChangesFromNotification:)
[[NSNotificationCenter defaultCenter] addObserver:self
selector:@selector(mergeChangesFromNotification:)
name:NSManagedObjectContextDidSaveNotification
object:otherContext];
}

- (void) observeContextOnMainThread:(NSManagedObjectContext *)otherContext
{
// NSLog(@"Start Observing on Main Thread");
[[NSNotificationCenter defaultCenter] addObserver:self
selector:@selector(mergeChangesOnMainThread:)
[[NSNotificationCenter defaultCenter] addObserver:self
selector:@selector(mergeChangesOnMainThread:)
name:NSManagedObjectContextDidSaveNotification
object:otherContext];
object:otherContext];
}

- (void) stopObservingContext:(NSManagedObjectContext *)otherContext
{
// NSLog(@"Stop Observing Context");
[[NSNotificationCenter defaultCenter] removeObserver:self
name:NSManagedObjectContextDidSaveNotification
[[NSNotificationCenter defaultCenter] removeObserver:self
name:NSManagedObjectContextDidSaveNotification
object:otherContext];
}

- (void) mergeChangesFromNotification:(NSNotification *)notification
{
NSLog(@"Merging changes to context%@", [NSThread isMainThread] ? @" *** on Main Thread ***" : @"");
<<<<<<< HEAD

=======
// NSAssert([NSThread isMainThread], @"Not on main thread");

// for (id object in [self updatedObjects])
// {
// if ([[object changedValues] count] > 0)
// {
// [self refreshObject:object mergeChanges:NO];
// }
// }
>>>>>>> b255dc40ab349c785671fef14df144ba15f82c7f
[self mergeChangesFromContextDidSaveNotification:notification];
}

- (void) mergeChangesOnMainThread:(NSNotification *)notification
{
<<<<<<< HEAD
if ([NSThread isMainThread])
{
[self mergeChangesFromNotification:notification];
Expand All @@ -101,6 +114,16 @@ - (void) mergeChangesOnMainThread:(NSNotification *)notification
{
[self performSelectorOnMainThread:@selector(mergeChangesFromNotification:) withObject:notification waitUntilDone:YES];
}
=======
if ([NSThread isMainThread])
{
[self mergeChangesFromNotification:notification];
}
else
{
[self performSelectorOnMainThread:@selector(mergeChangesFromNotification:) withObject:notification waitUntilDone:YES];
}
>>>>>>> b255dc40ab349c785671fef14df144ba15f82c7f
}

- (BOOL) save
Expand All @@ -116,12 +139,12 @@ - (BOOL) save
}
@catch (NSException *exception)
{
NSLog(@"Problem saving: %@", [exception userInfo] ?: [exception reason]);
NSLog(@"Problem saving: %@", (id)[exception userInfo] ?: (id)[exception reason]);
}

[ActiveRecordHelpers handleErrors:error];

return saved && error == nil;
return saved && error == nil;
}

- (void) saveWrapper
Expand All @@ -137,7 +160,7 @@ - (BOOL) saveOnBackgroundThread

return YES;
}

- (BOOL) saveOnMainThread
{
@synchronized(self)
Expand All @@ -151,15 +174,15 @@ - (BOOL) saveOnMainThread
+ (NSManagedObjectContext *) contextWithStoreCoordinator:(NSPersistentStoreCoordinator *)coordinator
{
NSManagedObjectContext *context = nil;
if (coordinator != nil)
if (coordinator != nil)
{
context = [[NSManagedObjectContext alloc] init];
[context setPersistentStoreCoordinator:coordinator];
}
return [context autorelease];
return [context autorelease];
}

+ (NSManagedObjectContext *) context
+ (NSManagedObjectContext *) context
{
return [self contextWithStoreCoordinator:[NSPersistentStoreCoordinator defaultStoreCoordinator]];
}
Expand Down

0 comments on commit 1083ca4

Please sign in to comment.