Skip to content

Commit

Permalink
Add iCloud support
Browse files Browse the repository at this point in the history
  • Loading branch information
casademora committed Nov 29, 2011
1 parent a3074e9 commit 719ffad
Show file tree
Hide file tree
Showing 9 changed files with 228 additions and 78 deletions.
3 changes: 3 additions & 0 deletions Source/Categories/NSManagedObjectContext+MagicalRecord.h
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,14 @@

#import "MagicalRecordHelpers.h"

extern NSString * const kMagicalRecordDidMergeChangesFromiCloudNotification;

@interface NSManagedObjectContext (MagicalRecord)

- (void) MR_observeContext:(NSManagedObjectContext *)otherContext;
- (void) MR_stopObservingContext:(NSManagedObjectContext *)otherContext;
- (void) MR_observeContextOnMainThread:(NSManagedObjectContext *)otherContext;
- (void) MR_observeiCloudChangesInCoordinator:(NSPersistentStoreCoordinator *)coordinator;

- (BOOL) MR_save;

Expand Down
77 changes: 45 additions & 32 deletions Source/Categories/NSManagedObjectContext+MagicalRecord.m
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,12 @@

static NSManagedObjectContext *defaultManageObjectContext_ = nil;
static NSString const * kMagicalRecordManagedObjectContextKey = @"MagicalRecord_NSManagedObjectContextForThreadKey";
NSString * const kMagicalRecordDidMergeChangesFromiCloudNotification = @"kMagicalRecordDidMergeChangesFromiCloudNotification";

@interface NSManagedObjectContext ()
@interface NSManagedObjectContext (MagicalRecordPrivate)

- (void) mergeChangesFromNotification:(NSNotification *)notification;
- (void) mergeChangesOnMainThread:(NSNotification *)notification;
- (void) MR_mergeChangesFromNotification:(NSNotification *)notification;
- (void) MR_mergeChangesOnMainThread:(NSNotification *)notification;

@end

Expand Down Expand Up @@ -54,7 +55,7 @@ + (void)MR_resetContextForCurrentThread
- (void) MR_observeContext:(NSManagedObjectContext *)otherContext
{
[[NSNotificationCenter defaultCenter] addObserver:self
selector:@selector(mergeChangesFromNotification:)
selector:@selector(MR_mergeChangesFromNotification:)
name:NSManagedObjectContextDidSaveNotification
object:otherContext];
}
Expand All @@ -63,7 +64,7 @@ - (void) MR_observeContextOnMainThread:(NSManagedObjectContext *)otherContext
{
// MRLog(@"Start Observing on Main Thread");
[[NSNotificationCenter defaultCenter] addObserver:self
selector:@selector(mergeChangesOnMainThread:)
selector:@selector(MR_mergeChangesOnMainThread:)
name:NSManagedObjectContextDidSaveNotification
object:otherContext];
}
Expand All @@ -78,7 +79,29 @@ - (void) MR_stopObservingContext:(NSManagedObjectContext *)otherContext

#pragma mark - Merge Helpers

- (void) mergeChangesFromNotification:(NSNotification *)notification
- (void) MR_observeiCloudChangesInCoordinator:(NSPersistentStoreCoordinator *)coordinator;
{
[[NSNotificationCenter defaultCenter] addObserver:self
selector:@selector(MR_mergeChangesFromiCloud:)
name:NSPersistentStoreDidImportUbiquitousContentChangesNotification
object:coordinator];

}

- (void) MR_mergeChangesFromiCloud:(NSNotification *)notification;
{
[self performBlock:^{

MRLog(@"Merging Changes from iCloud");
[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 *** " : @"",
Expand All @@ -87,37 +110,27 @@ - (void) mergeChangesFromNotification:(NSNotification *)notification
[self mergeChangesFromContextDidSaveNotification:notification];
}

- (void) mergeChangesOnMainThread:(NSNotification *)notification
- (void) MR_mergeChangesOnMainThread:(NSNotification *)notification;
{
if ([NSThread isMainThread])
{
[self mergeChangesFromNotification:notification];
[self MR_mergeChangesFromNotification:notification];
}
else
{
[self performSelectorOnMainThread:@selector(mergeChangesFromNotification:) withObject:notification waitUntilDone:YES];
[self performSelectorOnMainThread:@selector(MR_mergeChangesFromNotification:) withObject:notification waitUntilDone:YES];
}
}

- (void) mergeChangesFromiCloud:(NSNotification *)notification
{
NSManagedObjectContext* defaultContext = [[self class] MR_defaultContext];

[defaultContext performBlock:^{

[defaultContext mergeChangesFromContextDidSaveNotification:notification];
}];
}

#pragma mark - Save Helpers

- (BOOL)MR_save
- (BOOL) MR_save;
{
return [self MR_saveWithErrorHandler:nil];
}

#ifdef NS_BLOCKS_AVAILABLE
- (BOOL) MR_saveWithErrorHandler:(void (^)(NSError *))errorCallback
- (BOOL) MR_saveWithErrorHandler:(void (^)(NSError *))errorCallback;
{
NSError *error = nil;
BOOL saved = NO;
Expand Down Expand Up @@ -152,7 +165,7 @@ - (BOOL) MR_saveWithErrorHandler:(void (^)(NSError *))errorCallback
}
#endif

- (void) saveWrapper
- (void) MR_saveWrapper;
{
#ifdef NS_AUTOMATED_REFCOUNT_UNAVAILABLE
@autoreleasepool
Expand All @@ -168,30 +181,30 @@ - (void) saveWrapper

#pragma mark - Threading Helpers

- (BOOL)MR_saveOnBackgroundThread
- (BOOL) MR_saveOnBackgroundThread;
{
[self performSelectorInBackground:@selector(saveWrapper) withObject:nil];
[self performSelectorInBackground:@selector(MR_saveWrapper) withObject:nil];

return YES;
}

- (BOOL) MR_saveOnMainThread
- (BOOL) MR_saveOnMainThread;
{
@synchronized(self)
{
[self performSelectorOnMainThread:@selector(saveWrapper) withObject:nil waitUntilDone:YES];
[self performSelectorOnMainThread:@selector(MR_saveWrapper) withObject:nil waitUntilDone:YES];
}

return YES;
}

- (BOOL) MR_notifiesMainContextOnSave
- (BOOL) MR_notifiesMainContextOnSave;
{
NSNumber *notifies = objc_getAssociatedObject(self, @"notifiesMainContext");
return notifies ? [notifies boolValue] : NO;
}

- (void) MR_setNotifiesMainContextOnSave:(BOOL)enabled
- (void) MR_setNotifiesMainContextOnSave:(BOOL)enabled;
{
NSManagedObjectContext *mainContext = [[self class] MR_defaultContext];
if (self != mainContext)
Expand All @@ -208,7 +221,7 @@ - (void) MR_setNotifiesMainContextOnSave:(BOOL)enabled

#pragma mark - Creation Helpers

+ (NSManagedObjectContext *) MR_contextForCurrentThread
+ (NSManagedObjectContext *) MR_contextForCurrentThread;
{
if ([NSThread isMainThread])
{
Expand All @@ -227,7 +240,7 @@ + (NSManagedObjectContext *) MR_contextForCurrentThread
}
}

+ (NSManagedObjectContext *) MR_contextWithStoreCoordinator:(NSPersistentStoreCoordinator *)coordinator
+ (NSManagedObjectContext *) MR_contextWithStoreCoordinator:(NSPersistentStoreCoordinator *)coordinator;
{
NSManagedObjectContext *context = nil;
if (coordinator != nil)
Expand All @@ -247,12 +260,12 @@ + (NSManagedObjectContext *) MR_contextThatNotifiesDefaultContextOnMainThreadWit
return context;
}

+ (NSManagedObjectContext *) MR_context
+ (NSManagedObjectContext *) MR_context;
{
return [self MR_contextWithStoreCoordinator:[NSPersistentStoreCoordinator MR_defaultStoreCoordinator]];
}

+ (NSManagedObjectContext *) MR_contextThatNotifiesDefaultContextOnMainThread
+ (NSManagedObjectContext *) MR_contextThatNotifiesDefaultContextOnMainThread;
{
NSManagedObjectContext *context = [self MR_context];
context.MR_notifiesMainContextOnSave = YES;
Expand Down
1 change: 1 addition & 0 deletions Source/Categories/NSPersistentStore+MagicalRecord.h
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ extern NSString * const kMagicalRecordDefaultStoreFileName;
+ (void) MR_setDefaultPersistentStore:(NSPersistentStore *) store;

+ (NSURL *) MR_urlForStoreName:(NSString *)storeFileName;
+ (NSURL *) MR_cloudURLForUbiqutiousContainer:(NSString *)bucketName;

@end

Expand Down
8 changes: 8 additions & 0 deletions Source/Categories/NSPersistentStore+MagicalRecord.m
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,14 @@ + (NSURL *) MR_urlForStoreName:(NSString *)storeFileName
return [NSURL fileURLWithPath:[[self MR_applicationStorageDirectory] stringByAppendingPathComponent:storeFileName]];
}

+ (NSURL *) MR_cloudURLForUbiqutiousContainer:(NSString *)bucketName;
{
NSFileManager *fileManager = [[NSFileManager alloc] init];
NSURL *cloudURL = [fileManager URLForUbiquityContainerIdentifier:bucketName];

return cloudURL;
}

+ (NSURL *) MR_defaultLocalStoreUrl
{
return [self MR_urlForStoreName:kMagicalRecordDefaultStoreFileName];
Expand Down
10 changes: 8 additions & 2 deletions Source/Categories/NSPersistentStoreCoordinator+MagicalRecord.h
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
#import "MagicalRecordHelpers.h"
#import "NSPersistentStore+MagicalRecord.h"

extern NSString * const kMagicalRecordPSCDidCompleteiCloudSetupNotification;

@interface NSPersistentStoreCoordinator (MagicalRecord)

Expand All @@ -21,8 +22,13 @@
+ (NSPersistentStoreCoordinator *) MR_coordinatorWithSqliteStoreNamed:(NSString *)storeFileName;
+ (NSPersistentStoreCoordinator *) MR_coordinatorWithAutoMigratingSqliteStoreNamed:(NSString *)storeFileName;
+ (NSPersistentStoreCoordinator *) MR_coordinatorWithPersitentStore:(NSPersistentStore *)persistentStore;
+ (NSPersistentStoreCoordinator *) MR_coordinatorWithiCloudContainerID:(NSString *)containerID
contentNameKey:(NSString *)contentNameKey
localStoreNamed:(NSString *)localStoreName
cloudStorePathComponent:(NSString *)subPathComponent;

- (NSPersistentStore *) MR_addInMemoryStore;

- (void) MR_addAutoMigratingSqliteStoreNamed:(NSString *) storeFileName;
- (void) MR_addSqliteStoreNamed:(id)storeFileName withOptions:(__autoreleasing NSDictionary *)options;
- (void) MR_addiCloudContainerID:(NSString *)containerID contentNameKey:(NSString *)contentNameKey localStoreNamed:(NSString *)localStoreName cloudStorePathComponent:(NSString *)subPathComponent;
@end

Loading

0 comments on commit 719ffad

Please sign in to comment.