Skip to content

Commit a8b3d00

Browse files
authored
Fix custom objectId fetch (#139)
* Fix custom objectId fetch * add changelog * Add testcases * nit * nit playground
1 parent 50935e0 commit a8b3d00

File tree

8 files changed

+127
-5
lines changed

8 files changed

+127
-5
lines changed

CHANGELOG.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,9 @@
44
[Full Changelog](https://github.com/parse-community/Parse-Swift/compare/1.7.1...main)
55
* _Contributing to this repo? Add info about your change here to be included in the next release_
66

7+
__Fixes__
8+
- Fixed issue where Swift SDK prevented fetching of Parse objects when custom objectId was enabled ([#139](https://github.com/parse-community/Parse-Swift/pull/139)), thanks to [Corey Baker](https://github.com/cbaker6).
9+
710
__Improvements__
811
- Add playground example of saving Parse objects with a custom objectId ([#137](https://github.com/parse-community/Parse-Swift/pull/137)), thanks to [Corey Baker](https://github.com/cbaker6).
912

ParseSwift.playground/Pages/15 - Custom ObjectId.xcplaygroundpage/Contents.swift

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -80,4 +80,36 @@ score.save { result in
8080
}
8181
}
8282

83+
//: Fetch object
84+
score.fetch { result in
85+
switch result {
86+
case .success(let fetchedScore):
87+
print("Successfully fetched: \(fetchedScore)")
88+
case .failure(let error):
89+
assertionFailure("Error fetching: \(error)")
90+
}
91+
}
92+
93+
//: Query object
94+
let query = GameScore.query("objectId" == "myObjectId")
95+
query.first { result in
96+
switch result {
97+
case .success(let found):
98+
print(found)
99+
case .failure(let error):
100+
print(error)
101+
}
102+
}
103+
104+
//: Query object
105+
let query = GameScore.query("objectId" == "myObjectId")
106+
query.find { result in
107+
switch result {
108+
case .success(let found):
109+
print(found)
110+
case .failure(let error):
111+
print(error)
112+
}
113+
}
114+
83115
//: [Next](@next)

ParseSwift.playground/contents.xcplayground

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,4 +17,4 @@
1717
<page name='14 - Config'/>
1818
<page name='15 - Custom ObjectId'/>
1919
</pages>
20-
</playground>
20+
</playground>

Sources/ParseSwift/API/API+Commands.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -392,7 +392,7 @@ internal extension API.Command {
392392

393393
// MARK: Fetching
394394
static func fetchCommand<T>(_ object: T, include: [String]?) throws -> API.Command<T, T> where T: ParseObject {
395-
guard object.isSaved else {
395+
guard object.objectId != nil else {
396396
throw ParseError(code: .unknownError, message: "Cannot Fetch an object without id")
397397
}
398398

Sources/ParseSwift/Objects/ParseInstallation.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -402,7 +402,7 @@ extension ParseInstallation {
402402
}
403403

404404
func fetchCommand(include: [String]?) throws -> API.Command<Self, Self> {
405-
guard isSaved else {
405+
guard objectId != nil else {
406406
throw ParseError(code: .unknownError, message: "Cannot fetch an object without id")
407407
}
408408

Sources/ParseSwift/Objects/ParseUser.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -755,7 +755,7 @@ extension ParseUser {
755755
}
756756

757757
func fetchCommand(include: [String]?) throws -> API.Command<Self, Self> {
758-
guard isSaved else {
758+
guard objectId != nil else {
759759
throw ParseError(code: .unknownError, message: "Cannot fetch an object without id")
760760
}
761761

Sources/ParseSwift/Protocols/Objectable.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -80,7 +80,7 @@ extension Objectable {
8080
if !ParseSwift.configuration.allowCustomObjectId {
8181
return objectId != nil
8282
} else {
83-
return createdAt != nil
83+
return objectId != nil && createdAt != nil
8484
}
8585
}
8686

Tests/ParseSwiftTests/ParseObjectCustomObjectIdTests.swift

Lines changed: 87 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1657,4 +1657,91 @@ class ParseObjectCustomObjectIdTests: XCTestCase { // swiftlint:disable:this typ
16571657
}
16581658
wait(for: [expectation1], timeout: 20.0)
16591659
}
1660+
1661+
// swiftlint:disable:next function_body_length
1662+
func testFetch() {
1663+
var score = GameScore(score: 10)
1664+
let objectId = "yarr"
1665+
score.objectId = objectId
1666+
1667+
var scoreOnServer = score
1668+
scoreOnServer.createdAt = Date()
1669+
scoreOnServer.updatedAt = scoreOnServer.createdAt
1670+
scoreOnServer.ACL = nil
1671+
let encoded: Data!
1672+
do {
1673+
encoded = try ParseCoding.jsonEncoder().encode(scoreOnServer)
1674+
//Get dates in correct format from ParseDecoding strategy
1675+
scoreOnServer = try scoreOnServer.getDecoder().decode(GameScore.self, from: encoded)
1676+
} catch {
1677+
XCTFail("Should encode/decode. Error \(error)")
1678+
return
1679+
}
1680+
1681+
MockURLProtocol.mockRequests { _ in
1682+
return MockURLResponse(data: encoded, statusCode: 200, delay: 0.0)
1683+
}
1684+
do {
1685+
let fetched = try score.fetch(options: [])
1686+
XCTAssert(fetched.hasSameObjectId(as: scoreOnServer))
1687+
guard let fetchedCreatedAt = fetched.createdAt,
1688+
let fetchedUpdatedAt = fetched.updatedAt else {
1689+
XCTFail("Should unwrap dates")
1690+
return
1691+
}
1692+
guard let originalCreatedAt = scoreOnServer.createdAt,
1693+
let originalUpdatedAt = scoreOnServer.updatedAt else {
1694+
XCTFail("Should unwrap dates")
1695+
return
1696+
}
1697+
XCTAssertEqual(fetchedCreatedAt, originalCreatedAt)
1698+
XCTAssertEqual(fetchedUpdatedAt, originalUpdatedAt)
1699+
XCTAssertNil(fetched.ACL)
1700+
} catch {
1701+
XCTFail(error.localizedDescription)
1702+
}
1703+
}
1704+
1705+
func testFetchUser() { // swiftlint:disable:this function_body_length
1706+
var user = User()
1707+
let objectId = "yarr"
1708+
user.objectId = objectId
1709+
1710+
var userOnServer = user
1711+
userOnServer.createdAt = Date()
1712+
userOnServer.updatedAt = userOnServer.createdAt
1713+
userOnServer.ACL = nil
1714+
let encoded: Data!
1715+
do {
1716+
encoded = try userOnServer.getEncoder().encode(userOnServer, skipKeys: .none)
1717+
//Get dates in correct format from ParseDecoding strategy
1718+
userOnServer = try userOnServer.getDecoder().decode(User.self, from: encoded)
1719+
} catch {
1720+
XCTFail("Should encode/decode. Error \(error)")
1721+
return
1722+
}
1723+
MockURLProtocol.mockRequests { _ in
1724+
return MockURLResponse(data: encoded, statusCode: 200, delay: 0.0)
1725+
}
1726+
1727+
do {
1728+
let fetched = try user.fetch()
1729+
XCTAssert(fetched.hasSameObjectId(as: userOnServer))
1730+
guard let fetchedCreatedAt = fetched.createdAt,
1731+
let fetchedUpdatedAt = fetched.updatedAt else {
1732+
XCTFail("Should unwrap dates")
1733+
return
1734+
}
1735+
guard let originalCreatedAt = userOnServer.createdAt,
1736+
let originalUpdatedAt = userOnServer.updatedAt else {
1737+
XCTFail("Should unwrap dates")
1738+
return
1739+
}
1740+
XCTAssertEqual(fetchedCreatedAt, originalCreatedAt)
1741+
XCTAssertEqual(fetchedUpdatedAt, originalUpdatedAt)
1742+
XCTAssertNil(fetched.ACL)
1743+
} catch {
1744+
XCTFail(error.localizedDescription)
1745+
}
1746+
}
16601747
}

0 commit comments

Comments
 (0)