I need help to switch from Parse-iOS-SDK to Parse-Swift with a Redux-like implementation

Hello,

I am working on a project whose model is generated automatically in Swift from a web (Typescript) representation, and contained in a library (say “Model”).

Each object from the model must be representable as a JSON dictionary, the properties being primitive types or basic structs (i.e., ParseGeoPoint is represented as a “ModelGeoPoint” struct containing a latitude and a longitude Doubles, and ParseFile corresponds to a “ModelFile” type, contains two String properties: url and name).
Hence, Parse is not used in this library. As an example:

protocol MissionClass {
	var className: String { get }
	var state: [String: Any] { get }
}

struct Player: MissionClass {
    static var className: String {
        return "Player"
    }

    var state: [String: Any] = [:]

    var name: String? {
        set { data["name"] = newValue }
        get { data["name"] as? String }
    }

    var score: Double? {
        set { data["score"] = newValue }
        get { data["score"] as? Double }
    }

    // ...
}

With the legacy library (Parse-SDK-iOS), from the consumer app which uses the “Model” library, I could easily create a generic query which would only need the className from the associated object, and in the callback result, convert the properties (i.e., PFGeoPoint to ModelGeoPoint), in order to create the JSON dictionary as expected and fill the Redux state.

Now, I would like to replace the use of Parse-SDK-iOS for Parse-Swift, but I end up being stuck by the fact that this library seems to be more strongly typed. Admitting that I have a “Player” type into the “Model” library, I wouldn’t like to have to create a supplementary “Player” type into the consumer app, specifically to inherit ParseObject, because I would feel like having almost duplicate code.

What ideas could you suggest me?

Thank you for your help.

From what you’ve shown, Parse-Swift should make what you are doing easier because all ParseObject’s are structures and are Codable (encode/decode to JSON). I recommend exploring the Playgrounds to learn more. Remove the MissionClass protocol and just conform to ParseObject, Any isn’t Codable, you can learn more:

Remove your custom getters/setters. You can use the methods available on a ParseObject to encode/decode them to JSON when needed:

An example of using the aforementioned encoders to encode to a JSON dictionary (this will be less prone to error than the manual steps in your example):

Anything else you are trying to do sounds custom and you will need to figure out on your own as the SDK provides mechanisms to go to/from JSON and is designed to communicate with Parse Servers. Anything else needed isn’t provided by the Swift SDK natively.

1 Like

Hi @cbaker6 and thank you for your answer. Special thanks for the explanations and examples about the JSON encoding/decoding.

The only issue that I have is that my CTO wants the “Model” library (the one that contains the model) to be unrelated to any library like Parse, it should only use raw data and primitive types.

Then this is my consumer app that will embed both the model library and Parse, and say, "OK, this property is a Double in the model library so let’s use a Double, and this one is a ModelGeoPoint in the model library, so let’s map this into a ParseGeoPoint.

So I am sorry if it was unclear from my initial question, my real problem would rather be: how can I bridge the existing model to a Parse-Swift one without duplicating code.

Also, sorry is this request sounds strange but it is what I am being asked right now. Of course, I would accept any counter-argument that would suggest to embed Parse directly in the model library.

ParseObject’s are models, so you will need to write methods in your custom models that convert to/from ParseObject’s and extend ParseObjects to convert to/from your custom models. You can attempt to reduce code by using the same property names across models along with the JSON encoders/decoders I mentioned earlier.