Context in ParseSwift save

Hello, is there a way how to pass date as context for the .save() function in ParseSwift SDK? Thanks!

If I’m understanding the comment on how context was added to the JS SDK correctly

The context is added to the property of the ParseObject. If this is true, ParseSwift’s design allows you to do this easily by just defining the property in your struct:

struct GameScore: ParseObject {
   ...
   var _context: [String: String]? // Or any encodable object. I don’t use context, so not sure what it needs to look like. 
}

This will probably not be officially added to the Swift SDK as it will add some unnecessary complexity due to context being able to be any Encodable and I don’t see a reason to require users to add a _context property to all of their objects which will happen if it’s officially added to the SDK.

Try what I mentioned above and let me know if it works.

it throws following error without trying to read the context in beforeSave trigger:

ParseError(code: ParseSwift.ParseError.Code.invalidKeyName, message: “Invalid field name for update: _context”)

Also in Manuel’s example, he is accessing the context in the request itself, not the request.object :smiley:

Parse.Cloud.beforeSave("TestObject", req => {
    console.log(req.context.a);
});

the error sounds like I would need to add the field _context also as a field in the database

You can look in API to see where it should be added Parse-Swift/API.swift at main · parse-community/Parse-Swift · GitHub

I don’t use context, so I don’t know much about it.

From what I understand, context currently can’t be used by the Swift SDK because it doesn’t seem to be fully implemented on the server-side:

It seems like it should of had a dedicated header added which is currently missing from above. Probably best for @Manuel to confirm if this is true since he made the discussion and PR to expose context and may know more about it’s origins and why it doesn’t have a header (PR).

The JS SDK adds a _context key to the body for it to work. The Swift SDK doesn’t ever add keys like this to the body (this looks a little hacky IMO, but from the comments it seems some of the other SDKs do it). If there’s a header added on the server side or perhaps a different way to use it that someone mentions then it will be an easy add to the Swift SDK. Until then, context won’t be added to the Swift SDK.

I’ve opened a PR on the Swift SDK to support context:

As I mentioned in the previous comment, the server would have to receive header support for X-Parse-Context for it to work.

A PR has been created on the parse-server to enable context in a way that will work with the Swift SDK.