{"id":17569058,"url":"https://github.com/riywo/preloadedpersistentcontainer","last_synced_at":"2025-09-05T23:34:26.634Z","repository":{"id":66617105,"uuid":"144826493","full_name":"riywo/PreloadedPersistentContainer","owner":"riywo","description":"A Framework to extend NSPersistentContainer with preloaded SQLite data. This supports both macOS for build phase and iOS for runtime.","archived":false,"fork":false,"pushed_at":"2018-09-18T09:19:19.000Z","size":25,"stargazers_count":5,"open_issues_count":0,"forks_count":0,"subscribers_count":2,"default_branch":"master","last_synced_at":"2025-03-30T09:11:36.376Z","etag":null,"topics":["carthage","coredata","framework","ios","swift"],"latest_commit_sha":null,"homepage":"","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/riywo.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE.txt","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}},"created_at":"2018-08-15T08:24:18.000Z","updated_at":"2020-09-15T03:03:10.000Z","dependencies_parsed_at":"2024-04-18T16:32:43.631Z","dependency_job_id":null,"html_url":"https://github.com/riywo/PreloadedPersistentContainer","commit_stats":null,"previous_names":[],"tags_count":4,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/riywo%2FPreloadedPersistentContainer","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/riywo%2FPreloadedPersistentContainer/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/riywo%2FPreloadedPersistentContainer/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/riywo%2FPreloadedPersistentContainer/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/riywo","download_url":"https://codeload.github.com/riywo/PreloadedPersistentContainer/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":251304165,"owners_count":21567828,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","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":["carthage","coredata","framework","ios","swift"],"created_at":"2024-10-21T17:08:58.848Z","updated_at":"2025-04-28T11:28:36.553Z","avatar_url":"https://github.com/riywo.png","language":"Swift","funding_links":[],"categories":[],"sub_categories":[],"readme":"# PreloadedPersistentContainer\n[![GitHub license](https://img.shields.io/badge/license-MIT-lightgrey.svg)](https://raw.githubusercontent.com/riywo/PreloadedPersistentContainer/master/LICENSE.txt)\n[![Carthage compatible](https://img.shields.io/badge/Carthage-compatible-4BC51D.svg?style=flat)](https://github.com/Carthage/Carthage)\n\nA Framework to extend `NSPersistentContainer` with preloaded SQLite data. This supports both macOS for build phase and iOS for runtime.\n\n## Use cases\n\n- Preload multple records at build phase, then provide search experience to customers\n\n## Install\n\nUsing `Carthage`:\n\n```sh\n$ cat Cartfile\ngithub \"riywo/PreloadedPersistentContainer\"\n\n$ carthage update\n*** Fetching PreloadedPersistentContainer\n*** Checking out PreloadedPersistentContainer at \"v0.1.0\"\n*** xcodebuild output can be found in /var/folders/29/mmyrpb0d5g39glgdcv9x4z780000gn/T/carthage-xcodebuild.PBBnLr.log\n*** Building scheme \"PreloadedPersistentContainer iOS\" in PreloadedPersistentContainer.xcodeproj\n*** Building scheme \"PreloadedPersistentContainer macOS\" in PreloadedPersistentContainer.xcodeproj\n```\n\nNow, you can use Framework:\n\n```\n./Carthage/Build\n├── Mac\n│   ├── PreloadedPersistentContainer.framework\n│   └── PreloadedPersistentContainer.framework.dSYM\n└── iOS\n    ├── PreloadedPersistentContainer.framework\n    └── PreloadedPersistentContainer.framework.dSYM\n```\n\n## Usage\n\n### 1. Create a macOS CLI target.\n\nFile -\u003e New -\u003e Target -\u003e macOS Command Line Tool\n\n### 2. Associate each Framework to iOS App and macOS CLI.\n\nAdd to Linked Frameworks and Libraries\n\n### 3. Add macOS CLI to target memberships of your Core Data model, related classes, etc.\n\nOpen file -\u003e Show the File inspector (right pane) -\u003e Check macOS CLI in Target Membership\n\n### 4. Edit scheme of iOS App to add macOS CLI into its build targets before iOS App. Uncheck Parallelize Build.\n\nProduct -\u003e Scheme -\u003e Manage Schemes -\u003e Edit Your iOS scheme -\u003e Build -\u003e Add macOS CLI -\u003e Move it before iOS App -\u003e Uncheck Parallelize Build\n\n### 5. Add run script phase to iOS App build phases.\n\nOpen Buid Phases of your iOS App target -\u003e Add Run script -\u003e Move it after Compile Sources -\u003e Paste command below\n\n```sh\n${BUILT_PRODUCTS_DIR}/../${CONFIGURATION}/YourMacOSCLI\n```\n\n### 6. Edit macOS CLI's Build Settings to link libraries\n\nFramework with CLI is tricky. It requires additional steps to load Framework. See [How to add a dynamic Swift framework to a Command Line Tool](https://medium.com/livefront/how-to-add-a-dynamic-swift-framework-to-a-command-line-tool-bab6426d6c31)\n\nIn summary, because Swift stdlibs are already embedded in macOS Framework, you only need to do the steps below:\n\n- Add `Runpath Search Paths`\n    - `$(PROJECT_DIR)/Carthage/Build/Mac/` (to find Framework itself)\n    - `$(PROJECT_DIR)/Carthage/Build/Mac/PreloadedPersistentContainer.framework/Versions/Current/Frameworks` (to find Swift stdlibs)\n- Add `User-Defined` to dynamically link Swift stdlibs\n    - `SWIFT_FORCE_DYNAMIC_LINK_STDLIB` = `YES`\n    - `SWIFT_FORCE_STATIC_LINK_STDLIB` = `NO`\n\n### 7. Write preload functions in macOS CLI.\n\nIf this CLI runs on build phase of iOS App, it preloads SQLite files in you iOS App main bundle (`Your.app/YourModel.sqlite*`).\n\n```swift\nimport CoreData\nimport PreloadedPersistentContainer\n\nlet container = NSPersistentContainer(name: \"YourModel\")\ncontainer.loadPersistentStoresWithPreload { (storeDescription, error) in\n    if let error = error {\n        fatalError(\"Failed to load store: \\(error)\")\n    }\n}\n\nlet entity = YourEntity(context: container.viewContext)\nentity.id = 1\ntry! container.viewContext.save()\n```\n\n### 8. Use `loadPersistentStoresWithPreload()` in your iOS App.\n\nMaybe in AppDelegate.swift, replace `loadPersistentStores()` with `loadPersistentStoresWithPreload()`.\n\nThen, the bundled SQLite files (`Your.app/YourModel.sqlite*`) will be automatically read.\n\n```swift\nimport CoreData\nimport PreloadedPersistentContainer\n\nlazy var persistentContainer: NSPersistentContainer = {\n    let modelName = \"YourModel\"\n    let container = NSPersistentContainer(name: modelName)\n    container.loadPersistentStoresWithPreload(completionHandler: { (storeDescription, error) in\n        if let error = error as NSError? {\n            fatalError(\"Unresolved error \\(error), \\(error.userInfo)\")\n        }\n    })\n    return container\n}()\n```\n\n### 9. Build iOS App.\n\nHave fun!\n\n## References\n\n- [Creating Swift frameworks for iOS, OS X and tvOS with Unit Tests and Distributing via CocoaPods and Swift Package Manager](https://www.enekoalonso.com/articles/creating-swift-frameworks-for-ios-osx-and-tvos)\n- [Core Data: How to Preload Data and Use Existing SQLite Database](https://appcoda.com/core-data-preload-sqlite-database/)\n- [Core Data: Automate master data preloading \\- The Cookbook](https://tundrax.github.io/blog/2013/05/06/core-data-automate-master-data-preloading/)\n- [How to add a dynamic Swift framework to a Command Line Tool](https://medium.com/livefront/how-to-add-a-dynamic-swift-framework-to-a-command-line-tool-bab6426d6c31)\n\n\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Friywo%2Fpreloadedpersistentcontainer","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Friywo%2Fpreloadedpersistentcontainer","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Friywo%2Fpreloadedpersistentcontainer/lists"}