Closed pmmlo closed 2 years ago
@cbaker6 Any chance you could take a look?
It's really odd behavior. I have no problem updating once, but on the second/subsequent updates I get an error about needing a required field. Even if I pass the required field, I get the error.
Try changing: var createdBy: Pointer<User>?
-> var createdBy: User?
as this is what you probably really want anyways. The SDK will handle turning createdBy
to the proper pointer during the first save. After the initial save, you may need to fetch
or include
; createdBy
in your queries. ShotCodable
should probably conform to ParseObject
based on how you are using it and var shooter: Pointer<User>?
should be var shooter: User?
. Essentially, your objects should look similar to Author
in the playgrounds: https://github.com/parse-community/Parse-Swift/blob/4d2d509fc946ee70fbfcd75d7b0520cc7164d72e/ParseSwift.playground/Pages/8%20-%20Pointers.xcplaygroundpage/Contents.swift#L54-L172
Also, the code below is asynchronous, so there's no guarantee the saved version of gameScore
:
var gameScore = GameScore()
gameScore.createdBy = try? User.current.toPointer()
gameScore.save { result in
switch {
case .success(let success):
gameScore = success
case .failure(let error):
print("Failed to save \(error)")
}
}
will be available before gameScore.mergeable()
the way your current code is structured:
let shot = ShotCodable(name: "Curry", shooter: try? User.current.toPointer(), score: 3)
var mergeableGameScore = gameScore.mergeable()
mergeableGameScore.shots = [shot]
mergeableGameScore.save { result in
switch {
case .success(let success):
print(success)
case .failure(let error):
print("Failed to save \(error)")
}
}
You can fix this by using async/await or moving the code into the first save() { result in... }
Yup, that was the issue. Thanks!
I should actually be clearer just in case someone has a similar issue. Your answer is great, but wasn't the issue. It was apparently using createdAt
, updatedAt
, and objectId
as parameters within an array object. I suspect those are reserved, so those were not being saved. Not sure exactly for sure, but renaming these in my local project updates the object.
Thanks for the help!
I still recommend the changes. The way your objects are setup in your question reduces the ability of Swift SDK and Parse and requires you to make extra calls to the server along with write extra code (your example). The coding footprint can be reduced significantly by following the suggested way in Playgrounds along with the ability to use include/exclude/fetch directly.
New Issue Checklist
Issue Description
Very odd behavior. Initial edit of a nil-valued field saves without error, but subsequent changes/updates to the model returns an error 142 with the required field needed.
Steps to reproduce
Additionally, when I make the require column unrequired, there is no error. In other words, a class with no required fields succeeds without issue.
Example:
Actual Outcome
Error 142 with [required field] needed.
Expected Outcome
Save success.
Environment
Client
Server
Database
Logs