Skip to content

Commit

Permalink
Support for custom objectids, implements parse-community#1611
Browse files Browse the repository at this point in the history
  • Loading branch information
Martin Pfannemüller committed May 24, 2021
1 parent 2e4242c commit 83b77a0
Show file tree
Hide file tree
Showing 7 changed files with 85 additions and 9 deletions.
43 changes: 34 additions & 9 deletions Parse/Parse/PFObject.m
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@
#import "PFObject+Deprecated.h"
#import "PFObjectSubclassingController.h"

#import <Parse/Parse.h>

#import <objc/message.h>
#import <objc/objc-sync.h>
#import <objc/runtime.h>
Expand Down Expand Up @@ -1460,6 +1462,11 @@ - (BFTask *)saveAsync:(BFTask *)toAwait {
if (![self isDirty:YES]) {
return @YES;
}

if([Parse isAllowCustomObjectIds]){
NSError *error = [PFErrorUtilities errorWithCode:kPFErrorMissingObjectId message:@"ObjectId must not be null"];
return [BFTask taskWithError:error];
}

[self _objectWillSave];

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

if (self._state.objectId) {
return [PFRESTObjectCommand updateObjectCommandForObjectState:self._state
if(!self._state.objectId){
return [PFRESTObjectCommand createObjectCommandForObjectState:self._state
changes:parameters
operationSetUUID:changes.uuid
sessionToken:sessionToken];
sessionToken:sessionToken];
}else{
if([Parse isAllowCustomObjectIds]){
if(self._state.createdAt == nil){
return [PFRESTObjectCommand createObjectCommandForObjectState:self._state
changes:parameters
operationSetUUID:changes.uuid
sessionToken:sessionToken];
}else{
return [PFRESTObjectCommand updateObjectCommandForObjectState:self._state
changes:parameters
operationSetUUID:changes.uuid
sessionToken:sessionToken];
}
}else{
return [PFRESTObjectCommand updateObjectCommandForObjectState:self._state
changes:parameters
operationSetUUID:changes.uuid
sessionToken:sessionToken];
}
}

return [PFRESTObjectCommand createObjectCommandForObjectState:self._state
changes:parameters
operationSetUUID:changes.uuid
sessionToken:sessionToken];

}
}

Expand Down Expand Up @@ -1792,6 +1812,11 @@ + (PFObjectState *)_newObjectStateWithParseClassName:(NSString *)className
}

- (BFTask<PFVoid> *)_validateSaveEventuallyAsync {
if([Parse isAllowCustomObjectIds]){
NSError *error = [PFErrorUtilities errorWithCode:kPFErrorMissingObjectId message:@"ObjectId must not be null"];
return [BFTask taskWithError:error];
}

return [BFTask taskWithResult:nil];
}

Expand Down
16 changes: 16 additions & 0 deletions Parse/Parse/Parse.h
Original file line number Diff line number Diff line change
Expand Up @@ -152,6 +152,22 @@ NS_ASSUME_NONNULL_BEGIN
*/
@property (nonatomic, readonly, class) BOOL isLocalDatastoreEnabled PF_TV_UNAVAILABLE;

///--------------------------------------
#pragma mark - Allowing Custom ObjectIds
///--------------------------------------

/**
Allow custom ObjectIds in your application.`.
*/
+ (void)allowCustomObjectIds PF_TV_UNAVAILABLE;

/**
Flag that indicates whether Custom ObjectIds are allowed.
@return `YES` if Custom ObjectIds are allowed, otherwise `NO`.
*/
@property (nonatomic, readonly, class) BOOL isAllowCustomObjectIds PF_TV_UNAVAILABLE;

///--------------------------------------
#pragma mark - Enabling Extensions Data Sharing
///--------------------------------------
Expand Down
8 changes: 8 additions & 0 deletions Parse/Parse/Parse.m
Original file line number Diff line number Diff line change
Expand Up @@ -189,6 +189,14 @@ + (BOOL)isLocalDatastoreEnabled {
return currentParseManager_.offlineStoreLoaded;
}

///--------------------------------------
#pragma mark - Custom ObjectIds
///--------------------------------------

+ (BOOL)isAllowCustomObjectIds {
return currentParseConfiguration_.allowCustomObjectId;
}

///--------------------------------------
#pragma mark - User Interface
///--------------------------------------
Expand Down
22 changes: 22 additions & 0 deletions Parse/Parse/ParseClientConfiguration.h
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,17 @@ NS_ASSUME_NONNULL_BEGIN
*/
@property (nonatomic, assign, getter=isLocalDatastoreEnabled) BOOL localDatastoreEnabled PF_TV_UNAVAILABLE;

///--------------------------------------
#pragma mark - Enabling Custom Object Ids
///--------------------------------------

/**
Whether or not to enable support for custom object ids in the SDK.
The default value is `NO`.
*/
@property (nonatomic, assign, getter=allowCustomObjectId) BOOL allowCustomObjectId;

///--------------------------------------
#pragma mark - Enabling Extensions Data Sharing
///--------------------------------------
Expand Down Expand Up @@ -148,6 +159,17 @@ NS_ASSUME_NONNULL_BEGIN
*/
@property (nonatomic, assign, readonly, getter=isLocalDatastoreEnabled) BOOL localDatastoreEnabled;

///--------------------------------------
#pragma mark - Enabling Custom Object Ids
///--------------------------------------

/**
Whether or not to enable support for custom object ids in the SDK.
The default value is `NO`.
*/
@property (nonatomic, assign, readonly, getter=isAllowCustomObjectId) BOOL allowCustomObjectId;

///--------------------------------------
#pragma mark - Enabling Extensions Data Sharing
///--------------------------------------
Expand Down
2 changes: 2 additions & 0 deletions Parse/Parse/ParseClientConfiguration.m
Original file line number Diff line number Diff line change
Expand Up @@ -115,6 +115,7 @@ - (BOOL)isEqual:(id)object {
[self.server isEqualToString:other.server] &&
self.fileUploadController == other.fileUploadController &&
self.localDatastoreEnabled == other.localDatastoreEnabled &&
self.allowCustomObjectId == other.allowCustomObjectId &&
[PFObjectUtilities isObject:self.applicationGroupIdentifier equalToObject:other.applicationGroupIdentifier] &&
[PFObjectUtilities isObject:self.containingApplicationBundleIdentifier equalToObject:other.containingApplicationBundleIdentifier] &&
[PFObjectUtilities isObject:self.URLSessionConfiguration equalToObject:other.URLSessionConfiguration] &&
Expand All @@ -134,6 +135,7 @@ - (instancetype)copyWithZone:(NSZone *)zone {
configuration->_server = [self.server copy];
configuration->_fileUploadController = self->_fileUploadController;
configuration->_localDatastoreEnabled = self->_localDatastoreEnabled;
configuration->_allowCustomObjectId = self->_allowCustomObjectId;
configuration->_applicationGroupIdentifier = [self->_applicationGroupIdentifier copy];
configuration->_containingApplicationBundleIdentifier = [self->_containingApplicationBundleIdentifier copy];
configuration->_networkRetryAttempts = self->_networkRetryAttempts;
Expand Down
1 change: 1 addition & 0 deletions Parse/Tests/Unit/ObjectUnitTests.m
Original file line number Diff line number Diff line change
Expand Up @@ -307,6 +307,7 @@ -(void)testSaveRelationToACycle {
XCTAssertEqualObjects(error.domain, PFParseErrorDomain);
XCTAssertEqualObjects(error.localizedDescription, @"Found a circular dependency when saving.");
}

-(void)testSaveRelationToACycleInAnArray {
PFObject *objectA = [PFObject objectWithClassName:@"A"];
PFObject *objectB = [PFObject objectWithClassName:@"B"];
Expand Down
2 changes: 2 additions & 0 deletions Parse/Tests/Unit/ParseClientConfigurationTests.m
Original file line number Diff line number Diff line change
Expand Up @@ -40,13 +40,15 @@ - (void)testConfigurationWithBlock {
configuration.server = @"http://localhost";
configuration.localDatastoreEnabled = YES;
configuration.networkRetryAttempts = 1337;
configuration.allowCustomObjectId = YES
}];

XCTAssertEqualObjects(configuration.applicationId, @"foo");
XCTAssertEqualObjects(configuration.clientKey, @"bar");
XCTAssertEqualObjects(configuration.server, @"http://localhost");
XCTAssertTrue(configuration.localDatastoreEnabled);
XCTAssertEqual(configuration.networkRetryAttempts, 1337);
XCTAssertTrue(configuration.allowCustomObjectId);
}

- (void)testEqual {
Expand Down

0 comments on commit 83b77a0

Please sign in to comment.