Skip to content

Commit 83b77a0

Browse files
author
Martin Pfannemüller
committed
Support for custom objectids, implements parse-community#1611
1 parent 2e4242c commit 83b77a0

File tree

7 files changed

+85
-9
lines changed

7 files changed

+85
-9
lines changed

Parse/Parse/PFObject.m

Lines changed: 34 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,8 @@
1313
#import "PFObject+Deprecated.h"
1414
#import "PFObjectSubclassingController.h"
1515

16+
#import <Parse/Parse.h>
17+
1618
#import <objc/message.h>
1719
#import <objc/objc-sync.h>
1820
#import <objc/runtime.h>
@@ -1460,6 +1462,11 @@ - (BFTask *)saveAsync:(BFTask *)toAwait {
14601462
if (![self isDirty:YES]) {
14611463
return @YES;
14621464
}
1465+
1466+
if([Parse isAllowCustomObjectIds]){
1467+
NSError *error = [PFErrorUtilities errorWithCode:kPFErrorMissingObjectId message:@"ObjectId must not be null"];
1468+
return [BFTask taskWithError:error];
1469+
}
14631470

14641471
[self _objectWillSave];
14651472

@@ -1532,18 +1539,31 @@ - (nullable PFRESTCommand *)_constructSaveCommandForChanges:(PFOperationSet *)ch
15321539
NSDictionary *parameters = [self _convertToDictionaryForSaving:changes withObjectEncoder:encoder error:error];
15331540
PFPreconditionBailOnError(parameters, error, nil);
15341541

1535-
if (self._state.objectId) {
1536-
return [PFRESTObjectCommand updateObjectCommandForObjectState:self._state
1542+
if(!self._state.objectId){
1543+
return [PFRESTObjectCommand createObjectCommandForObjectState:self._state
15371544
changes:parameters
15381545
operationSetUUID:changes.uuid
1539-
sessionToken:sessionToken];
1546+
sessionToken:sessionToken];
1547+
}else{
1548+
if([Parse isAllowCustomObjectIds]){
1549+
if(self._state.createdAt == nil){
1550+
return [PFRESTObjectCommand createObjectCommandForObjectState:self._state
1551+
changes:parameters
1552+
operationSetUUID:changes.uuid
1553+
sessionToken:sessionToken];
1554+
}else{
1555+
return [PFRESTObjectCommand updateObjectCommandForObjectState:self._state
1556+
changes:parameters
1557+
operationSetUUID:changes.uuid
1558+
sessionToken:sessionToken];
1559+
}
1560+
}else{
1561+
return [PFRESTObjectCommand updateObjectCommandForObjectState:self._state
1562+
changes:parameters
1563+
operationSetUUID:changes.uuid
1564+
sessionToken:sessionToken];
1565+
}
15401566
}
1541-
1542-
return [PFRESTObjectCommand createObjectCommandForObjectState:self._state
1543-
changes:parameters
1544-
operationSetUUID:changes.uuid
1545-
sessionToken:sessionToken];
1546-
15471567
}
15481568
}
15491569

@@ -1792,6 +1812,11 @@ + (PFObjectState *)_newObjectStateWithParseClassName:(NSString *)className
17921812
}
17931813

17941814
- (BFTask<PFVoid> *)_validateSaveEventuallyAsync {
1815+
if([Parse isAllowCustomObjectIds]){
1816+
NSError *error = [PFErrorUtilities errorWithCode:kPFErrorMissingObjectId message:@"ObjectId must not be null"];
1817+
return [BFTask taskWithError:error];
1818+
}
1819+
17951820
return [BFTask taskWithResult:nil];
17961821
}
17971822

Parse/Parse/Parse.h

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -152,6 +152,22 @@ NS_ASSUME_NONNULL_BEGIN
152152
*/
153153
@property (nonatomic, readonly, class) BOOL isLocalDatastoreEnabled PF_TV_UNAVAILABLE;
154154

155+
///--------------------------------------
156+
#pragma mark - Allowing Custom ObjectIds
157+
///--------------------------------------
158+
159+
/**
160+
Allow custom ObjectIds in your application.`.
161+
*/
162+
+ (void)allowCustomObjectIds PF_TV_UNAVAILABLE;
163+
164+
/**
165+
Flag that indicates whether Custom ObjectIds are allowed.
166+
167+
@return `YES` if Custom ObjectIds are allowed, otherwise `NO`.
168+
*/
169+
@property (nonatomic, readonly, class) BOOL isAllowCustomObjectIds PF_TV_UNAVAILABLE;
170+
155171
///--------------------------------------
156172
#pragma mark - Enabling Extensions Data Sharing
157173
///--------------------------------------

Parse/Parse/Parse.m

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -189,6 +189,14 @@ + (BOOL)isLocalDatastoreEnabled {
189189
return currentParseManager_.offlineStoreLoaded;
190190
}
191191

192+
///--------------------------------------
193+
#pragma mark - Custom ObjectIds
194+
///--------------------------------------
195+
196+
+ (BOOL)isAllowCustomObjectIds {
197+
return currentParseConfiguration_.allowCustomObjectId;
198+
}
199+
192200
///--------------------------------------
193201
#pragma mark - User Interface
194202
///--------------------------------------

Parse/Parse/ParseClientConfiguration.h

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,17 @@ NS_ASSUME_NONNULL_BEGIN
6767
*/
6868
@property (nonatomic, assign, getter=isLocalDatastoreEnabled) BOOL localDatastoreEnabled PF_TV_UNAVAILABLE;
6969

70+
///--------------------------------------
71+
#pragma mark - Enabling Custom Object Ids
72+
///--------------------------------------
73+
74+
/**
75+
Whether or not to enable support for custom object ids in the SDK.
76+
77+
The default value is `NO`.
78+
*/
79+
@property (nonatomic, assign, getter=allowCustomObjectId) BOOL allowCustomObjectId;
80+
7081
///--------------------------------------
7182
#pragma mark - Enabling Extensions Data Sharing
7283
///--------------------------------------
@@ -148,6 +159,17 @@ NS_ASSUME_NONNULL_BEGIN
148159
*/
149160
@property (nonatomic, assign, readonly, getter=isLocalDatastoreEnabled) BOOL localDatastoreEnabled;
150161

162+
///--------------------------------------
163+
#pragma mark - Enabling Custom Object Ids
164+
///--------------------------------------
165+
166+
/**
167+
Whether or not to enable support for custom object ids in the SDK.
168+
169+
The default value is `NO`.
170+
*/
171+
@property (nonatomic, assign, readonly, getter=isAllowCustomObjectId) BOOL allowCustomObjectId;
172+
151173
///--------------------------------------
152174
#pragma mark - Enabling Extensions Data Sharing
153175
///--------------------------------------

Parse/Parse/ParseClientConfiguration.m

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -115,6 +115,7 @@ - (BOOL)isEqual:(id)object {
115115
[self.server isEqualToString:other.server] &&
116116
self.fileUploadController == other.fileUploadController &&
117117
self.localDatastoreEnabled == other.localDatastoreEnabled &&
118+
self.allowCustomObjectId == other.allowCustomObjectId &&
118119
[PFObjectUtilities isObject:self.applicationGroupIdentifier equalToObject:other.applicationGroupIdentifier] &&
119120
[PFObjectUtilities isObject:self.containingApplicationBundleIdentifier equalToObject:other.containingApplicationBundleIdentifier] &&
120121
[PFObjectUtilities isObject:self.URLSessionConfiguration equalToObject:other.URLSessionConfiguration] &&
@@ -134,6 +135,7 @@ - (instancetype)copyWithZone:(NSZone *)zone {
134135
configuration->_server = [self.server copy];
135136
configuration->_fileUploadController = self->_fileUploadController;
136137
configuration->_localDatastoreEnabled = self->_localDatastoreEnabled;
138+
configuration->_allowCustomObjectId = self->_allowCustomObjectId;
137139
configuration->_applicationGroupIdentifier = [self->_applicationGroupIdentifier copy];
138140
configuration->_containingApplicationBundleIdentifier = [self->_containingApplicationBundleIdentifier copy];
139141
configuration->_networkRetryAttempts = self->_networkRetryAttempts;

Parse/Tests/Unit/ObjectUnitTests.m

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -307,6 +307,7 @@ -(void)testSaveRelationToACycle {
307307
XCTAssertEqualObjects(error.domain, PFParseErrorDomain);
308308
XCTAssertEqualObjects(error.localizedDescription, @"Found a circular dependency when saving.");
309309
}
310+
310311
-(void)testSaveRelationToACycleInAnArray {
311312
PFObject *objectA = [PFObject objectWithClassName:@"A"];
312313
PFObject *objectB = [PFObject objectWithClassName:@"B"];

Parse/Tests/Unit/ParseClientConfigurationTests.m

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,13 +40,15 @@ - (void)testConfigurationWithBlock {
4040
configuration.server = @"http://localhost";
4141
configuration.localDatastoreEnabled = YES;
4242
configuration.networkRetryAttempts = 1337;
43+
configuration.allowCustomObjectId = YES
4344
}];
4445

4546
XCTAssertEqualObjects(configuration.applicationId, @"foo");
4647
XCTAssertEqualObjects(configuration.clientKey, @"bar");
4748
XCTAssertEqualObjects(configuration.server, @"http://localhost");
4849
XCTAssertTrue(configuration.localDatastoreEnabled);
4950
XCTAssertEqual(configuration.networkRetryAttempts, 1337);
51+
XCTAssertTrue(configuration.allowCustomObjectId);
5052
}
5153

5254
- (void)testEqual {

0 commit comments

Comments
 (0)