Permalink
Browse files

Fix unit tests

Extract out some more categories on NSManagedObjectContext
  • Loading branch information...
1 parent 85fc403 commit c9c8d2473aedbe05502093455f3080b4f0325101 @casademora casademora committed Mar 9, 2012
View
@@ -18,3 +18,4 @@ UserInterfaceState.xcuserstate
/Magical Record.xcodeproj/project.xcworkspace/xcuserdata/saul.xcuserdatad/UserInterfaceState.xcuserstate
/Magical Record.xcodeproj/xcuserdata/saul.xcuserdatad/xcdebugger/Breakpoints.xcbkptlist
/Magical Record.xcodeproj/xcuserdata/gfurman.xcuserdatad
+Project Files/Magical Record.xcodeproj/xcuserdata
@@ -17,10 +17,6 @@
@implementation NSObject (MagicalRecord_DataImport)
//#warning If you implement valueForUndefinedKey: in any NSObject in your code, this may be the problem if something broke
-//TODO: This method needs to be:
-// 1) Renamed to MR_valueForUndefinedKey:
-// 2) swizzled in and out only when importing data.
-// This will be done in a really short update...stay tuned
- (id) MR_valueForUndefinedKey:(NSString *)key
{
return nil;
@@ -13,8 +13,12 @@ @implementation NSString (MagicalRecord_DataImport)
- (NSString *) MR_capitalizedFirstCharaterString;
{
- NSString *firstChar = [[self substringToIndex:1] capitalizedString];
- return [firstChar stringByAppendingString:[self substringFromIndex:1]];
+ if ([self length] > 0)
+ {
+ NSString *firstChar = [[self substringToIndex:1] capitalizedString];
+ return [firstChar stringByAppendingString:[self substringFromIndex:1]];
+ }
+ return self;
}
@end
@@ -368,34 +368,6 @@ + (NSArray *) MR_updateFromArray:(NSArray *)listOfObjectData inContext:(NSManage
return [self MR_findAllWithPredicate:[NSPredicate predicateWithFormat:@"self IN %@", objectIDs] inContext:context];
}
-
-+ (NSArray *) MR_updateFromArray:(NSArray *)listOfObjectData;
-{
- return [self MR_updateFromArray:listOfObjectData inContext:[NSManagedObjectContext MR_defaultContext]];
-}
-
-+ (NSArray *) MR_updateFromArray:(NSArray *)listOfObjectData inContext:(NSManagedObjectContext *)context;
-{
- NSMutableArray *objectIDs = [NSMutableArray array];
-
- [MRCoreDataAction saveDataWithBlock:^(NSManagedObjectContext *localContext)
- {
- [listOfObjectData enumerateObjectsUsingBlock:^(id obj, NSUInteger idx, BOOL *stop) {
-
- NSDictionary *objectData = (NSDictionary *)obj;
-
- NSManagedObject *dataObject = [self MR_updateFromObject:objectData inContext:localContext];
-
- if ([context obtainPermanentIDsForObjects:[NSArray arrayWithObject:dataObject] error:nil])
- {
- [objectIDs addObject:[dataObject objectID]];
- }
- }];
- }];
-
- return [self MR_findAllWithPredicate:[NSPredicate predicateWithFormat:@"self IN %@", objectIDs] inContext:context];
-}
-
@end
#pragma clang diagnostic pop
@@ -0,0 +1,20 @@
+//
+// NSManagedObjectContext+MagicalObserving.h
+// Magical Record
+//
+// Created by Saul Mora on 3/9/12.
+// Copyright (c) 2012 Magical Panda Software LLC. All rights reserved.
+//
+
+#import <CoreData/CoreData.h>
+
+@interface NSManagedObjectContext (MagicalObserving)
+
+- (void) MR_observeContext:(NSManagedObjectContext *)otherContext;
+- (void) MR_stopObservingContext:(NSManagedObjectContext *)otherContext;
+- (void) MR_observeContextOnMainThread:(NSManagedObjectContext *)otherContext;
+
+- (void) MR_observeiCloudChangesInCoordinator:(NSPersistentStoreCoordinator *)coordinator;
+- (void) MR_stopObservingiCloudChangesInCoordinator:(NSPersistentStoreCoordinator *)coordinator;
+
+@end
@@ -0,0 +1,99 @@
+//
+// NSManagedObjectContext+MagicalObserving.m
+// Magical Record
+//
+// Created by Saul Mora on 3/9/12.
+// Copyright (c) 2012 Magical Panda Software LLC. All rights reserved.
+//
+
+#import "NSManagedObjectContext+MagicalObserving.h"
+
+static void const * kMagicalRecordNotifiesMainContextAssociatedValueKey = @"kMagicalRecordNotifiesMainContextOnSave";
+NSString * const kMagicalRecordDidMergeChangesFromiCloudNotification = @"kMagicalRecordDidMergeChangesFromiCloudNotification";
+
+
+@implementation NSManagedObjectContext (MagicalObserving)
+
+#pragma mark - Context Observation Helpers
+
+- (void) MR_observeContext:(NSManagedObjectContext *)otherContext
+{
+ [[NSNotificationCenter defaultCenter] addObserver:self
+ selector:@selector(MR_mergeChangesFromNotification:)
+ name:NSManagedObjectContextDidSaveNotification
+ object:otherContext];
+}
+
+- (void) MR_observeContextOnMainThread:(NSManagedObjectContext *)otherContext
+{
+ [[NSNotificationCenter defaultCenter] addObserver:self
+ selector:@selector(MR_mergeChangesOnMainThread:)
+ name:NSManagedObjectContextDidSaveNotification
+ object:otherContext];
+}
+
+- (void) MR_stopObservingContext:(NSManagedObjectContext *)otherContext
+{
+ [[NSNotificationCenter defaultCenter] removeObserver:self
+ name:NSManagedObjectContextDidSaveNotification
+ object:otherContext];
+}
+
+#pragma mark - Context iCloud Merge Helpers
+
+- (void) MR_mergeChangesFromiCloud:(NSNotification *)notification;
+{
+ [self performBlock:^{
+
+ MRLog(@"Merging changes From iCloud %@context%@",
+ self == [NSManagedObjectContext MR_defaultContext] ? @"*** DEFAULT *** " : @"",
+ ([NSThread isMainThread] ? @" *** on Main Thread ***" : @""));
+
+ [self mergeChangesFromContextDidSaveNotification:notification];
+
+ [[NSNotificationCenter defaultCenter] postNotificationName:kMagicalRecordDidMergeChangesFromiCloudNotification
+ object:self
+ userInfo:[notification userInfo]];
+ }];
+}
+
+- (void) MR_mergeChangesFromNotification:(NSNotification *)notification;
+{
+ MRLog(@"Merging changes to %@context%@",
+ self == [NSManagedObjectContext MR_defaultContext] ? @"*** DEFAULT *** " : @"",
+ ([NSThread isMainThread] ? @" *** on Main Thread ***" : @""));
+
+ [self mergeChangesFromContextDidSaveNotification:notification];
+}
+
+- (void) MR_mergeChangesOnMainThread:(NSNotification *)notification;
+{
+ if ([NSThread isMainThread])
+ {
+ [self MR_mergeChangesFromNotification:notification];
+ }
+ else
+ {
+ [self performSelectorOnMainThread:@selector(MR_mergeChangesFromNotification:) withObject:notification waitUntilDone:YES];
+ }
+}
+
+- (void) MR_observeiCloudChangesInCoordinator:(NSPersistentStoreCoordinator *)coordinator;
+{
+ if (![MagicalRecord isICloudEnabled]) return;
+ [[NSNotificationCenter defaultCenter] addObserver:self
+ selector:@selector(MR_mergeChangesFromiCloud:)
+ name:NSPersistentStoreDidImportUbiquitousContentChangesNotification
+ object:coordinator];
+
+}
+
+- (void) MR_stopObservingiCloudChangesInCoordinator:(NSPersistentStoreCoordinator *)coordinator;
+{
+ if (![MagicalRecord isICloudEnabled]) return;
+ [[NSNotificationCenter defaultCenter] removeObserver:self
+ name:NSPersistentStoreDidImportUbiquitousContentChangesNotification
+ object:coordinator];
+}
+
+@end
@@ -11,33 +11,13 @@ extern NSString * const kMagicalRecordDidMergeChangesFromiCloudNotification;
@interface NSManagedObjectContext (MagicalRecord)
-+ (NSManagedObjectContext *) MR_context;
-+ (NSManagedObjectContext *) MR_contextForCurrentThread;
++ (void) MR_initializeDefaultContextWithCoordinator:(NSPersistentStoreCoordinator *)coordinator;
-+ (NSManagedObjectContext *) MR_contextThatNotifiesDefaultContextOnMainThread;
-+ (NSManagedObjectContext *) MR_contextThatNotifiesDefaultContextOnMainThreadWithCoordinator:(NSPersistentStoreCoordinator *)coordinator;
++ (NSManagedObjectContext *) MR_context;
+ (NSManagedObjectContext *) MR_contextWithStoreCoordinator:(NSPersistentStoreCoordinator *)coordinator;
-
-- (void) MR_observeContext:(NSManagedObjectContext *)otherContext;
-- (void) MR_stopObservingContext:(NSManagedObjectContext *)otherContext;
-- (void) MR_observeContextOnMainThread:(NSManagedObjectContext *)otherContext;
-
-- (void) MR_observeiCloudChangesInCoordinator:(NSPersistentStoreCoordinator *)coordinator;
-- (void) MR_stopObservingiCloudChangesInCoordinator:(NSPersistentStoreCoordinator *)coordinator;
-
-- (BOOL) MR_save;
-
-#ifdef NS_BLOCKS_AVAILABLE
-- (BOOL) MR_saveWithErrorHandler:(void (^)(NSError *))errorCallback;
-#endif
-
-- (BOOL) MR_saveOnMainThread;
-- (BOOL) MR_saveOnBackgroundThread;
-
+ (void) MR_resetDefaultContext;
+ (NSManagedObjectContext *) MR_defaultContext;
+ (void) MR_setDefaultContext:(NSManagedObjectContext *)moc;
-+ (void) MR_resetContextForCurrentThread;
@end
Oops, something went wrong.

0 comments on commit c9c8d24

Please sign in to comment.