-
Notifications
You must be signed in to change notification settings - Fork 1
/
CKDatabase+Convenience.m
172 lines (134 loc) · 9.04 KB
/
CKDatabase+Convenience.m
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
//
// CKDatabase+Convenience.m
// Ringo
//
// Created by Alexander Ivanov on 27.06.15.
// Copyright (c) 2015 Alexander Ivanov. All rights reserved.
//
#import "CKDatabase+Convenience.h"
@implementation CKDatabase (Convenience)
- (void)saveRecordWithType:(NSString *)type name:(NSString *)name fields:(NSDictionary *)fields completionHandler:(void (^)(CKRecord *, NSError *))completionHandler {
CKRecordID *ID = [[CKRecordID alloc] initWithRecordName:name];
CKRecord *record = [[CKRecord alloc] initWithRecordType:type recordID:ID];
for (NSString *key in fields)
[record setObject:fields[key] forKey:key];
[self saveRecord:record completionHandler:completionHandler];
}
- (void)deleteRecordWithName:(NSString *)name completionHandler:(void (^)(CKRecordID *, NSError *))completionHandler {
CKRecordID *ID = [[CKRecordID alloc] initWithRecordName:name];
[self deleteRecordWithID:ID completionHandler:completionHandler];
}
- (void)startOperation:(CKDatabaseOperation *)operation allowsCellularAccess:(BOOL)allowsCellularAccess {
operation.allowsCellularAccess = allowsCellularAccess;
if (allowsCellularAccess)
operation.qualityOfService = NSQualityOfServiceUserInitiated;
operation.database = self;
[operation start];
}
- (CKModifyRecordsOperation *)deleteRecords:(NSArray<CKRecord *> *)records completionHandler:(void (^)(NSArray<CKRecord *> *, NSArray<CKRecordID *> *, NSError *))completionHandler {
if (!records.count)
return Nil;
NSArray<CKRecordID *> *recordIDs = [records map:^id(CKRecord *obj) {
return obj.recordID;
}];
CKModifyRecordsOperation *operation = [[CKModifyRecordsOperation alloc] initWithRecordsToSave:Nil recordIDsToDelete:recordIDs];
operation.modifyRecordsCompletionBlock = completionHandler;
[self startOperation:operation allowsCellularAccess:YES];
return operation;
}
- (CKModifyRecordsOperation *)updateRecords:(NSArray<CKRecord *> *)records completionHandler:(void (^)(NSArray<CKRecord *> *, NSArray<CKRecordID *> *, NSError *))completionHandler {
if (!records.count)
return Nil;
CKModifyRecordsOperation *operation = [[CKModifyRecordsOperation alloc] initWithRecordsToSave:records recordIDsToDelete:Nil];
operation.modifyRecordsCompletionBlock = completionHandler;
[self startOperation:operation allowsCellularAccess:YES];
return operation;
}
- (CKModifyRecordsOperation *)deleteRecord:(CKRecord *)record completionHandler:(void (^)(NSArray<CKRecord *> *, NSArray<CKRecordID *> *, NSError *))completionHandler {
return [self deleteRecords:arr_(record) completionHandler:completionHandler];
}
- (CKModifyRecordsOperation *)updateRecord:(CKRecord *)record completionHandler:(void (^)(NSArray<CKRecord *> *, NSArray<CKRecordID *> *, NSError *))completionHandler {
return [self updateRecords:arr_(record) completionHandler:completionHandler];
}
- (CKModifySubscriptionsOperation *)saveSubscriptions:(NSArray<CKSubscription *> *)subscriptionsToSave deleteSubscriptionsWithIDs:(NSArray<NSString *> *)subscriptionIDsToDelete completionHandler:(void (^)(NSArray<CKSubscription *> *savedSubscriptions, NSArray<NSString *> *deletedSubscriptionIDs, NSError *operationError))completionHandler {
if (!subscriptionsToSave.count && !subscriptionIDsToDelete.count)
return Nil;
CKModifySubscriptionsOperation *operation = [[CKModifySubscriptionsOperation alloc] initWithSubscriptionsToSave:subscriptionIDsToDelete.count ? Nil : subscriptionsToSave subscriptionIDsToDelete:subscriptionIDsToDelete];
operation.modifySubscriptionsCompletionBlock = subscriptionsToSave.count && subscriptionIDsToDelete.count ? ^(NSArray<CKSubscription *> * savedSubscriptions, NSArray<NSString *> * deletedSubscriptionIDs, NSError * operationError) {
CKModifySubscriptionsOperation *operation = [[CKModifySubscriptionsOperation alloc] initWithSubscriptionsToSave:subscriptionsToSave subscriptionIDsToDelete:Nil];
operation.modifySubscriptionsCompletionBlock = ^(NSArray<CKSubscription *> * savedSubscriptions, NSArray<NSString *> * _deletedSubscriptionIDs, NSError * operationError) {
if (completionHandler)
completionHandler(savedSubscriptions, deletedSubscriptionIDs, operationError);
[operationError log:@"saveSubscriptions:"];
};
[self startOperation:operation allowsCellularAccess:YES];
[operationError log:@"deleteSubscriptions:"];
} : ^(NSArray<CKSubscription *> * savedSubscriptions, NSArray<NSString *> * deletedSubscriptionIDs, NSError * operationError) {
if (completionHandler)
completionHandler(savedSubscriptions, deletedSubscriptionIDs, operationError);
[operationError log:@"modifySubscriptions:"];
};
[self startOperation:operation allowsCellularAccess:YES];
return operation;
}
- (CKModifySubscriptionsOperation *)saveSubscriptions:(NSArray<CKSubscription *> *)subscriptions completionHandler:(void (^)(NSArray<CKSubscription *> *, NSArray<NSString *> *, NSError *))completionHandler {
return [self saveSubscriptions:subscriptions deleteSubscriptionsWithIDs:Nil completionHandler:completionHandler];
}
- (CKModifySubscriptionsOperation *)saveSubscriptions:(NSArray<CKSubscription *> *)subscriptions {
return [self saveSubscriptions:subscriptions completionHandler:^(NSArray<CKSubscription *> *savedSubscriptions, NSArray<NSString *> *deletedSubscriptionIDs, NSError *operationError) {
[operationError log:@"saveSubscriptions:"];
}];
}
- (CKModifySubscriptionsOperation *)saveSubscription:(CKSubscription *)subscription {
return [self saveSubscriptions:arr_(subscription)];
}
- (CKModifySubscriptionsOperation *)deleteSubscriptionsWithIDs:(NSArray<NSString *> *)subscriptionIDs completionHandler:(void (^)(NSArray <CKSubscription *> *, NSArray <NSString *> *, NSError *))completionHandler {
return [self saveSubscriptions:Nil deleteSubscriptionsWithIDs:subscriptionIDs completionHandler:completionHandler];
}
- (CKModifySubscriptionsOperation *)deleteSubscriptionsWithIDs:(NSArray<NSString *> *)subscriptionIDs {
return [self deleteSubscriptionsWithIDs:subscriptionIDs completionHandler:^(NSArray<CKSubscription *> *savedSubscriptions, NSArray<NSString *> *deletedSubscriptionIDs, NSError *operationError) {
[operationError log:@"deleteSubscriptionsWithIDs:"];
}];
}
- (CKModifySubscriptionsOperation *)deleteSubscriptionWithID:(NSString *)subscriptionID {
return [self deleteSubscriptionsWithIDs:arr_(subscriptionID)];
}
- (CKFetchSubscriptionsOperation *)fetchSubscriptionsWithIDs:(NSArray<NSString *> *)subscriptionIDs completionHandler:(void (^)(NSDictionary <NSString *, CKSubscription *> *, NSError *))completionHandler {
CKFetchSubscriptionsOperation *operation = subscriptionIDs ? [[CKFetchSubscriptionsOperation alloc] initWithSubscriptionIDs:subscriptionIDs] : [CKFetchSubscriptionsOperation fetchAllSubscriptionsOperation];
operation.fetchSubscriptionCompletionBlock = completionHandler;
[self startOperation:operation allowsCellularAccess:YES];
return operation;
}
- (CKFetchSubscriptionsOperation *)modifySubscriptions:(NSArray<CKSubscription *> *)subscriptions completionHandler:(void (^)(NSArray<CKSubscription *> *, NSArray<NSString *> *))completionHandler {
return [self fetchSubscriptionsWithIDs:Nil completionHandler:^(NSDictionary<NSString *,CKSubscription *> *subscriptionsBySubscriptionID, NSError *operationError) {
NSArray<CKSubscription *> *subscriptionsToSave = [subscriptions query:^BOOL(CKSubscription *obj) {
return subscriptionsBySubscriptionID[obj.subscriptionID] == Nil;
}];
NSArray<NSString *> *subscriptionIDsToDelete = [subscriptionsBySubscriptionID.allKeys query:^BOOL(NSString *subscriptionID) {
return ![subscriptions any:^BOOL(CKSubscription *subscription) {
return [subscription.subscriptionID isEqualToString:subscriptionID];
}];
}];
[self saveSubscriptions:subscriptionsToSave deleteSubscriptionsWithIDs:subscriptionIDsToDelete completionHandler:^(NSArray<CKSubscription *> *savedSubscriptions, NSArray<NSString *> *deletedSubscriptionIDs, NSError *operationError) {
if (completionHandler)
completionHandler(savedSubscriptions, deletedSubscriptionIDs);
}];
[operationError log:@"fetchSubscriptionsWithIDs:"];
}];
}
- (CKFetchSubscriptionsOperation *)modifySubscriptions:(NSArray<CKSubscription *> *)subscriptions {
return [self modifySubscriptions:subscriptions completionHandler:Nil];
}
- (CKFetchSubscriptionsOperation *)modifyAllSubscriptions:(NSArray<CKSubscription *> *)subscriptions completionHandler:(void (^)(NSArray<CKSubscription *> *, NSArray<NSString *> *))completionHandler {
return [self fetchSubscriptionsWithIDs:Nil completionHandler:^(NSDictionary<NSString *,CKSubscription *> *subscriptionsBySubscriptionID, NSError *operationError) {
[self saveSubscriptions:subscriptions deleteSubscriptionsWithIDs:subscriptionsBySubscriptionID.allKeys completionHandler:^(NSArray<CKSubscription *> *savedSubscriptions, NSArray<NSString *> *deletedSubscriptionIDs, NSError *operationError) {
if (completionHandler)
completionHandler(savedSubscriptions, deletedSubscriptionIDs);
}];
[operationError log:@"fetchSubscriptionsWithIDs:"];
}];
}
- (CKFetchSubscriptionsOperation *)modifyAllSubscriptions:(NSArray<CKSubscription *> *)subscriptions {
return [self modifyAllSubscriptions:subscriptions completionHandler:Nil];
}
@end