Skip to content
This repository has been archived by the owner on Mar 4, 2020. It is now read-only.

Commit

Permalink
Prefix support to selectively sync keys with a given prefix
Browse files Browse the repository at this point in the history
  • Loading branch information
MugunthKumar committed May 31, 2015
1 parent 9e57c89 commit 4781c7c
Show file tree
Hide file tree
Showing 2 changed files with 65 additions and 65 deletions.
2 changes: 1 addition & 1 deletion MKiCloudSync.h
Expand Up @@ -41,5 +41,5 @@

@interface MKiCloudSync : NSObject

+(void) start;
+(void) startWithPrefix:(NSString*) prefixToSync;
@end
128 changes: 64 additions & 64 deletions MKiCloudSync.m
Expand Up @@ -23,86 +23,86 @@
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
// THE SOFTWARE.

// As a side note, you might also consider
// As a side note, you might also consider
// 1) tweeting about this mentioning @mugunthkumar
// 2) A paypal donation to mugunth.kumar@gmail.com


#import "MKiCloudSync.h"

static NSString *prefix;
@implementation MKiCloudSync

+(void) updateToiCloud:(NSNotification*) notificationObject {

NSDictionary *dict = [[NSUserDefaults standardUserDefaults] dictionaryRepresentation];

[dict enumerateKeysAndObjectsUsingBlock:^(id key, id obj, BOOL *stop) {

[[NSUbiquitousKeyValueStore defaultStore] setObject:obj forKey:key];
}];

[[NSUbiquitousKeyValueStore defaultStore] synchronize];

NSDictionary *dict = [[NSUserDefaults standardUserDefaults] dictionaryRepresentation];

[dict enumerateKeysAndObjectsUsingBlock:^(id key, id obj, BOOL *stop) {

if([key hasPrefix:prefix]) {
[[NSUbiquitousKeyValueStore defaultStore] setObject:obj forKey:key];
}
}];

[[NSUbiquitousKeyValueStore defaultStore] synchronize];
}

+(void) updateFromiCloud:(NSNotification*) notificationObject {

NSUbiquitousKeyValueStore *iCloudStore = [NSUbiquitousKeyValueStore defaultStore];
NSDictionary *dict = [iCloudStore dictionaryRepresentation];

// prevent NSUserDefaultsDidChangeNotification from being posted while we update from iCloud

[[NSNotificationCenter defaultCenter] removeObserver:self
name:NSUserDefaultsDidChangeNotification
object:nil];

[dict enumerateKeysAndObjectsUsingBlock:^(id key, id obj, BOOL *stop) {

[[NSUserDefaults standardUserDefaults] setObject:obj forKey:key];
}];

[[NSUserDefaults standardUserDefaults] synchronize];

// enable NSUserDefaultsDidChangeNotification notifications again

[[NSNotificationCenter defaultCenter] addObserver:self
selector:@selector(updateToiCloud:)
name:NSUserDefaultsDidChangeNotification
object:nil];

[[NSNotificationCenter defaultCenter] postNotificationName:kMKiCloudSyncNotification object:nil];
}

+(void) start {

if(NSClassFromString(@"NSUbiquitousKeyValueStore")) { // is iOS 5?

if([NSUbiquitousKeyValueStore defaultStore]) { // is iCloud enabled

[[NSNotificationCenter defaultCenter] addObserver:self
selector:@selector(updateFromiCloud:)
name:NSUbiquitousKeyValueStoreDidChangeExternallyNotification
object:nil];

[[NSNotificationCenter defaultCenter] addObserver:self
selector:@selector(updateToiCloud:)
name:NSUserDefaultsDidChangeNotification object:nil];
} else {
DLog(@"iCloud not enabled");
}
}
else {
DLog(@"Not an iOS 5 device");
NSUbiquitousKeyValueStore *iCloudStore = [NSUbiquitousKeyValueStore defaultStore];
NSDictionary *dict = [iCloudStore dictionaryRepresentation];

// prevent NSUserDefaultsDidChangeNotification from being posted while we update from iCloud

[[NSNotificationCenter defaultCenter] removeObserver:self
name:NSUserDefaultsDidChangeNotification
object:nil];

[dict enumerateKeysAndObjectsUsingBlock:^(id key, id obj, BOOL *stop) {

if([key hasPrefix:prefix]) {
[[NSUserDefaults standardUserDefaults] setObject:obj forKey:key];
}
}];

[[NSUserDefaults standardUserDefaults] synchronize];

// enable NSUserDefaultsDidChangeNotification notifications again

[[NSNotificationCenter defaultCenter] addObserver:self
selector:@selector(updateToiCloud:)
name:NSUserDefaultsDidChangeNotification
object:nil];

[[NSNotificationCenter defaultCenter] postNotificationName:kMKiCloudSyncNotification object:nil];
}

+(void) startWithPrefix:(NSString*) prefixToSync {

prefix = prefixToSync;
if([NSUbiquitousKeyValueStore defaultStore]) { // is iCloud enabled

[[NSNotificationCenter defaultCenter] addObserver:self
selector:@selector(updateFromiCloud:)
name:NSUbiquitousKeyValueStoreDidChangeExternallyNotification
object:nil];

[[NSNotificationCenter defaultCenter] addObserver:self
selector:@selector(updateToiCloud:)
name:NSUserDefaultsDidChangeNotification object:nil];
} else {
DLog(@"iCloud not enabled");
}
}

+ (void) dealloc {
[[NSNotificationCenter defaultCenter] removeObserver:self
name:NSUbiquitousKeyValueStoreDidChangeExternallyNotification
object:nil];

[[NSNotificationCenter defaultCenter] removeObserver:self
name:NSUserDefaultsDidChangeNotification
object:nil];

[[NSNotificationCenter defaultCenter] removeObserver:self
name:NSUbiquitousKeyValueStoreDidChangeExternallyNotification
object:nil];

[[NSNotificationCenter defaultCenter] removeObserver:self
name:NSUserDefaultsDidChangeNotification
object:nil];
}
@end

0 comments on commit 4781c7c

Please sign in to comment.