Skip to content

fix: Parse.setServer does not set new server URL #1708

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 4 commits into from
Feb 20, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 0 additions & 3 deletions Parse/Parse/Internal/PFInternalUtils.h
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,6 @@

@interface PFInternalUtils : NSObject

+ (NSString *)parseServerURLString;
+ (void)setParseServer:(NSString *)server;

/**
Clears system time zone cache, gets the name of the time zone
and caches it. This method is completely thread-safe.
Expand Down
19 changes: 0 additions & 19 deletions Parse/Parse/Internal/PFInternalUtils.m
Original file line number Diff line number Diff line change
Expand Up @@ -40,27 +40,8 @@
#import "PFProduct.h"
#endif

static NSString *parseServer_;

@implementation PFInternalUtils

+ (void)initialize {
if (self == [PFInternalUtils class]) {
[self setParseServer:_ParseDefaultServerURLString];
}
}

+ (NSString *)parseServerURLString {
return parseServer_;
}

// Useful for testing.
// Beware of race conditions if you call setParseServer while something else may be using
// httpClient.
+ (void)setParseServer:(NSString *)server {
parseServer_ = [server copy];
}

+ (NSString *)currentSystemTimeZoneName {
static NSLock *methodLock;
static dispatch_once_t onceToken;
Expand Down
12 changes: 7 additions & 5 deletions Parse/Parse/Source/Parse.h
Original file line number Diff line number Diff line change
Expand Up @@ -107,11 +107,13 @@ NS_ASSUME_NONNULL_BEGIN
@property (nonatomic, nullable, readonly, class) ParseClientConfiguration *currentConfiguration;

/**
Sets the server URL to connect to Parse Server. The local client cache is not cleared.
@discussion This can be used to update the server URL after this client has been initialized, without having to destroy this client. An example use case is
server connection failover, where the clients connects to another URL if the server becomes unreachable at the current URL.
@warning The new server URL must point to a Parse Server that connects to the same database. Otherwise, issues may arise
related to locally cached data or delayed methods such as saveEventually.
Sets the server URL to connect to Parse Server.
@discussion This can be used to update the server URL after the client was initialized. An example use case is server
connection failover, where the client connects to another URL if the server becomes unreachable at the current URL. The
client will be re-initialized maintaining the same configuration. Any pending requests will still be made against the previous
server URL that was set at the time the request was queued.
@warning The new server URL must point to a Parse Server that connects to the same database. Otherwise, issues may
arise related to locally cached data or delayed methods such as saveEventually.
@param server The server URL to set.
*/
+ (void)setServer:(nonnull NSString *)server;
Expand Down
19 changes: 14 additions & 5 deletions Parse/Parse/Source/Parse.m
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,6 @@ + (void)setApplicationId:(NSString *)applicationId clientKey:(NSString *)clientK
PFParameterAssert(clientKey.length, @"`clientKey` should not be nil.");
currentParseConfiguration_.applicationId = applicationId;
currentParseConfiguration_.clientKey = clientKey;
currentParseConfiguration_.server = [PFInternalUtils parseServerURLString]; // TODO: (nlutsenko) Clean this up after tests are updated.

[self initializeWithConfiguration:currentParseConfiguration_];

Expand All @@ -69,13 +68,17 @@ + (void)setApplicationId:(NSString *)applicationId clientKey:(NSString *)clientK
}

+ (void)initializeWithConfiguration:(ParseClientConfiguration *)configuration {
PFConsistencyAssert(![self currentConfiguration], @"Parse is already initialized.");
[self initializeWithConfigurationAllowingReinitialize:configuration];
}

+ (void)initializeWithConfigurationAllowingReinitialize:(ParseClientConfiguration *)configuration {
PFConsistencyAssert(configuration.applicationId.length != 0,
@"You must set your configuration's `applicationId` before calling %s!", __PRETTY_FUNCTION__);
PFConsistencyAssert(![PFApplication currentApplication].extensionEnvironment ||
configuration.applicationGroupIdentifier == nil ||
configuration.containingApplicationBundleIdentifier != nil,
@"'containingApplicationBundleIdentifier' must be non-nil in extension environment");
PFConsistencyAssert(![self currentConfiguration], @"Parse is already initialized.");

ParseManager *manager = [[ParseManager alloc] initWithConfiguration:configuration];
[manager startManaging];
Expand All @@ -94,11 +97,15 @@ + (void)initializeWithConfiguration:(ParseClientConfiguration *)configuration {
object:nil];
return nil;
}];

}

+ (void)setServer:(nonnull NSString *)server {
[PFInternalUtils setParseServer:server];
// Use current config with new server
ParseClientConfiguration *config = currentParseManager_ ? currentParseManager_.configuration : currentParseConfiguration_;
config.server = server;

// Re-initialize SDK
[self initializeWithConfigurationAllowingReinitialize:config];
}

+ (nullable ParseClientConfiguration *)currentConfiguration {
Expand Down Expand Up @@ -126,7 +133,9 @@ + (nullable NSString *)getClientKey {
}

+ (nullable NSString *)server {
return [[PFInternalUtils parseServerURLString] copy];
ParseClientConfiguration *config = currentParseManager_ ? currentParseManager_.configuration
: currentParseConfiguration_;
return currentParseManager_.configuration.server;
}

///--------------------------------------
Expand Down