Skip to content

Commit

Permalink
Clean up changes from merged pull request
Browse files Browse the repository at this point in the history
Signed-off-by: Alexsander Akers <a2@pandamonia.us>
  • Loading branch information
a2 committed Jul 8, 2012
1 parent 2c4672a commit bbf8468
Show file tree
Hide file tree
Showing 2 changed files with 68 additions and 70 deletions.
3 changes: 1 addition & 2 deletions MKiCloudSync.h
Expand Up @@ -30,12 +30,11 @@
#define MKiCloudSyncDebug 0

// Posted
extern NSString *MKiCloudSyncDidUpdateNotification;
extern NSString *const MKiCloudSyncDidUpdateNotification;

@interface MKiCloudSync : NSObject

+ (BOOL) isSyncing;

+ (BOOL) start;

+ (NSMutableSet *) ignoredKeys;
Expand Down
135 changes: 67 additions & 68 deletions MKiCloudSync.m
Expand Up @@ -24,101 +24,92 @@
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
// THE SOFTWARE.

NSString *MKiCloudSyncDidUpdateNotification = @"MKiCloudSyncDidUpdateNotification";

#import "MKiCloudSync.h"

NSString *const MKiCloudSyncDidUpdateNotification = @"MKiCloudSyncDidUpdateNotification";

static BOOL _isSyncing;
static dispatch_queue_t _queue;

@interface MKiCloudSync ()

+ (BOOL) tryToStartSync;

+ (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;
return isSyncing;
}

+ (BOOL) start
{
if ([NSUbiquitousKeyValueStore class] && [NSUbiquitousKeyValueStore defaultStore])
if ([NSUbiquitousKeyValueStore class] && [NSUbiquitousKeyValueStore defaultStore] && [self tryToStartSync])
{
if ([self tryToStartSync]) {
#if MKiCloudSyncDebug
NSLog(@"MKiCloudSync: Will start sync");
NSLog(@"MKiCloudSync: Will start sync");
#endif
// 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];

// 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");
NSLog(@"MKiCloudSync: Did start sync");
#endif
return YES;
}
return YES;
}

return NO;
}
+ (BOOL) tryToStartSync
{
__block BOOL didSucceed = NO;

dispatch_sync(_queue, ^{
if (!_isSyncing)
{
_isSyncing = YES;
didSucceed = YES;
}
});

return didSucceed;
}

+ (NSMutableSet *) ignoredKeys
{
static NSMutableSet *ignoredKeys;
static dispatch_once_t once;
dispatch_once(&once, ^{
static dispatch_once_t token;
dispatch_once(&token, ^{
ignoredKeys = [NSMutableSet new];
});

Expand All @@ -138,23 +129,30 @@ + (void) cleanUbiquitousStore
[keys enumerateObjectsUsingBlock: ^(NSString *key, BOOL *stop) {
[store removeObjectForKey: key];
}];

[store synchronize];

#if MKiCloudSyncDebug
NSLog(@"MKiCloudSync: Cleaned ubiquitous store");
NSLog(@"MKiCloudSync: Cleaned ubiquitous store");
#endif
}
+ (void) initialize
{
if (self == [MKiCloudSync class])
{
_isSyncing = NO;
_queue = dispatch_queue_create("com.mugunthkumar.MKiCloudSync", DISPATCH_QUEUE_SERIAL);
}
}
+ (void) pullFromICloud: (NSNotification *) note
{
NSNotificationCenter *dnc = [NSNotificationCenter defaultCenter];
[dnc removeObserver: self name: NSUserDefaultsDidChangeNotification object: nil];

NSUbiquitousKeyValueStore *store = note.object;
NSArray *changedKeys = [note.userInfo objectForKey: NSUbiquitousKeyValueStoreChangedKeysKey];

#if MKiCloudSyncDebug
NSLog(@"MKiCloudSync: Pulled from iCloud");
NSLog(@"MKiCloudSync: Pulled from iCloud");
#endif

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

#if MKiCloudSyncDebug
NSLog(@"MKiCloudSync: Stopped syncing with iCloud");
#endif
});
}

Expand Down

0 comments on commit bbf8468

Please sign in to comment.