{"id":31284823,"url":"https://github.com/markbattistella/swiftdatacounter","last_synced_at":"2026-05-08T14:16:46.086Z","repository":{"id":314678652,"uuid":"1056129172","full_name":"markbattistella/SwiftDataCounter","owner":"markbattistella","description":"SwiftDataCounter is a Swift package that provides live tracking of SwiftData model counts with optional per-model or default limits. It listens for ModelContext save notifications and automatically refreshes counts, while also logging changes and limit crossings using OSLog.","archived":false,"fork":false,"pushed_at":"2026-03-08T11:32:24.000Z","size":45,"stargazers_count":0,"open_issues_count":0,"forks_count":0,"subscribers_count":0,"default_branch":"main","last_synced_at":"2026-03-24T09:11:42.536Z","etag":null,"topics":["ios","ipados","macos","open-source","package-manager","swift","swift-package-manager","swiftdata","swiftui","tvos","visionos","watchos"],"latest_commit_sha":null,"homepage":"https://swiftpackageindex.com/markbattistella/SwiftDataCounter/documentation","language":"Swift","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/markbattistella.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":null,"code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null,"zenodo":null,"notice":null,"maintainers":null,"copyright":null,"agents":null,"dco":null,"cla":null},"funding":{"github":["markbattistella"],"custom":["https://www.paypal.me/markbattistella/5AUD","https://www.paypal.me/markbattistella/10AUD","https://www.paypal.me/markbattistella/20AUD"]}},"created_at":"2025-09-13T12:58:03.000Z","updated_at":"2026-03-08T11:32:07.000Z","dependencies_parsed_at":"2025-09-20T12:01:24.166Z","dependency_job_id":null,"html_url":"https://github.com/markbattistella/SwiftDataCounter","commit_stats":null,"previous_names":["markbattistella/swiftdatacounter"],"tags_count":9,"template":false,"template_full_name":null,"purl":"pkg:github/markbattistella/SwiftDataCounter","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/markbattistella%2FSwiftDataCounter","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/markbattistella%2FSwiftDataCounter/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/markbattistella%2FSwiftDataCounter/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/markbattistella%2FSwiftDataCounter/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/markbattistella","download_url":"https://codeload.github.com/markbattistella/SwiftDataCounter/tar.gz/refs/heads/main","sbom_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/markbattistella%2FSwiftDataCounter/sbom","scorecard":null,"host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":286080680,"owners_count":32783747,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2026-05-08T08:22:46.396Z","status":"ssl_error","status_checked_at":"2026-05-08T08:22:45.650Z","response_time":54,"last_error":"SSL_connect returned=1 errno=0 peeraddr=140.82.121.6:443 state=error: unexpected eof while reading","robots_txt_status":"success","robots_txt_updated_at":"2025-07-24T06:49:26.215Z","robots_txt_url":"https://github.com/robots.txt","online":false,"can_crawl_api":true,"host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["ios","ipados","macos","open-source","package-manager","swift","swift-package-manager","swiftdata","swiftui","tvos","visionos","watchos"],"created_at":"2025-09-24T07:10:23.025Z","updated_at":"2026-05-08T14:16:46.073Z","avatar_url":"https://github.com/markbattistella.png","language":"Swift","funding_links":["https://github.com/sponsors/markbattistella","https://www.paypal.me/markbattistella/5AUD","https://www.paypal.me/markbattistella/10AUD","https://www.paypal.me/markbattistella/20AUD"],"categories":[],"sub_categories":[],"readme":"\u003c!-- markdownlint-disable MD033 MD041 --\u003e\n\u003cdiv align=\"center\"\u003e\n\n# SwiftDataCounter\n\n[![Swift Version][Shield1]](https://swiftpackageindex.com/markbattistella/SwiftDataCounter)\n\n[![OS Platforms][Shield2]](https://swiftpackageindex.com/markbattistella/SwiftDataCounter)\n\n[![Licence][Shield3]](https://github.com/markbattistella/SwiftDataCounter/blob/main/LICENSE)\n\n\u003c/div\u003e\n\n`SwiftDataCounter` is a Swift package that provides live tracking of SwiftData model counts with optional per-model or default limits. It listens for `ModelContext` save notifications and automatically refreshes counts, while also logging changes and limit crossings using `OSLog`.\n\n## Why Use This Package?\n\n`SwiftDataCounter` is designed for apps that need to track usage of SwiftData models and enforce limits. Common use cases:\n\n- **Free vs Paid Limits**: Enforce \"up to 10 users on the free plan.\"\n- **Feature Gating**: Track counts of posts, projects, or files to unlock or disable features.\n- **Analytics**: Log model usage over time without writing boilerplate counting logic.\n- **Debugging**: Automatically observe when models exceed their limits.\n\nBy centralising count tracking and limit checks, you can keep app logic clean and consistent.\n\n\u003e [!NOTE]\n\u003e Tracked models must conform to `FetchablePersistentModel` and define a `fetchDescriptor` so that `EntityCounter` can query their counts.\n\n## Features\n\n- **Live Counts**: Automatically refreshes when the `ModelContext` saves.\n- **Per-Model Limits**: Specify limits individually or apply a default to all.\n- **Aggregate Queries**: Get combined totals and remaining capacity across all tracked models.\n\n## Installation\n\n### Swift Package Manager\n\nTo add `SwiftDataCounter` to your project, use the Swift Package Manager:\n\n1. Open your project in Xcode.\n1. Go to `File \u003e Add Packages`.\n1. Enter the repository URL:\n\n    ```url\n    https://github.com/markbattistella/SwiftDataCounter\n    ```\n\n1. Click **Add Package**.\n\n## Usage\n\n### Setup\n\nTracked models must conform to `FetchablePersistentModel` and implement a static `fetchDescriptor`:\n\n```swift\n// Extend your existing model\nextension User: FetchablePersistentModel {\n    static var fetchDescriptor: FetchDescriptor\u003cUser\u003e {\n        FetchDescriptor\u003cUser\u003e()\n    }\n}\n```\n\nTrack models with a **shared default limit**:\n\n```swift\nimport SwiftDataCounter\n\nlet counter = EntityCounter(\n    context: modelContext,\n    for: User.self, Post.self,\n    defaultLimit: 100\n)\n```\n\nOr with **per-model limits**:\n\n```swift\nlet counter = EntityCounter(\n    context: modelContext,\n    for: (User.self, 10), (Post.self, nil) // nil = unlimited\n)\n```\n\n### Per-Model Queries\n\n```swift\nlet users = counter.count(for: User.self)\n// eg. 6\n\nlet remainingUsers = counter.remaining(for: User.self)\n// eg. 4 (10 limit - 6 used)\n\nlet userLimit = counter.limit(for: Post.self)\n// preconditionFailure since Post is set to unlimited (nil)\n\nif counter.isOverLimit(for: User.self) {\n    print(\"User count is over the limit!\")\n}\n```\n\n### Aggregate Queries\n\n```swift\nlet total = counter.grandCount\n\n// Combined limit including unlimited models - can return nil\nlet combined = counter.combinedLimit(scope: .all)\n\n// Combined limit excluding unlimited models\nlet finite = counter.combinedLimit(scope: .excludingUnlimited)\n\n// Remaining total capacity\nlet remaining = counter.combinedRemaining(scope: .all)\n\nif counter.isOverAnyLimit {\n    print(\"At least one model is over its limit.\")\n}\n```\n\n### Convenience Extensions\n\nFor cleaner code, extend `EntityCounter` with typed accessors:\n\n```swift\nextension EntityCounter {\n    var userCount: Int { count(for: User.self) }\n    var userRemaining: Int { remaining(for: User.self) }\n    var userLimit: Int { limit(for: User.self) }\n    var isUserOverLimit: Bool { isOverLimit(for: User.self) }\n}\n```\n\nThen use:\n\n```swift\nif isUserOverLimit {\n    print(\"Too many users. Remaining: \\(userRemaining)\")\n}\n```\n\n## Logging\n\n`SwiftDataCounter` uses `OSLog` via `SimpleLogger`.\n\nIt automatically logs:\n\n- Model initialisation counts.\n- Count changes (old → new).\n- Limit crossings (exceeded / back under).\n\nExample log in Console.app:\n\n```log\nEntityCounter initialised. Tracking 2 models, defaultLimit = 10\nTracking User with limit 10\nTracking Post with no limit\nUser count changed from 3 to 5\nUser exceeded limit 10. Current count: 11\n```\n\n## Warnings\n\n\u003e [!WARNING]  \n\u003e The API is **strict**:\n\u003e\n\u003e - Calling `remaining(for:)` or `limit(for:)` on a model configured with `nil` (unlimited) will cause a **runtime crash** via `preconditionFailure`.  \n\u003e - This is intentional to surface incorrect usage early - if a model is unlimited, you should not be asking for its remaining capacity or limit.  \n\u003e - Always design your code so that only limited models are passed to these methods.  \n\n## Licence\n\n`SwiftDataCounter` is released under the MIT licence. See LICENCE for details.\n\n[Shield1]: https://img.shields.io/endpoint?url=https%3A%2F%2Fswiftpackageindex.com%2Fapi%2Fpackages%2Fmarkbattistella%2FSwiftDataCounter%2Fbadge%3Ftype%3Dswift-versions\n\n[Shield2]: https://img.shields.io/endpoint?url=https%3A%2F%2Fswiftpackageindex.com%2Fapi%2Fpackages%2Fmarkbattistella%2FSwiftDataCounter%2Fbadge%3Ftype%3Dplatforms\n\n[Shield3]: https://img.shields.io/badge/Licence-MIT-white?labelColor=blue\u0026style=flat\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fmarkbattistella%2Fswiftdatacounter","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fmarkbattistella%2Fswiftdatacounter","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fmarkbattistella%2Fswiftdatacounter/lists"}