Skip to content

Commit

Permalink
Merge pull request a2#4 from rastersize/master
Browse files Browse the repository at this point in the history
Change so that +start can be called multiple times
  • Loading branch information
a2 committed Jul 8, 2012
2 parents 7e9c803 + e051611 commit 2c4672a
Show file tree
Hide file tree
Showing 2 changed files with 75 additions and 26 deletions.
2 changes: 2 additions & 0 deletions MKiCloudSync.h
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,8 @@ extern NSString *MKiCloudSyncDidUpdateNotification;

@interface MKiCloudSync : NSObject

+ (BOOL) isSyncing;

+ (BOOL) start;

+ (NSMutableSet *) ignoredKeys;
Expand Down
99 changes: 73 additions & 26 deletions MKiCloudSync.m
Original file line number Diff line number Diff line change
Expand Up @@ -33,41 +33,82 @@ @interface MKiCloudSync ()
+ (void) pullFromICloud: (NSNotification *) note;
+ (void) pushToICloud;

+ (BOOL)tryToStartSync;

@end

@implementation MKiCloudSync

static dispatch_queue_t _queue;
static BOOL _isSyncing;

+ (void)initialize
{
static dispatch_once_t onceToken;
dispatch_once(&onceToken, ^{
_queue = dispatch_queue_create("com.mugunthkumarMKiCloudSync", DISPATCH_QUEUE_SERIAL);
_isSyncing = NO;
});
}

+ (BOOL) isSyncing
{
__block BOOL isSyncing = NO;
dispatch_sync(_queue, ^{
isSyncing = _isSyncing;
});
return isSyncing;
}

+ (BOOL)tryToStartSync
{
__block BOOL didSucceed = NO;
dispatch_sync(_queue, ^{
if (!_isSyncing) {
_isSyncing = YES;
didSucceed = YES;
}
});
return didSucceed;
}

+ (BOOL) start
{
if ([NSUbiquitousKeyValueStore class] && [NSUbiquitousKeyValueStore defaultStore])
{
// Force push
[MKiCloudSync pushToICloud];

// Force pull
NSUbiquitousKeyValueStore *store = [NSUbiquitousKeyValueStore defaultStore];
NSDictionary *dict = [store dictionaryRepresentation];

NSUserDefaults *userDefaults = [NSUserDefaults standardUserDefaults];
[dict enumerateKeysAndObjectsUsingBlock: ^(id key, id obj, BOOL *stop) {
[userDefaults setObject: obj forKey: key];
}];
[userDefaults synchronize];

NSNotificationCenter *dnc = [NSNotificationCenter defaultCenter];

// Post notification
[dnc postNotificationName: MKiCloudSyncDidUpdateNotification object: self];

// Add self as observer
[dnc addObserver: self selector: @selector(pullFromICloud:) name: NSUbiquitousKeyValueStoreDidChangeExternallyNotification object: store];
[dnc addObserver: self selector: @selector(pushToICloud) name: NSUserDefaultsDidChangeNotification object: nil];

if ([self tryToStartSync]) {
#if MKiCloudSyncDebug
NSLog(@"MKiCloudSync: Updating from iCloud");
NSLog(@"MKiCloudSync: Will start sync");
#endif

return YES;

// Force push
[MKiCloudSync pushToICloud];

// Force pull
NSUbiquitousKeyValueStore *store = [NSUbiquitousKeyValueStore defaultStore];
NSDictionary *dict = [store dictionaryRepresentation];

NSUserDefaults *userDefaults = [NSUserDefaults standardUserDefaults];
[dict enumerateKeysAndObjectsUsingBlock: ^(id key, id obj, BOOL *stop) {
[userDefaults setObject: obj forKey: key];
}];
[userDefaults synchronize];

NSNotificationCenter *dnc = [NSNotificationCenter defaultCenter];

// Post notification
[dnc postNotificationName: MKiCloudSyncDidUpdateNotification object: self];

// Add self as observer
[dnc addObserver: self selector: @selector(pullFromICloud:) name: NSUbiquitousKeyValueStoreDidChangeExternallyNotification object: store];
[dnc addObserver: self selector: @selector(pushToICloud) name: NSUserDefaultsDidChangeNotification object: nil];

#if MKiCloudSyncDebug
NSLog(@"MKiCloudSync: Did start sync");
NSLog(@"MKiCloudSync: Updating from iCloud");
#endif
return YES;
}
}

return NO;
Expand Down Expand Up @@ -147,7 +188,13 @@ + (void) pushToICloud
}
+ (void) stop
{
[[NSNotificationCenter defaultCenter] removeObserver: self];
#if MKiCloudSyncDebug
NSLog(@"MKiCloudSync: Stop syncining with iCloud");
#endif
dispatch_sync(_queue, ^{
_isSyncing = NO;
[[NSNotificationCenter defaultCenter] removeObserver: self];
});
}

@end

0 comments on commit 2c4672a

Please sign in to comment.