Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Send only changed properties to cloud by default when updating object #401

Closed
3 tasks done
mtrezza opened this issue Sep 3, 2022 · 2 comments · Fixed by #406
Closed
3 tasks done

Send only changed properties to cloud by default when updating object #401

mtrezza opened this issue Sep 3, 2022 · 2 comments · Fixed by #406
Labels
type:feature New feature or improvement of existing feature

Comments

@mtrezza
Copy link
Member

mtrezza commented Sep 3, 2022

New Feature / Enhancement Checklist

Current Limitation

The Parse Swift SDK requires additional code overhead and developer considerations when updating objects. Specifically, to send only changed properties to the server, the SDK requires the developer to create an object clone from ParseObject.mergeable and override an internal method ParseObject.merge . The same behavior is achieved by other Parse SDKs without these efforts.

The disadvantages are:

  • Developer needs to manually maintain custom properties in the overridden ParseObject.merge method. For example, if a new custom property is added to the object but not to the internal method, it creates data inconsistencies that may lead to bugs that are difficult to track down.
  • Adds code complexity for what is supposed to be one of the simplest operations - updating an object.

Feature / Enhancement Description

The Parse Swift SDK should send only the changed properties to the server by default. Without requiring the developer to override an internal method of ParseObject and without having to clone the object from ParseObject.mergeable.

Example Use Case

The following examples compare how to update a saved object in the Parse ObjC SDK vs. the Parse Swift SDK.

// Parse ObjC SDK
PFObject *obj = [PFObject objectWithClassName:@"Example"];
obj[@"key"] = @"value1";
[obj save];
obj[@"key"] = @"value2";
[obj save];

// Parse Swift SDK
struct Example: ParseObject {
  var objectId: String?
  var createdAt: Date?
  var updatedAt: Date?
  var ACL: ParseACL?
  var originalData: Data? 
  var key: String?

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

let obj = Example()
obj.key = "value1"
obj.save()
var objMergable = obj.mergeable
objMergable.key = "value2"
objMergable.save()

Alternatives / Workarounds

n/a

References

@mtrezza mtrezza added the type:feature New feature or improvement of existing feature label Sep 3, 2022
@parse-github-assistant
Copy link

parse-github-assistant bot commented Sep 3, 2022

Thanks for opening this issue!

  • 🎉 We are excited about your ideas for improvement!

@mtrezza
Copy link
Member Author

mtrezza commented Sep 9, 2022

PR #406 was a great step towards simplification.

To follow up on the example above, the code after the PR would be:

// Parse ObjC SDK
PFObject *obj = [PFObject objectWithClassName:@"Example"];
obj[@"key"] = @"value1";
[obj save];
obj[@"key"] = @"value2";
[obj save];

// Parse Swift SDK
struct Example: ParseObject {
  var objectId: String?
  var createdAt: Date?
  var updatedAt: Date?
  var ACL: ParseACL?
  var originalData: Data? 
  var key: String?
}

let obj = Example()
obj.key = "value1"
obj.save()
let updatedObj = try obj.key.set(\.key, to: "value2")
let savedUpdatedObj = try await updatedObj.save()

// or without declaring new vars for every state
var obj = Example()
obj.key = "value1"
obj.save()
obj = try obj.key.set(\.key, to: "value2")
obj = try await obj.save()

Is that correct?

If so, I think we can reword the warning in the migration guide, but not remove it completely. Because a developer still has to keep in mind that there is a special approach (even though simplified thanks to #406) needed to only send updated keys to the server, and they cannot simply use obj.key = "value2".

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
type:feature New feature or improvement of existing feature
Projects
None yet
1 participant