Skip to content

Conversation

@royjit
Copy link
Contributor

@royjit royjit commented May 10, 2023

Description

Changes from v2

  • Adds @available(iOS 13.0.0, *) for async functions

This PR introduces the LazyReference type. The post reference in the Comment type is replaced with an instance of LazyReference.

Before:

struct Comment: Model {
  let id: String
  var content: String?
  var post: Post
}

After:

struct Comment: Model {
  let _post: LazyReference<Post>
  public let post: Post { 
     get async throws {
         try await _post.require()
     }
  }
}

The computed property post is backed by the internal _post of type LazyReference. The computed property allows the developer to make an async call to lazy load the post. With the computed property and internal property, the developer call pattern becomes:

let post = try await comment.post

When the association is required, the underlying API used is _post.require(), if the association is optional, then _post.get(). _post.require() will throw if the model cannot be loaded, while get() will return nil. The LazyReference type allows the data payloads to include all fields (eager loaded) or include only the primary keys of post (lazy loaded), used for decoding to the type into one of the two states.

When a LazyReference instance is created during data decoding, a model provider is created using the registered plugin's model provider registry.

General Checklist

  • Added new tests to cover change, if needed
  • Build succeeds with all target using Swift Package Manager
  • All unit tests pass
  • All integration tests pass
  • Security oriented best practices and standards are followed (e.g. using input sanitization, principle of least privilege, etc)
  • Documentation update for the change if required
  • PR title conforms to conventional commit style
  • New or updated tests include Given When Then inline code documentation and are named accordingly testThing_condition_expectation()
  • If breaking change, documentation/changelog update with migration instructions

By submitting this pull request, I confirm that my contribution is made under the terms of the Apache 2.0 license.

@royjit royjit requested a review from a team as a code owner May 10, 2023 18:48
@royjit royjit merged commit bd164aa into royjit.feat.lazyloading May 17, 2023
@royjit royjit deleted the royjit.coremodels branch May 17, 2023 19:41
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.

2 participants