Files
MagicalRecord/NSManagedObjectContext+ActiveRecord.m
Saul Mora 1083ca4119 merging
2010-11-19 10:21:51 -07:00

198 lines
5.0 KiB
Objective-C

//
// NSManagedObjectContext+ActiveRecord.m
//
// Created by Saul Mora on 11/23/09.
// Copyright 2010 Magical Panda Software, LLC All rights reserved.
//
#import "NSManagedObject+ActiveRecord.h"
#import "NSManagedObjectContext+ActiveRecord.h"
#import "NSPersistentStoreCoordinator+ActiveRecord.h"
static NSManagedObjectContext *defaultManageObjectContext = nil;
@implementation NSManagedObjectContext (ActiveRecord)
+ (NSManagedObjectContext *)defaultContext
{
// NSAssert([NSThread isMainThread], @"The defaultContext must only be accessed on the **Main Thread**");
@synchronized (self)
{
if (defaultManageObjectContext)
{
return defaultManageObjectContext;
}
}
return nil;
}
+ (void) setDefaultContext:(NSManagedObjectContext *)moc
{
[defaultManageObjectContext release];
defaultManageObjectContext = [moc retain];
}
+ (void) resetDefaultContext
{
dispatch_sync(dispatch_get_main_queue(), ^{
[[NSManagedObjectContext defaultContext] reset];
});
}
+ (NSManagedObjectContext *) contextForCurrentThread
{
if ( [NSThread isMainThread] )
{
return [self defaultContext];
}
else
{
NSMutableDictionary *threadDict = [[NSThread currentThread] threadDictionary];
NSManagedObjectContext *threadContext = [threadDict objectForKey:@"MO_Context"];
if ( threadContext == nil )
{
threadContext = [self context];
[threadDict setObject:threadContext forKey:@"MO_Context"];
//[threadContext release];
}
return threadContext;
}
}
- (void) observeContext:(NSManagedObjectContext *)otherContext
{
[[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:)
name:NSManagedObjectContextDidSaveNotification
object:otherContext];
}
- (void) stopObservingContext:(NSManagedObjectContext *)otherContext
{
// NSLog(@"Stop Observing Context");
[[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];
}
else
{
[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
{
NSError *error = nil;
BOOL saved = NO;
@try
{
NSLog(@"Saving %@Context%@",
self == [[self class] defaultContext] ? @" *** Default *** ": @"",
([NSThread isMainThread] ? @" *** on Main Thread ***" : @""));
saved = [self save:&error];
}
@catch (NSException *exception)
{
NSLog(@"Problem saving: %@", (id)[exception userInfo] ?: (id)[exception reason]);
}
[ActiveRecordHelpers handleErrors:error];
return saved && error == nil;
}
- (void) saveWrapper
{
NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
[self save];
[pool drain];
}
- (BOOL) saveOnBackgroundThread
{
[self performSelectorInBackground:@selector(saveWrapper) withObject:nil];
return YES;
}
- (BOOL) saveOnMainThread
{
@synchronized(self)
{
[self performSelectorOnMainThread:@selector(saveWrapper) withObject:nil waitUntilDone:YES];
}
return YES;
}
+ (NSManagedObjectContext *) contextWithStoreCoordinator:(NSPersistentStoreCoordinator *)coordinator
{
NSManagedObjectContext *context = nil;
if (coordinator != nil)
{
context = [[NSManagedObjectContext alloc] init];
[context setPersistentStoreCoordinator:coordinator];
}
return [context autorelease];
}
+ (NSManagedObjectContext *) context
{
return [self contextWithStoreCoordinator:[NSPersistentStoreCoordinator defaultStoreCoordinator]];
}
+ (NSManagedObjectContext *) contextThatNotifiesDefaultContextOnMainThread
{
NSManagedObjectContext *context = [self context];
[[self defaultContext] observeContextOnMainThread:context];
return context;
}
@end