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

feat: add isDirtyForKey method for ParseObjects #9

Merged
merged 3 commits into from
Oct 29, 2022
Merged

Conversation

cbaker6
Copy link
Member

@cbaker6 cbaker6 commented Oct 29, 2022

New Pull Request Checklist

Issue Description

Currently a developer has to write extra code to determine if a ParseObject has a dirty key.

Approach

Add the following extensions to check a dirty KeyPath:

public extension ParseObject {

      /**
      Get whether a value associated with a `KeyPath` has been added/updated on the client, but has
      not been updated on a Parse Server.
      - parameter keyPath: The `KeyPath` of the value to check.
      - throws: An error of type `ParseError`.
      - returns: **true** if the `KeyPath` is dirty, **false** otherwise.
      - important: In order for a `KeyPath` to be considered dirty, the respective `ParseObject` needs to
      first be saved to a Parse Server and fetched locally.
      - note: This method should only be used after updating a `ParseObject` using `.set()` or
      `.mergeable` otherwide it will always return **false**.
      */
     func isDirtyForKey<W>(_ keyPath: KeyPath<Self, W?>) throws -> Bool where W: Equatable {
         guard let originalData = originalData else {
             return false
         }
         let original = try ParseCoding.jsonDecoder().decode(Self.self,
                                                             from: originalData)
         return self[keyPath: keyPath] != original[keyPath: keyPath]
     }

     /**
      Creates a `ParseObject` with a specified `objectId`. Can be used to create references or associations
      between `ParseObject`'s.
      - warning: It is required that all added properties be optional properties so they can eventually be used as
      Parse `Pointer`'s. If a developer really wants to have a required key, they should require it on the server-side or
      create methods to check the respective properties on the client-side before saving objects. See
      [here](https://github.com/parse-community/Parse-Swift/pull/315#issuecomment-1014701003)
      for more information.
      */
     init(objectId: String) {
         self.init()
         self.objectId = objectId
     }
}

TODOs before merging

  • Add tests
  • Add entry to changelog
  • Add changes to documentation (guides, repository pages, in-code descriptions)

@codecov-commenter
Copy link

codecov-commenter commented Oct 29, 2022

Codecov Report

Merging #9 (0256045) into main (56d556f) will decrease coverage by 0.04%.
The diff coverage is 100.00%.

@@            Coverage Diff             @@
##             main       #9      +/-   ##
==========================================
- Coverage   90.44%   90.40%   -0.05%     
==========================================
  Files         164      161       -3     
  Lines       16345    16347       +2     
==========================================
- Hits        14784    14779       -5     
- Misses       1561     1568       +7     
Impacted Files Coverage Δ
Sources/ParseSwift/Objects/ParseObject.swift 90.31% <100.00%> (+0.01%) ⬆️
Sources/ParseSwift/LiveQuery/ParseLiveQuery.swift 76.16% <0.00%> (-0.59%) ⬇️
Sources/ParseSwift/Coding/ParseEncoder.swift 77.68% <0.00%> (-0.46%) ⬇️
TestHostTV/AppDelegate.swift
TestHost/AppDelegate.swift
TestHostTV/ViewController.swift
Sources/ParseSwift/Coding/ParseCoding.swift 100.00% <0.00%> (+2.59%) ⬆️

Help us with your feedback. Take ten seconds to tell us how you rate us. Have a feature suggestion? Share it here.

@cbaker6 cbaker6 merged commit b96f4d7 into main Oct 29, 2022
@cbaker6 cbaker6 deleted the initWithObjectId branch October 29, 2022 20:58
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

2 participants