From 0c60bcd4b8c2ca38555f9e7a3fd57cd7e9c0258a Mon Sep 17 00:00:00 2001 From: Saul Mora Date: Tue, 19 Apr 2011 17:41:08 -0600 Subject: [PATCH] Added inContext helper to get a managed object in another context --- ActiveRecordHelpers.m | 2 +- NSManagedObject+ActiveRecord.h | 2 ++ NSManagedObject+ActiveRecord.m | 6 ++++++ NSPersistentStore+ActiveRecord.h | 2 +- NSPersistentStore+ActiveRecord.m | 2 +- NSPersistentStoreCoordinator+ActiveRecord.m | 4 ++-- 6 files changed, 13 insertions(+), 5 deletions(-) diff --git a/ActiveRecordHelpers.m b/ActiveRecordHelpers.m index 28744cb..d2115df 100644 --- a/ActiveRecordHelpers.m +++ b/ActiveRecordHelpers.m @@ -19,7 +19,7 @@ [NSManagedObjectContext setDefaultContext:nil]; [NSManagedObjectModel setDefaultManagedObjectModel:nil]; [NSPersistentStoreCoordinator setDefaultStoreCoordinator:nil]; - [NSPersistentStore setDetaultPersistentStore:nil]; + [NSPersistentStore setDefaultPersistentStore:nil]; } + (void) handleErrors:(NSError *)error diff --git a/NSManagedObject+ActiveRecord.h b/NSManagedObject+ActiveRecord.h index 69309ae..fb93106 100644 --- a/NSManagedObject+ActiveRecord.h +++ b/NSManagedObject+ActiveRecord.h @@ -90,6 +90,8 @@ + (NSArray *)findByAttribute:(NSString *)attribute withValue:(id)searchValue andOrderBy:(NSString *)sortTerm ascending:(BOOL)ascending; + (NSArray *)findByAttribute:(NSString *)attribute withValue:(id)searchValue andOrderBy:(NSString *)sortTerm ascending:(BOOL)ascending inContext:(NSManagedObjectContext *)context; +- (id) inContext:(NSManagedObjectContext *)otherContext; + #if TARGET_OS_IPHONE + (NSFetchedResultsController *) fetchAllSortedBy:(NSString *)sortTerm ascending:(BOOL)ascending withPredicate:(NSPredicate *)searchTerm groupBy:(NSString *)groupingKeyPath; diff --git a/NSManagedObject+ActiveRecord.m b/NSManagedObject+ActiveRecord.m index 2c4c974..8f68671 100644 --- a/NSManagedObject+ActiveRecord.m +++ b/NSManagedObject+ActiveRecord.m @@ -644,6 +644,12 @@ static NSUInteger defaultBatchSize = kActiveRecordDefaultBatchSize; return YES; } +- (id) inContext:(NSManagedObjectContext *)otherContext; +{ + NSManagedObjectID *objectID = [self objectID]; + return [otherContext objectWithID:objectID]; +} + - (NSNumber *)maxValueFor:(NSString *)property { NSManagedObject *obj = [[self class] findFirstByAttribute:property diff --git a/NSPersistentStore+ActiveRecord.h b/NSPersistentStore+ActiveRecord.h index f65c97c..aa0f994 100644 --- a/NSPersistentStore+ActiveRecord.h +++ b/NSPersistentStore+ActiveRecord.h @@ -14,7 +14,7 @@ + (NSURL *) defaultLocalStoreUrl; + (NSPersistentStore *) defaultPersistentStore; -+ (void) setDetaultPersistentStore:(NSPersistentStore *) store; ++ (void) setDefaultPersistentStore:(NSPersistentStore *) store; + (NSURL *) urlForStoreName:(NSString *)storeFileName; diff --git a/NSPersistentStore+ActiveRecord.m b/NSPersistentStore+ActiveRecord.m index 40d1f1e..e11ae85 100644 --- a/NSPersistentStore+ActiveRecord.m +++ b/NSPersistentStore+ActiveRecord.m @@ -16,7 +16,7 @@ static NSPersistentStore *defaultPersistentStore = nil; return defaultPersistentStore; } -+ (void) setDetaultPersistentStore:(NSPersistentStore *) store ++ (void) setDefaultPersistentStore:(NSPersistentStore *) store { [defaultPersistentStore release]; defaultPersistentStore = [store retain]; diff --git a/NSPersistentStoreCoordinator+ActiveRecord.m b/NSPersistentStoreCoordinator+ActiveRecord.m index e819aab..b9a1e48 100644 --- a/NSPersistentStoreCoordinator+ActiveRecord.m +++ b/NSPersistentStoreCoordinator+ActiveRecord.m @@ -44,7 +44,7 @@ static NSPersistentStoreCoordinator *defaultCoordinator = nil; { [ActiveRecordHelpers handleErrors:error]; } - [NSPersistentStore setDetaultPersistentStore:store]; + [NSPersistentStore setDefaultPersistentStore:store]; } + (NSPersistentStoreCoordinator *) coordinatorWithPersitentStore:(NSPersistentStore *)persistentStore; @@ -102,7 +102,7 @@ static NSPersistentStoreCoordinator *defaultCoordinator = nil; NSManagedObjectModel *model = [NSManagedObjectModel defaultManagedObjectModel]; NSPersistentStoreCoordinator *psc = [[NSPersistentStoreCoordinator alloc] initWithManagedObjectModel:model]; - [NSPersistentStore setDetaultPersistentStore:[psc addInMemoryStore]]; + [NSPersistentStore setDefaultPersistentStore:[psc addInMemoryStore]]; return [psc autorelease]; }