ParseSwift not Saving Int to Column - undefined

67 Views Asked by At

I have a simple GameScore object as defined in docs (https://github.com/parse-community/Parse-Swift/blob/main/ParseSwift.playground/Pages/1%20-%20Your%20first%20Object.xcplaygroundpage/Contents.swift)

import Foundation
import ParseSwift

struct GameScore: ParseObject {
    var objectId: String?
    var createdAt: Date?
    var updatedAt: Date?
    var ACL: ParseACL?
    var originalData: Data?


    // Your own properties
    var score: Int?
    var name: String?

    func merge(with object: Self) throws -> Self {
        var updated = try mergeParse(with: object)
        if updated.shouldRestoreKey(\.score, original: object) {
            updated.score = object.score
        }
        if updated.shouldRestoreKey(\.name, original: object) {
            updated.name = object.name
        }
        return updated
    }
}
extension GameScore {

    init(score: Int) {
        self.score = score
    }

    init(score: Int, name: String) {
        self.score = score
        self.name = name
    }

    init(objectId: String?) {
        self.objectId = objectId
    }
}

Then I save a GameScore object to the server by calling this function:

func createScore() {
    var score = GameScore(score: Int(scoreScore), name: scoreName)
    score.score = Int(scoreScore)
    score.save { [weak self] result in
        switch result {
        case .success(let savedScore):
            score = savedScore
            self?.gameScore = savedScore
            self?.fetchGameScores()
            print("savedScore - score: \(score.score ?? 0), name: \(score.name ?? "")")
        case .failure(let error):
            print("save error: \(error.localizedDescription)")
        }
    }
}

Now, when I save this object to the Parse server it saves it but the score property is always undefined. What am I doing wrong? I also tried incrementing the score property and the increment() operation by 1 for example works as expected and the score becomes 1. But it never accepts the initial value I set the score to be.

This should be very simple and easy to do yet it's not working and I don't understand why.

NOTE: I'm using the ParseSwift library and not the standard Parse iOS SDK that is in Obj-C.

Update

When I put a breakpoint in the save functions callback block, I do see that the savedScore in the:

case .success(let savedScore) does have a score of 100 or something undefined. So maybe the issue is with my Parse server not showing this Int value properly in the dashboard? It's very strange. However, it does show the name property correctly that also is on the GameScore object. Also, after fetching the GameScores from the server, the newest created GameScore looks like has an undefined score instead of what the response callback says. So the server is not saving the Int value for some reason.

Update 2

I created another column in Parse server and a corresponding property in my GameScore object called point (just like example shows) just to test it out. With point, it saves the initial value I give it correctly! I don't know what the issue is with score but maybe it's a keyword or something that's not working.

1

There are 1 best solutions below

2
On

score is a protected field in Parse and this key is skipped so it's never encoded to the server and shouldn't be used as a property unless used for querying (see more below). The parse-server shows what score is used for: mongo, postgres, query.

The score field is used for querying, see more here:

/**
 Conform to this protocol to add the required properties to your `ParseObject`
 for using `QueryConstraint.matchesText()` and `Query.sortByTextScore()`.
 - note: In order to sort you must use `Query.sortByTextScore()`.
   To retrieve the weight/rank, access the "score" property of your `ParseObject`.
 */

public protocol ParseQueryScorable {
    /**
     The weight/rank of a `QueryConstraint.matchesText()`.
    */
    var score: Double? { get }
}