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

fix(DataStore): Store larger than 32-bit values in Int64 over Int #3367

Merged
merged 2 commits into from
Dec 1, 2023

Conversation

lawmicha
Copy link
Contributor

@lawmicha lawmicha commented Nov 16, 2023

Issue #

#3220

Table of Contents

  1. fix(DataStore): Store larger than 32-bit values in Int64 over Int #3367 (You are here)
  2. feat(DataStore): DisableSubscriptions flag for watchOS #3368
  3. fix: watchOS support - disable network monitoring, add client timeout #141

Description

This PR is one of the changes necessary get DataStore working on watchOS. Currently, there are two issues with running DataStore on watchOS:

  1. It is observed that Int type is of size 32-bit on a physical watch device. When storing a value that's greater than 32-bits, it will crash with the error:
Swift/arm64_32-apple-watchos.swiftinterface:34421: Fatal error: Double value cannot be converted to Int because the result would be greater than Int.max
  1. DataStore does not transition to an active sync state as it is stalled on connecting to the websocket endpoint. More to come regarding this issue in the next PR.

This PR addresses the first issue, as a pre-requisite to the second issue. We do this change first to make sure there are no regressions with existing use cases, and gives us a clean slate to work with. As well, it should be easier to get this change through the pipeline as it only involves internal implementation details of DataStore and have no customer impacting changes.

Int is replaced with Int64 when we know that the values we are storing are larger than 32-bit. In DataStore, ModelMetadata and MutationSyncMetadata tables have columns storing lastSync and lastChangedAt. The values are, for example, 1,699,631,281,107 which is much larger than the largest 32-bit value of 2,147,483,647.

DataStore uses a local SQLite database, and have no changes to the local table generation, continue to store the values as integer, which supports 4B (32 bit) or 8B (64 bits).

INTEGER. The value is a signed integer, stored in 0, 1, 2, 3, 4, 6, or 8 bytes depending on the magnitude of the value.

https://www.sqlite.org/datatype3.html

When creating a new watchOS app, the Architecture is set with “Standard Architectures (arm64, arm7k, arm64_32)”. (Xcode 15+).

arm64_32 is a variant of arm64 with 32-bit pointer sizes, used on Apple Watch Series 4 and later.

https://stackoverflow.com/questions/67042548/watchos-multiple-build-targets-any-watchos-device-vs-armv7k-arm64-32

While the issue seems counter to the statement:

On 32-bit platforms, Int is the same size as Int32, and on 64-bit platforms, Int is the same size as Int64.

https://developer.apple.com/documentation/swift/int

Setting things up with the latest Xcode (15.1), Apple Watch Series 9 running watchOS 10.1, the Int type is observed to be 32-bit size, crashing on the error above.

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.

@lawmicha lawmicha requested a review from a team as a code owner November 16, 2023 16:59
@lawmicha lawmicha merged commit 66c46a7 into main Dec 1, 2023
76 checks passed
@lawmicha lawmicha deleted the lawmicha.int64 branch December 1, 2023 16:11
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