Skip to content

Fix saving ParseObjects with Pointers as properties #169

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 6 commits into from
Jun 19, 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
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.8.2...main)
[Full Changelog](https://github.com/parse-community/Parse-Swift/compare/1.8.3...main)
* _Contributing to this repo? Add info about your change here to be included in the next release_

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

__Fixes__
- Fixed a bug that prevented saving ParseObjects that had Pointers as properties ([#169](https://github.com/parse-community/Parse-Swift/pull/169)), thanks to [Corey Baker](https://github.com/cbaker6).

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

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ struct Book: ParseObject {
var createdAt: Date?
var updatedAt: Date?
var ACL: ParseACL?
var relatedBook: Pointer<Book>?

//: Your own properties.
var title: String?
Expand Down Expand Up @@ -78,7 +79,9 @@ author2.save { result in
assert(savedAuthorAndBook.ACL == nil)
assert(savedAuthorAndBook.otherBooks?.count == 2)

//: Notice the pointer objects haven't been updated on the client.
print("Saved \(savedAuthorAndBook)")

case .failure(let error):
assertionFailure("Error saving: \(error)")
}
Expand Down Expand Up @@ -160,5 +163,40 @@ do {
print("\(error)")
}

//: Here's an example of saving Pointers as properties
do {
// First we query
let query5 = try Author.query("book" == newBook)
.include("book")

query5.first { results in
switch results {
case .success(let author):
print("Found author and included all: \(author)")
//: Setup related books.
newBook.relatedBook = try? author.otherBooks?.first?.toPointer()

newBook.save { result in
switch result {
case .success(let updatedBook):
assert(updatedBook.objectId != nil)
assert(updatedBook.createdAt != nil)
assert(updatedBook.updatedAt != nil)
assert(updatedBook.ACL == nil)
assert(updatedBook.relatedBook != nil)

print("Saved \(updatedBook)")
case .failure(let error):
assertionFailure("Error saving: \(error)")
}
}
case .failure(let error):
assertionFailure("Error querying: \(error)")
}
}
} catch {
print("\(error)")
}

PlaygroundPage.current.finishExecution()
//: [Next](@next)
2 changes: 1 addition & 1 deletion ParseSwift.podspec
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
Pod::Spec.new do |s|
s.name = "ParseSwift"
s.version = "1.8.2"
s.version = "1.8.3"
s.summary = "Parse Pure Swift SDK"
s.homepage = "https://github.com/parse-community/Parse-Swift"
s.authors = {
Expand Down
16 changes: 8 additions & 8 deletions ParseSwift.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -2433,7 +2433,7 @@
INSTALL_PATH = "$(LOCAL_LIBRARY_DIR)/Frameworks";
IPHONEOS_DEPLOYMENT_TARGET = 12.0;
LD_RUNPATH_SEARCH_PATHS = "$(inherited) @executable_path/Frameworks @loader_path/Frameworks";
MARKETING_VERSION = 1.8.2;
MARKETING_VERSION = 1.8.3;
PRODUCT_BUNDLE_IDENTIFIER = com.parse.ParseSwift;
PRODUCT_NAME = ParseSwift;
SKIP_INSTALL = YES;
Expand All @@ -2457,7 +2457,7 @@
INSTALL_PATH = "$(LOCAL_LIBRARY_DIR)/Frameworks";
IPHONEOS_DEPLOYMENT_TARGET = 12.0;
LD_RUNPATH_SEARCH_PATHS = "$(inherited) @executable_path/Frameworks @loader_path/Frameworks";
MARKETING_VERSION = 1.8.2;
MARKETING_VERSION = 1.8.3;
PRODUCT_BUNDLE_IDENTIFIER = com.parse.ParseSwift;
PRODUCT_NAME = ParseSwift;
SKIP_INSTALL = YES;
Expand Down Expand Up @@ -2523,7 +2523,7 @@
INSTALL_PATH = "$(LOCAL_LIBRARY_DIR)/Frameworks";
LD_RUNPATH_SEARCH_PATHS = "$(inherited) @executable_path/../Frameworks @loader_path/Frameworks";
MACOSX_DEPLOYMENT_TARGET = 10.13;
MARKETING_VERSION = 1.8.2;
MARKETING_VERSION = 1.8.3;
PRODUCT_BUNDLE_IDENTIFIER = com.parse.ParseSwift;
PRODUCT_NAME = ParseSwift;
SDKROOT = macosx;
Expand All @@ -2549,7 +2549,7 @@
INSTALL_PATH = "$(LOCAL_LIBRARY_DIR)/Frameworks";
LD_RUNPATH_SEARCH_PATHS = "$(inherited) @executable_path/../Frameworks @loader_path/Frameworks";
MACOSX_DEPLOYMENT_TARGET = 10.13;
MARKETING_VERSION = 1.8.2;
MARKETING_VERSION = 1.8.3;
PRODUCT_BUNDLE_IDENTIFIER = com.parse.ParseSwift;
PRODUCT_NAME = ParseSwift;
SDKROOT = macosx;
Expand Down Expand Up @@ -2696,7 +2696,7 @@
INFOPLIST_FILE = "ParseSwift-watchOS/Info.plist";
INSTALL_PATH = "$(LOCAL_LIBRARY_DIR)/Frameworks";
LD_RUNPATH_SEARCH_PATHS = "$(inherited) @executable_path/Frameworks @loader_path/Frameworks";
MARKETING_VERSION = 1.8.2;
MARKETING_VERSION = 1.8.3;
MTL_ENABLE_DEBUG_INFO = INCLUDE_SOURCE;
MTL_FAST_MATH = YES;
PRODUCT_BUNDLE_IDENTIFIER = "com.parse.ParseSwift-watchOS";
Expand Down Expand Up @@ -2725,7 +2725,7 @@
INFOPLIST_FILE = "ParseSwift-watchOS/Info.plist";
INSTALL_PATH = "$(LOCAL_LIBRARY_DIR)/Frameworks";
LD_RUNPATH_SEARCH_PATHS = "$(inherited) @executable_path/Frameworks @loader_path/Frameworks";
MARKETING_VERSION = 1.8.2;
MARKETING_VERSION = 1.8.3;
MTL_FAST_MATH = YES;
PRODUCT_BUNDLE_IDENTIFIER = "com.parse.ParseSwift-watchOS";
PRODUCT_NAME = ParseSwift;
Expand All @@ -2752,7 +2752,7 @@
INFOPLIST_FILE = "ParseSwift-tvOS/Info.plist";
INSTALL_PATH = "$(LOCAL_LIBRARY_DIR)/Frameworks";
LD_RUNPATH_SEARCH_PATHS = "$(inherited) @executable_path/Frameworks @loader_path/Frameworks";
MARKETING_VERSION = 1.8.2;
MARKETING_VERSION = 1.8.3;
MTL_ENABLE_DEBUG_INFO = INCLUDE_SOURCE;
MTL_FAST_MATH = YES;
PRODUCT_BUNDLE_IDENTIFIER = "com.parse.ParseSwift-tvOS";
Expand Down Expand Up @@ -2780,7 +2780,7 @@
INFOPLIST_FILE = "ParseSwift-tvOS/Info.plist";
INSTALL_PATH = "$(LOCAL_LIBRARY_DIR)/Frameworks";
LD_RUNPATH_SEARCH_PATHS = "$(inherited) @executable_path/Frameworks @loader_path/Frameworks";
MARKETING_VERSION = 1.8.2;
MARKETING_VERSION = 1.8.3;
MTL_FAST_MATH = YES;
PRODUCT_BUNDLE_IDENTIFIER = "com.parse.ParseSwift-tvOS";
PRODUCT_NAME = ParseSwift;
Expand Down
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ import PackageDescription
let package = Package(
name: "YOUR_PROJECT_NAME",
dependencies: [
.package(url: "https://github.com/parse-community/Parse-Swift", from: "1.8.1"),
.package(url: "https://github.com/parse-community/Parse-Swift", from: "1.8.3"),
]
)
```
Expand Down
2 changes: 1 addition & 1 deletion Scripts/jazzy.sh
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ bundle exec jazzy \
--author_url http://parseplatform.org \
--github_url https://github.com/parse-community/Parse-Swift \
--root-url http://parseplatform.org/Parse-Swift/api/ \
--module-version 1.8.2 \
--module-version 1.8.3 \
--theme fullwidth \
--skip-undocumented \
--output ./docs/api \
Expand Down
2 changes: 1 addition & 1 deletion Sources/ParseSwift/Coding/ParseEncoder.swift
Original file line number Diff line number Diff line change
Expand Up @@ -893,7 +893,7 @@ extension _ParseEncoder {
//COREY: DON'T remove the force unwrap, it will crash the app
// swiftlint:disable:next force_cast
return try self.box(value as! [String : Encodable])
} else if value is PointerType {
} else if value is ParsePointer {
ignoreSkipKeys = true
}

Expand Down
12 changes: 7 additions & 5 deletions Sources/ParseSwift/Types/Pointer.swift
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@ import Foundation
import Combine
#endif

protocol ParsePointer { }

private func getObjectId<T: ParseObject>(target: T) throws -> String {
guard let objectId = target.objectId else {
throw ParseError(code: .missingObjectId, message: "Cannot set a pointer to an unsaved object")
Expand All @@ -18,7 +20,7 @@ private func getObjectId(target: Objectable) throws -> String {
}

/// A Pointer referencing a ParseObject.
public struct Pointer<T: ParseObject>: Fetchable, Encodable, Hashable {
public struct Pointer<T: ParseObject>: ParsePointer, Fetchable, Encodable, Hashable {

private let __type: String = "Pointer" // swiftlint:disable:this identifier_name

Expand Down Expand Up @@ -127,13 +129,13 @@ public extension Pointer {
#endif
}

internal struct PointerType: Encodable {
internal struct PointerType: ParsePointer, Encodable {

var __type: String = "Pointer" // swiftlint:disable:this identifier_name
public var objectId: String
public var className: String
var objectId: String
var className: String

public init(_ target: Objectable) throws {
init(_ target: Objectable) throws {
self.objectId = try getObjectId(target: target)
self.className = target.className
}
Expand Down
35 changes: 35 additions & 0 deletions Tests/ParseSwiftTests/ParsePointerTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ class ParsePointerTests: XCTestCase {

//: Your own properties
var score: Int
var other: Pointer<GameScore>?

//: a custom initializer
init(score: Int) {
Expand Down Expand Up @@ -199,6 +200,40 @@ class ParsePointerTests: XCTestCase {
}

#if !os(Linux) && !os(Android)
func testEncodeEmbeddedPointer() throws {
var score = GameScore(score: 10)
let objectId = "yarr"
score.objectId = objectId

var score2 = GameScore(score: 50)
score2.other = try score.toPointer()

let encoded = try score2.getEncoder().encode(score2,
collectChildren: false,
objectsSavedBeforeThisOne: nil,
filesSavedBeforeThisOne: nil)

let decoded = String(data: encoded.encoded, encoding: .utf8)
XCTAssertEqual(decoded,
// swiftlint:disable:next line_length
"{\"score\":50,\"other\":{\"__type\":\"Pointer\",\"className\":\"GameScore\",\"objectId\":\"yarr\"}}")
XCTAssertEqual(encoded.unique.count, 0)
XCTAssertEqual(encoded.unsavedChildren.count, 0)
}

func testPointerTypeEncoding() throws {
var score = GameScore(score: 10)
let objectId = "yarr"
score.objectId = objectId

let pointerType = try PointerType(score)

let encoded = try ParseCoding.parseEncoder().encode(pointerType)
let decoded = String(data: encoded, encoding: .utf8)
XCTAssertEqual(decoded,
"{\"__type\":\"Pointer\",\"className\":\"GameScore\",\"objectId\":\"yarr\"}")
}

func testThreadSafeFetchAsync() throws {
var score = GameScore(score: 10)
let objectId = "yarr"
Expand Down