Skip to content

Commit

Permalink
Added database subscriptions. Fixed a bug when deleting existing reco…
Browse files Browse the repository at this point in the history
…rdZoneID subscription
  • Loading branch information
mentrena committed Oct 7, 2018
1 parent e6d1124 commit 9f8541b
Show file tree
Hide file tree
Showing 14 changed files with 188 additions and 19 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -452,7 +452,7 @@
files = (
);
inputPaths = (
"${SRCROOT}/Pods/Target Support Files/Pods-SyncKitCoreDataExample/Pods-SyncKitCoreDataExample-frameworks.sh",
"${PODS_ROOT}/Target Support Files/Pods-SyncKitCoreDataExample/Pods-SyncKitCoreDataExample-frameworks.sh",
"${BUILT_PRODUCTS_DIR}/SyncKit/SyncKit.framework",
);
name = "[CP] Embed Pods Frameworks";
Expand All @@ -461,7 +461,7 @@
);
runOnlyForDeploymentPostprocessing = 0;
shellPath = /bin/sh;
shellScript = "\"${SRCROOT}/Pods/Target Support Files/Pods-SyncKitCoreDataExample/Pods-SyncKitCoreDataExample-frameworks.sh\"\n";
shellScript = "\"${PODS_ROOT}/Target Support Files/Pods-SyncKitCoreDataExample/Pods-SyncKitCoreDataExample-frameworks.sh\"\n";
showEnvVarsInLog = 0;
};
4403ED7D9555A91B762043CF /* [CP] Embed Pods Frameworks */ = {
Expand All @@ -470,7 +470,7 @@
files = (
);
inputPaths = (
"${SRCROOT}/Pods/Target Support Files/Pods-SyncKitCoreDataExampleTests/Pods-SyncKitCoreDataExampleTests-frameworks.sh",
"${PODS_ROOT}/Target Support Files/Pods-SyncKitCoreDataExampleTests/Pods-SyncKitCoreDataExampleTests-frameworks.sh",
"${BUILT_PRODUCTS_DIR}/OCMock/OCMock.framework",
);
name = "[CP] Embed Pods Frameworks";
Expand All @@ -479,7 +479,7 @@
);
runOnlyForDeploymentPostprocessing = 0;
shellPath = /bin/sh;
shellScript = "\"${SRCROOT}/Pods/Target Support Files/Pods-SyncKitCoreDataExampleTests/Pods-SyncKitCoreDataExampleTests-frameworks.sh\"\n";
shellScript = "\"${PODS_ROOT}/Target Support Files/Pods-SyncKitCoreDataExampleTests/Pods-SyncKitCoreDataExampleTests-frameworks.sh\"\n";
showEnvVarsInLog = 0;
};
D95C6C6549BDF6E545FD0AC9 /* [CP] Check Pods Manifest.lock */ = {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -415,24 +415,54 @@ - (void)testEraseRemote_deletesRecordZone
XCTAssert(called == YES);
}

- (void)testSubscribeForUpdateNotifications_savesToDatabase
- (void)testSubscribeForRecordZoneNotifications_savesToDatabase
{
[self clearAllUserDefaults];

XCTestExpectation *expectation = [self expectationWithDescription:@"Save subscription called"];

__block BOOL called = NO;

self.mockDatabase.subscriptionIDReturnValue = @"123";
self.mockDatabase.saveSubscriptionCalledBlock = ^(CKSubscription *subscription) {
called = YES;
[expectation fulfill];
};

[self.synchronizer subscribeForChangesIn:self.recordZoneID completion:nil];
[self.synchronizer subscribeForChangesIn:self.recordZoneID completion:^(NSError * _Nullable error) {
[expectation fulfill];
}];

[self waitForExpectationsWithTimeout:1 handler:nil];

NSString *subscriptionID = [self.synchronizer subscriptionIDForRecordZoneID:self.recordZoneID];

XCTAssertTrue(called);
XCTAssertEqual(subscriptionID, @"123");
}

- (void)testSubscribeForDatabaseNotifications_savesToDatabase
{
[self clearAllUserDefaults];

XCTestExpectation *expectation = [self expectationWithDescription:@"Save subscription called"];

__block BOOL called = NO;

self.mockDatabase.scope = CKDatabaseScopeShared;
self.mockDatabase.subscriptionIDReturnValue = @"456";
self.mockDatabase.saveSubscriptionCalledBlock = ^(CKSubscription *subscription) {
called = YES;
};

[self.synchronizer subscribeForChangesInDatabaseWithCompletion:^(NSError * _Nullable error) {
[expectation fulfill];
}];

[self waitForExpectationsWithTimeout:1 handler:nil];
NSString *subscriptionID = [self.synchronizer subscriptionIDForDatabaseSubscription];

XCTAssertTrue(called);
XCTAssertEqual(subscriptionID, @"456");
}

- (void)testSubscribeForUpdateNotifications_existingSubscription_updatesSubscriptionID
Expand Down Expand Up @@ -477,14 +507,18 @@ - (void)testDeleteSubscription_deletesOnDatabase

self.mockDatabase.deleteSubscriptionCalledBlock = ^(NSString *subscriptionID) {
called = YES;
[deleted fulfill];
};

[self.synchronizer cancelSubscriptionForChangesIn:self.recordZoneID completion:nil];
[self.synchronizer cancelSubscriptionForChangesIn:self.recordZoneID completion:^(NSError * _Nullable error) {
[deleted fulfill];
}];

[self waitForExpectationsWithTimeout:1 handler:nil];

NSString *subscriptionID = [self.synchronizer subscriptionIDForRecordZoneID:self.recordZoneID];

XCTAssertTrue(called);
XCTAssertNil(subscriptionID);
}

- (void)testDeleteSubscription_noLocalSubscriptionButRemoteOne_deletesOnDatabase
Expand Down
3 changes: 3 additions & 0 deletions Example/CoreData/SyncKitCoreDataExampleTests/QSMockDatabase.h
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@

@interface QSMockDatabase : NSObject

@property (nonatomic, assign) CKDatabaseScope scope;
@property (nonatomic, readonly, nullable) NSString * serverToken;

@property (nonatomic, strong, nonnull) NSArray *receivedRecords;
Expand All @@ -29,6 +30,8 @@

@property (nonatomic, strong, nullable) NSArray *subscriptions;

@property (nonatomic, strong, nullable) NSString *subscriptionIDReturnValue;

@property (nonatomic, copy, nullable) void(^fetchDatabaseChangesOperationEnqueuedBlock)(CKFetchDatabaseChangesOperation * _Nonnull operation);
@property (nonatomic, copy, nullable) void(^fetchRecordZoneChangesOperationEnqueuedBlock)(CKFetchRecordZoneChangesOperation * _Nonnull operation);
@property (nonatomic, copy, nullable) void(^modifyRecordsOperationEnqueuedBlock)(CKModifyRecordsOperation * _Nonnull operation);
Expand Down
17 changes: 16 additions & 1 deletion Example/CoreData/SyncKitCoreDataExampleTests/QSMockDatabase.m
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,16 @@ @interface QSMockDatabase ()

@implementation QSMockDatabase

- (instancetype)init
{
self = [super init];
if (self) {
self.scope = CKDatabaseScopePrivate;
self.subscriptionIDReturnValue = @"subscriptionIdentifier";
}
return self;
}

- (NSString *)serverToken
{
return [NSString stringWithFormat:@"token%ld", (long)self.tokenIndex];
Expand All @@ -38,6 +48,11 @@ - (NSArray *)deletedRecordIDs
return _deletedRecordIDs;
}

- (CKDatabaseScope)databaseScope
{
return self.scope;
}

- (void)addOperation:(CKDatabaseOperation *)operation
{
if ([operation isKindOfClass:[CKFetchRecordZoneChangesOperation class]]) {
Expand Down Expand Up @@ -156,7 +171,7 @@ - (void)saveSubscription:(CKSubscription *)subscription completionHandler:(void
self.saveSubscriptionCalledBlock(subscription);
}
id mockSubscription = OCMClassMock([CKSubscription class]);
OCMStub([mockSubscription subscriptionID]).andReturn(@"subscriptionIdentifier");
OCMStub([mockSubscription subscriptionID]).andReturn(self.subscriptionIDReturnValue);
completionHandler(mockSubscription, nil);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
<key>CFBundlePackageType</key>
<string>FMWK</string>
<key>CFBundleShortVersionString</key>
<string>0.6.4</string>
<string>0.6.5</string>
<key>CFBundleVersion</key>
<string>$(CURRENT_PROJECT_VERSION)</string>
<key>NSPrincipalClass</key>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
<key>CFBundlePackageType</key>
<string>FMWK</string>
<key>CFBundleShortVersionString</key>
<string>0.6.4</string>
<string>0.6.5</string>
<key>CFBundleVersion</key>
<string>$(CURRENT_PROJECT_VERSION)</string>
<key>NSHumanReadableCopyright</key>
Expand Down
2 changes: 1 addition & 1 deletion Example/Realm/SyncKitRealmIOS/SyncKitRealm/Info.plist
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
<key>CFBundlePackageType</key>
<string>FMWK</string>
<key>CFBundleShortVersionString</key>
<string>0.6.4</string>
<string>0.6.5</string>
<key>CFBundleVersion</key>
<string>$(CURRENT_PROJECT_VERSION)</string>
<key>NSPrincipalClass</key>
Expand Down
2 changes: 1 addition & 1 deletion Example/Realm/SyncKitRealmOSX/SyncKitRealmOSX/Info.plist
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
<key>CFBundlePackageType</key>
<string>FMWK</string>
<key>CFBundleShortVersionString</key>
<string>0.6.4</string>
<string>0.6.5</string>
<key>CFBundleVersion</key>
<string>$(CURRENT_PROJECT_VERSION)</string>
<key>NSHumanReadableCopyright</key>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
<key>CFBundlePackageType</key>
<string>FMWK</string>
<key>CFBundleShortVersionString</key>
<string>0.6.4</string>
<string>0.6.5</string>
<key>CFBundleVersion</key>
<string>$(CURRENT_PROJECT_VERSION)</string>
<key>NSPrincipalClass</key>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
<key>CFBundlePackageType</key>
<string>FMWK</string>
<key>CFBundleShortVersionString</key>
<string>0.6.4</string>
<string>0.6.5</string>
<key>CFBundleVersion</key>
<string>$(CURRENT_PROJECT_VERSION)</string>
<key>NSPrincipalClass</key>
Expand Down
2 changes: 1 addition & 1 deletion SyncKit.podspec
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@

Pod::Spec.new do |s|
s.name = 'SyncKit'
s.version = '0.6.4'
s.version = '0.6.5'
s.summary = 'CloudKit synchronization for your Core Data or Realm model.'

s.description = <<-DESC
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,9 @@ FOUNDATION_EXPORT NSString * _Nonnull const QSCloudKitModelCompatibilityVersionK
- (CKServerChangeToken *_Nullable)getStoredDatabaseToken;
- (void)storeDatabaseToken:(CKServerChangeToken *_Nullable)token;
- (NSString *_Nullable)storedSubscriptionIDForRecordZoneID:(CKRecordZoneID *_Nonnull)zoneID;
- (NSString *_Nullable)storedDatabaseSubscriptionID;
- (void)storeSubscriptionID:(NSString *_Nonnull)subscriptionID forRecordZoneID:(CKRecordZoneID *_Nonnull)zoneID;
- (void)storeDatabaseSubscriptionID:(NSString *_Nonnull)subscriptionID;
- (void)clearSubscriptionID:(NSString *_Nonnull)subscriptionID;
- (void)clearAllStoredSubscriptionIDs;

Expand Down
21 changes: 21 additions & 0 deletions SyncKit/Classes/QSSynchronizer/QSCloudKitSynchronizer+Private.m
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,11 @@ - (NSString *)storeKeyForZoneID:(CKRecordZoneID *)zoneID
return [self userDefaultsKeyForKey:[NSString stringWithFormat:@"%@.%@", zoneID.ownerName, zoneID.zoneName]];
}

- (NSString *)storeKeyForDatabase:(CKDatabase *)database
{
return [self userDefaultsKeyForKey:database.databaseScope == CKDatabaseScopePrivate ? @"privateDatabase" : @"sharedDatabase"];
}

- (NSString *)getStoredDeviceUUID
{
return (NSString *)[self.keyValueStore objectForKey:[self userDefaultsKeyForKey:QSCloudKitStoredDeviceUUIDKey]];
Expand Down Expand Up @@ -74,6 +79,12 @@ - (NSString *)storedSubscriptionIDForRecordZoneID:(CKRecordZoneID *)zoneID
return subscriptionIDsDictionary[[self storeKeyForZoneID:zoneID]];
}

- (NSString *_Nullable)storedDatabaseSubscriptionID
{
NSDictionary *subscriptionIDsDictionary = [self getStoredSubscriptionIDsDictionary];
return subscriptionIDsDictionary[[self storeKeyForDatabase:self.database]];
}

- (void)storeSubscriptionID:(NSString *)subscriptionID forRecordZoneID:(CKRecordZoneID *)zoneID
{
NSMutableDictionary *subscriptionIDsDictionary = [[self getStoredSubscriptionIDsDictionary] mutableCopy];
Expand All @@ -84,6 +95,16 @@ - (void)storeSubscriptionID:(NSString *)subscriptionID forRecordZoneID:(CKRecord
[self setStoredSubscriptionIDsDictionary:subscriptionIDsDictionary];
}

- (void)storeDatabaseSubscriptionID:(NSString *_Nonnull)subscriptionID
{
NSMutableDictionary *subscriptionIDsDictionary = [[self getStoredSubscriptionIDsDictionary] mutableCopy];
if (!subscriptionIDsDictionary) {
subscriptionIDsDictionary = [NSMutableDictionary dictionary];
}
subscriptionIDsDictionary[[self storeKeyForDatabase:self.database]] = subscriptionID;
[self setStoredSubscriptionIDsDictionary:subscriptionIDsDictionary];
}

- (void)clearSubscriptionID:(NSString *)subscriptionID
{
NSMutableDictionary *subscriptionIDsDictionary = [[self getStoredSubscriptionIDsDictionary] mutableCopy];
Expand Down
Loading

0 comments on commit 9f8541b

Please sign in to comment.