Skip to content

Allow mixed custom objectId environment #222

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 5 commits into from
Sep 2, 2021
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
2 changes: 1 addition & 1 deletion .codecov.yml
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ coverage:
status:
patch:
default:
target: 49
target: auto
changes: false
project:
default:
Expand Down
8 changes: 7 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,9 +1,15 @@
# Parse-Swift Changelog

### main
[Full Changelog](https://github.com/parse-community/Parse-Swift/compare/1.9.6...main)
[Full Changelog](https://github.com/parse-community/Parse-Swift/compare/1.9.7...main)
* _Contributing to this repo? Add info about your change here to be included in the next release_

### 1.9.7
[Full Changelog](https://github.com/parse-community/Parse-Swift/compare/1.9.6...1.9.7)

__Improvements__
- Properly allow a mixed custom objectId environment without compromising safety checks using .save(). If a developer wants to ignore the objectId checks, they need to specify isIgnoreCustomObjectIdConfig = true each time ([#222](https://github.com/parse-community/Parse-Swift/pull/222)), thanks to [Corey Baker](https://github.com/cbaker6).

### 1.9.6
[Full Changelog](https://github.com/parse-community/Parse-Swift/compare/1.9.5...1.9.6)

Expand Down
5 changes: 3 additions & 2 deletions Sources/ParseSwift/API/API+Commands.swift
Original file line number Diff line number Diff line change
Expand Up @@ -344,8 +344,9 @@ internal extension API.Command {
}

// MARK: Saving ParseObjects
static func save<T>(_ object: T) throws -> API.Command<T, T> where T: ParseObject {
if ParseSwift.configuration.allowCustomObjectId && object.objectId == nil {
static func save<T>(_ object: T,
isIgnoreCustomObjectIdConfig: Bool) throws -> API.Command<T, T> where T: ParseObject {
if ParseSwift.configuration.allowCustomObjectId && object.objectId == nil && !isIgnoreCustomObjectIdConfig {
throw ParseError(code: .missingObjectId, message: "objectId must not be nil")
}
if object.isSaved {
Expand Down
8 changes: 6 additions & 2 deletions Sources/ParseSwift/Objects/ParseInstallation+combine.swift
Original file line number Diff line number Diff line change
Expand Up @@ -42,9 +42,11 @@ public extension ParseInstallation {
- returns: A publisher that eventually produces a single value and then finishes or fails.
- important: If an object saved has the same objectId as current, it will automatically update the current.
*/
func savePublisher(options: API.Options = []) -> Future<Self, ParseError> {
func savePublisher(isIgnoreCustomObjectIdConfig: Bool = false,
options: API.Options = []) -> Future<Self, ParseError> {
Future { promise in
self.save(options: options,
self.save(isIgnoreCustomObjectIdConfig: isIgnoreCustomObjectIdConfig,
options: options,
completion: promise)
}
}
Expand Down Expand Up @@ -102,10 +104,12 @@ public extension Sequence where Element: ParseInstallation {
*/
func saveAllPublisher(batchLimit limit: Int? = nil,
transaction: Bool = false,
isIgnoreCustomObjectIdConfig: Bool = false,
options: API.Options = []) -> Future<[(Result<Self.Element, ParseError>)], ParseError> {
Future { promise in
self.saveAll(batchLimit: limit,
transaction: transaction,
isIgnoreCustomObjectIdConfig: isIgnoreCustomObjectIdConfig,
options: options,
completion: promise)
}
Expand Down
81 changes: 75 additions & 6 deletions Sources/ParseSwift/Objects/ParseInstallation.swift
Original file line number Diff line number Diff line change
Expand Up @@ -426,6 +426,32 @@ extension ParseInstallation {
- important: If an object saved has the same objectId as current, it will automatically update the current.
*/
public func save(options: API.Options = []) throws -> Self {
try save(isIgnoreCustomObjectIdConfig: false,
options: options)
}

/**
Saves the `ParseInstallation` *synchronously* and throws an error if there's an issue.

- parameter isIgnoreCustomObjectIdConfig: Ignore checking for `objectId`
when `ParseConfiguration.allowCustomObjectId = true` to allow for mixed
`objectId` environments. Defaults to false.
- parameter options: A set of header options sent to the server. Defaults to an empty set.
- throws: An error of type `ParseError`.
- returns: Returns saved `ParseInstallation`.
- important: If an object saved has the same objectId as current, it will automatically update the current.
- warning: If you are using `ParseConfiguration.allowCustomObjectId = true`
and plan to generate all of your `objectId`'s on the client-side then you should leave
`isIgnoreCustomObjectIdConfig = false`. Setting
`ParseConfiguration.allowCustomObjectId = true` and
`isIgnoreCustomObjectIdConfig = true` means the client will generate `objectId`'s
and the server will generate an `objectId` only when the client does not provide one. This can
increase the probability of colliding `objectId`'s as the client and server `objectId`'s may be generated using
different algorithms. This can also lead to overwriting of `ParseObject`'s by accident as the
client-side checks are disabled. Developers are responsible for handling such cases.
*/
public func save(isIgnoreCustomObjectIdConfig: Bool,
options: API.Options = []) throws -> Self {
var options = options
options.insert(.cachePolicy(.reloadIgnoringLocalCacheData))
var childObjects: [String: PointerType]?
Expand All @@ -445,7 +471,7 @@ extension ParseInstallation {
throw error
}

let result: Self = try saveCommand()
let result: Self = try saveCommand(isIgnoreCustomObjectIdConfig: isIgnoreCustomObjectIdConfig)
.execute(options: options,
callbackQueue: .main,
childObjects: childObjects,
Expand All @@ -457,13 +483,26 @@ extension ParseInstallation {
/**
Saves the `ParseInstallation` *asynchronously* and executes the given callback block.

- parameter isIgnoreCustomObjectIdConfig: Ignore checking for `objectId`
when `ParseConfiguration.allowCustomObjectId = true` to allow for mixed
`objectId` environments. Defaults to false.
- parameter options: A set of header options sent to the server. Defaults to an empty set.
- parameter callbackQueue: The queue to return to after completion. Default value of .main.
- parameter completion: The block to execute.
It should have the following argument signature: `(Result<Self, ParseError>)`.
- important: If an object saved has the same objectId as current, it will automatically update the current.
- warning: If you are using `ParseConfiguration.allowCustomObjectId = true`
and plan to generate all of your `objectId`'s on the client-side then you should leave
`isIgnoreCustomObjectIdConfig = false`. Setting
`ParseConfiguration.allowCustomObjectId = true` and
`isIgnoreCustomObjectIdConfig = true` means the client will generate `objectId`'s
and the server will generate an `objectId` only when the client does not provide one. This can
increase the probability of colliding `objectId`'s as the client and server `objectId`'s may be generated using
different algorithms. This can also lead to overwriting of `ParseObject`'s by accident as the
client-side checks are disabled. Developers are responsible for handling such cases.
*/
public func save(
isIgnoreCustomObjectIdConfig: Bool = false,
options: API.Options = [],
callbackQueue: DispatchQueue = .main,
completion: @escaping (Result<Self, ParseError>) -> Void
Expand All @@ -473,7 +512,7 @@ extension ParseInstallation {
self.ensureDeepSave(options: options) { (savedChildObjects, savedChildFiles, error) in
guard let parseError = error else {
do {
try self.saveCommand()
try self.saveCommand(isIgnoreCustomObjectIdConfig: isIgnoreCustomObjectIdConfig)
.executeAsync(options: options,
callbackQueue: callbackQueue,
childObjects: savedChildObjects,
Expand Down Expand Up @@ -515,8 +554,8 @@ extension ParseInstallation {
}
}

func saveCommand() throws -> API.Command<Self, Self> {
if ParseSwift.configuration.allowCustomObjectId && objectId == nil {
func saveCommand(isIgnoreCustomObjectIdConfig: Bool = false) throws -> API.Command<Self, Self> {
if ParseSwift.configuration.allowCustomObjectId && objectId == nil && !isIgnoreCustomObjectIdConfig {
throw ParseError(code: .missingObjectId, message: "objectId must not be nil")
}
if isSaved {
Expand Down Expand Up @@ -643,6 +682,9 @@ public extension Sequence where Element: ParseInstallation {
- parameter batchLimit: The maximum number of objects to send in each batch. If the items to be batched.
is greater than the `batchLimit`, the objects will be sent to the server in waves up to the `batchLimit`.
Defaults to 50.
- parameter isIgnoreCustomObjectIdConfig: Ignore checking for `objectId`
when `ParseConfiguration.allowCustomObjectId = true` to allow for mixed
`objectId` environments. Defaults to false.
- parameter options: A set of header options sent to the server. Defaults to an empty set.
- parameter transaction: Treat as an all-or-nothing operation. If some operation failure occurs that
prevents the transaction from completing, then none of the objects are committed to the Parse Server database.
Expand All @@ -653,9 +695,19 @@ public extension Sequence where Element: ParseInstallation {
- warning: If `transaction = true`, then `batchLimit` will be automatically be set to the amount of the
objects in the transaction. The developer should ensure their respective Parse Servers can handle the limit or else
the transactions can fail.
- warning: If you are using `ParseConfiguration.allowCustomObjectId = true`
and plan to generate all of your `objectId`'s on the client-side then you should leave
`isIgnoreCustomObjectIdConfig = false`. Setting
`ParseConfiguration.allowCustomObjectId = true` and
`isIgnoreCustomObjectIdConfig = true` means the client will generate `objectId`'s
and the server will generate an `objectId` only when the client does not provide one. This can
increase the probability of colliding `objectId`'s as the client and server `objectId`'s may be generated using
different algorithms. This can also lead to overwriting of `ParseObject`'s by accident as the
client-side checks are disabled. Developers are responsible for handling such cases.
*/
func saveAll(batchLimit limit: Int? = nil, // swiftlint:disable:this function_body_length
transaction: Bool = false,
isIgnoreCustomObjectIdConfig: Bool = false,
options: API.Options = []) throws -> [(Result<Self.Element, ParseError>)] {
var options = options
options.insert(.cachePolicy(.reloadIgnoringLocalCacheData))
Expand Down Expand Up @@ -703,7 +755,9 @@ public extension Sequence where Element: ParseInstallation {
}

var returnBatch = [(Result<Self.Element, ParseError>)]()
let commands = try map { try $0.saveCommand() }
let commands = try map {
try $0.saveCommand(isIgnoreCustomObjectIdConfig: isIgnoreCustomObjectIdConfig)
}
let batchLimit: Int!
if transaction {
batchLimit = commands.count
Expand Down Expand Up @@ -731,6 +785,9 @@ public extension Sequence where Element: ParseInstallation {
Defaults to 50.
- parameter transaction: Treat as an all-or-nothing operation. If some operation failure occurs that
prevents the transaction from completing, then none of the objects are committed to the Parse Server database.
- parameter isIgnoreCustomObjectIdConfig: Ignore checking for `objectId`
when `ParseConfiguration.allowCustomObjectId = true` to allow for mixed
`objectId` environments. Defaults to false.
- parameter options: A set of header options sent to the server. Defaults to an empty set.
- parameter callbackQueue: The queue to return to after completion. Default value of .main.
- parameter completion: The block to execute.
Expand All @@ -739,10 +796,20 @@ public extension Sequence where Element: ParseInstallation {
- warning: If `transaction = true`, then `batchLimit` will be automatically be set to the amount of the
objects in the transaction. The developer should ensure their respective Parse Servers can handle the limit or else
the transactions can fail.
- warning: If you are using `ParseConfiguration.allowCustomObjectId = true`
and plan to generate all of your `objectId`'s on the client-side then you should leave
`isIgnoreCustomObjectIdConfig = false`. Setting
`ParseConfiguration.allowCustomObjectId = true` and
`isIgnoreCustomObjectIdConfig = true` means the client will generate `objectId`'s
and the server will generate an `objectId` only when the client does not provide one. This can
increase the probability of colliding `objectId`'s as the client and server `objectId`'s may be generated using
different algorithms. This can also lead to overwriting of `ParseObject`'s by accident as the
client-side checks are disabled. Developers are responsible for handling such cases.
*/
func saveAll( // swiftlint:disable:this function_body_length cyclomatic_complexity
batchLimit limit: Int? = nil,
transaction: Bool = false,
isIgnoreCustomObjectIdConfig: Bool = false,
options: API.Options = [],
callbackQueue: DispatchQueue = .main,
completion: @escaping (Result<[(Result<Element, ParseError>)], ParseError>) -> Void
Expand Down Expand Up @@ -805,7 +872,9 @@ public extension Sequence where Element: ParseInstallation {

do {
var returnBatch = [(Result<Self.Element, ParseError>)]()
let commands = try map { try $0.saveCommand() }
let commands = try map {
try $0.saveCommand(isIgnoreCustomObjectIdConfig: isIgnoreCustomObjectIdConfig)
}
let batchLimit: Int!
if transaction {
batchLimit = commands.count
Expand Down
20 changes: 18 additions & 2 deletions Sources/ParseSwift/Objects/ParseObject+combine.swift
Original file line number Diff line number Diff line change
Expand Up @@ -39,9 +39,11 @@ public extension ParseObject {
- returns: A publisher that eventually produces a single value and then finishes or fails.
- important: If an object saved has the same objectId as current, it will automatically update the current.
*/
func savePublisher(options: API.Options = []) -> Future<Self, ParseError> {
func savePublisher(isIgnoreCustomObjectIdConfig: Bool = false,
options: API.Options = []) -> Future<Self, ParseError> {
Future { promise in
self.save(options: options,
self.save(isIgnoreCustomObjectIdConfig: isIgnoreCustomObjectIdConfig,
options: options,
completion: promise)
}
}
Expand Down Expand Up @@ -89,19 +91,33 @@ public extension Sequence where Element: ParseObject {
Defaults to 50.
- parameter transaction: Treat as an all-or-nothing operation. If some operation failure occurs that
prevents the transaction from completing, then none of the objects are committed to the Parse Server database.
- parameter isIgnoreCustomObjectIdConfig: Ignore checking for `objectId`
when `ParseConfiguration.allowCustomObjectId = true` to allow for mixed
`objectId` environments. Defaults to false.
- parameter options: A set of header options sent to the server. Defaults to an empty set.
- returns: A publisher that eventually produces a single value and then finishes or fails.
- important: If an object saved has the same objectId as current, it will automatically update the current.
- warning: If `transaction = true`, then `batchLimit` will be automatically be set to the amount of the
objects in the transaction. The developer should ensure their respective Parse Servers can handle the limit or else
the transactions can fail.
- warning: If you are using `ParseConfiguration.allowCustomObjectId = true`
and plan to generate all of your `objectId`'s on the client-side then you should leave
`isIgnoreCustomObjectIdConfig = false`. Setting
`ParseConfiguration.allowCustomObjectId = true` and
`isIgnoreCustomObjectIdConfig = true` means the client will generate `objectId`'s
and the server will generate an `objectId` only when the client does not provide one. This can
increase the probability of colliiding `objectId`'s as the client and server `objectId`'s may be generated using
different algorithms. This can also lead to overwriting of `ParseObject`'s by accident as the
client-side checks are disabled. Developers are responsible for handling such cases.
*/
func saveAllPublisher(batchLimit limit: Int? = nil,
transaction: Bool = false,
isIgnoreCustomObjectIdConfig: Bool = false,
options: API.Options = []) -> Future<[(Result<Self.Element, ParseError>)], ParseError> {
Future { promise in
self.saveAll(batchLimit: limit,
transaction: transaction,
isIgnoreCustomObjectIdConfig: isIgnoreCustomObjectIdConfig,
options: options,
completion: promise)
}
Expand Down
Loading