1 ////////////////////////////////////////////////////////////////////////////
3 // Copyright 2016 Realm Inc.
5 // Licensed under the Apache License, Version 2.0 (the "License");
6 // you may not use this file except in compliance with the License.
7 // You may obtain a copy of the License at
9 // http://www.apache.org/licenses/LICENSE-2.0
11 // Unless required by applicable law or agreed to in writing, software
12 // distributed under the License is distributed on an "AS IS" BASIS,
13 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 // See the License for the specific language governing permissions and
15 // limitations under the License.
17 ////////////////////////////////////////////////////////////////////////////
19 #import <Realm/RLMSyncUtil.h>
21 #import <Realm/RLMProperty.h>
22 #import <Realm/RLMRealmConfiguration.h>
23 #import <Realm/RLMSyncCredentials.h>
25 typedef NS_ENUM(NSUInteger, RLMSyncSystemErrorKind) {
27 RLMSyncSystemErrorKindClientReset,
28 RLMSyncSystemErrorKindPermissionDenied,
30 RLMSyncSystemErrorKindClient,
31 RLMSyncSystemErrorKindConnection,
32 RLMSyncSystemErrorKindSession,
33 RLMSyncSystemErrorKindUser,
34 RLMSyncSystemErrorKindUnknown,
39 typedef void(^RLMSyncCompletionBlock)(NSError * _Nullable, NSDictionary * _Nullable);
40 typedef void(^RLMSyncBasicErrorReportingBlock)(NSError * _Nullable);
42 typedef NSString* RLMServerPath;
44 NS_ASSUME_NONNULL_BEGIN
46 extern RLMIdentityProvider const RLMIdentityProviderAccessToken;
47 extern RLMIdentityProvider const RLMIdentityProviderRealm;
49 extern NSString *const kRLMSyncAppIDKey;
50 extern NSString *const kRLMSyncDataKey;
51 extern NSString *const kRLMSyncErrorJSONKey;
52 extern NSString *const kRLMSyncErrorStatusCodeKey;
53 extern NSString *const kRLMSyncIdentityKey;
54 extern NSString *const kRLMSyncIsAdminKey;
55 extern NSString *const kRLMSyncNewPasswordKey;
56 extern NSString *const kRLMSyncPasswordKey;
57 extern NSString *const kRLMSyncPathKey;
58 extern NSString *const kRLMSyncTokenKey;
59 extern NSString *const kRLMSyncProviderKey;
60 extern NSString *const kRLMSyncProviderIDKey;
61 extern NSString *const kRLMSyncRegisterKey;
62 extern NSString *const kRLMSyncUnderlyingErrorKey;
63 extern NSString *const kRLMSyncUserIDKey;
65 #define RLM_SYNC_UNINITIALIZABLE \
66 - (instancetype)init __attribute__((unavailable("This type cannot be created directly"))); \
67 + (instancetype)new __attribute__((unavailable("This type cannot be created directly")));
71 /// A macro to parse a string out of a JSON dictionary, or return nil.
72 #define RLM_SYNC_PARSE_STRING_OR_ABORT(json_macro_val, key_macro_val, prop_macro_val) \
74 id data = json_macro_val[key_macro_val]; \
75 if (![data isKindOfClass:[NSString class]]) { return nil; } \
76 self.prop_macro_val = data; \
79 #define RLM_SYNC_PARSE_OPTIONAL_STRING(json_macro_val, key_macro_val, prop_macro_val) \
81 id data = json_macro_val[key_macro_val]; \
82 if (![data isKindOfClass:[NSString class]]) { data = nil; } \
83 self.prop_macro_val = data; \
86 #define RLM_SYNC_PARSE_OPTIONAL_BOOL(json_macro_val, key_macro_val, prop_macro_val) \
88 id data = json_macro_val[key_macro_val]; \
89 if (![data isKindOfClass:[NSNumber class]]) { data = @NO; } \
90 self.prop_macro_val = [data boolValue]; \
93 /// A macro to parse a double out of a JSON dictionary, or return nil.
94 #define RLM_SYNC_PARSE_DOUBLE_OR_ABORT(json_macro_val, key_macro_val, prop_macro_val) \
96 id data = json_macro_val[key_macro_val]; \
97 if (![data isKindOfClass:[NSNumber class]]) { return nil; } \
98 self.prop_macro_val = [data doubleValue]; \
101 /// A macro to build a sub-model out of a JSON dictionary, or return nil.
102 #define RLM_SYNC_PARSE_MODEL_OR_ABORT(json_macro_val, key_macro_val, class_macro_val, prop_macro_val) \
104 id raw = json_macro_val[key_macro_val]; \
105 if (![raw isKindOfClass:[NSDictionary class]]) { return nil; } \
106 id model = [[class_macro_val alloc] initWithDictionary:raw]; \
107 if (!model) { return nil; } \
108 self.prop_macro_val = model; \
111 /// A macro to build an array of sub-models out of a JSON dictionary, or return nil.
112 #define RLM_SYNC_PARSE_MODEL_ARRAY_OR_ABORT(json_macro_val, key_macro_val, class_macro_val, prop_macro_val) \
114 NSArray *jsonArray = json_macro_val[key_macro_val]; \
115 if (![jsonArray isKindOfClass:[NSArray class]]) { return nil; } \
116 NSMutableArray *buffer = [NSMutableArray array]; \
117 for (id value in jsonArray) { \
119 if ([value isKindOfClass:[NSDictionary class]]) { next = [[class_macro_val alloc] initWithDictionary:value]; } \
120 if (!next) { return nil; } \
121 [buffer addObject:next]; \
123 self.prop_macro_val = [buffer copy]; \
126 #define RLM_SYNC_PARSE_OPTIONAL_MODEL(json_macro_val, key_macro_val, class_macro_val, prop_macro_val) \
129 id raw = json_macro_val[key_macro_val]; \
130 if (![raw isKindOfClass:[NSDictionary class]]) { model = nil; } \
131 else { model = [[class_macro_val alloc] initWithDictionary:raw]; } \
132 self.prop_macro_val = model; \