Skip to content

Commit

Permalink
Merge remote-tracking branch 'pandamonia/master'
Browse files Browse the repository at this point in the history
* pandamonia/master:
  Tag version 1.0.2
  Clean up changes from merged pull request

Conflicts:
	MKiCloudSync.m

Signed-off-by: Aron Cedercrantz <[email protected]>
  • Loading branch information
rastersize committed Aug 26, 2012
2 parents a3027d9 + 67d416f commit 3e36b94
Show file tree
Hide file tree
Showing 3 changed files with 82 additions and 80 deletions.
3 changes: 1 addition & 2 deletions MKiCloudSync.h
Original file line number Diff line number Diff line change
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
155 changes: 79 additions & 76 deletions MKiCloudSync.m
Original file line number Diff line number Diff line change
Expand Up @@ -3,19 +3,19 @@
//
// Created by Mugunth Kumar on 11/20//11.
// Modified by Alexsander Akers on 1/4/12.
//
//
// Copyright (C) 2011-2020 by Steinlogic
//
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
//
// The above copyright notice and this permission notice shall be included in
// all copies or substantial portions of the Software.
//
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
Expand All @@ -24,106 +24,99 @@
// 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.mugunthkumar.MKiCloudSync", 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])
if ([NSUbiquitousKeyValueStore class] && [NSUbiquitousKeyValueStore defaultStore] && [self tryToStartSync])
{
if ([self tryToStartSync]) {
#if MKiCloudSyncDebug
NSLog(@"MKiCloudSync: Will start sync");
NSLog(@"MKiCloudSync: Will start sync");
#endif
// Force pull
NSUbiquitousKeyValueStore *store = [NSUbiquitousKeyValueStore defaultStore];
NSDictionary *dict = [store dictionaryRepresentation];

NSMutableSet *syncedKeys = [NSMutableSet setWithArray: [dict allKeys]];
if ([[self whitelistedKeys] count] > 0) {
[syncedKeys intersectSet: [self whitelistedKeys]];
}
[syncedKeys minusSet: [self ignoredKeys]];

NSUserDefaults *userDefaults = [NSUserDefaults standardUserDefaults];
for (id key in syncedKeys) {
id obj = [store objectForKey: key];
[userDefaults setObject: obj forKey:key];
}
[userDefaults synchronize];

// Force push
[MKiCloudSync pushToICloud];

// Post notification
NSNotificationCenter *dnc = [NSNotificationCenter defaultCenter];
[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;
// Force push
[MKiCloudSync pushToICloud];

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

NSMutableSet *syncedKeys = [NSMutableSet setWithArray: [dict allKeys]];
if ([[self whitelistedKeys] count] > 0) {
[syncedKeys intersectSet: [self whitelistedKeys]];
}
[syncedKeys minusSet: [self ignoredKeys]];

NSUserDefaults *userDefaults = [NSUserDefaults standardUserDefaults];
for (id key in syncedKeys) {
id obj = [store objectForKey: key];
[userDefaults setObject: obj forKey:key];
}
[userDefaults synchronize];

// Post notification
NSNotificationCenter *dnc = [NSNotificationCenter defaultCenter];
[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");
#endif
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 = nil;
static dispatch_once_t once;
dispatch_once(&once, ^{
static dispatch_once_t token;
dispatch_once(&token, ^{
ignoredKeys = [NSMutableSet new];
});

Expand Down Expand Up @@ -160,20 +153,29 @@ + (void) cleanUbiquitousStore
[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;
NSMutableSet *changedKeys = [NSMutableSet setWithArray:[note.userInfo objectForKey: NSUbiquitousKeyValueStoreChangedKeysKey]];

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

if ([[self whitelistedKeys] count] > 0) {
Expand Down Expand Up @@ -218,12 +220,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
4 changes: 2 additions & 2 deletions MKiCloudSync.podspec
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
Pod::Spec.new do |s|
s.name = 'MKiCloudSync'
s.version = '1.0.1'
s.version = '1.0.2'
s.license = 'MIT'
s.summary = 'Sync your NSUserDefaults to iCloud automatically.'
s.homepage = 'https://github.com/pandamonia/MKiCloudSync'
s.author = { 'Alexsander Akers' => '[email protected]',
'Mugunth Kumar' => '[email protected]' }
s.source = { :git => 'https://github.com/pandamonia/MKiCloudSync.git', :tag => 'v1.0.1' }
s.source = { :git => 'https://github.com/pandamonia/MKiCloudSync.git', :tag => 'v1.0.2' }
s.platform = :ios
s.source_files = 'MKiCloudSync.{h,m}'
s.requires_arc = true
Expand Down

0 comments on commit 3e36b94

Please sign in to comment.