core-data | Sample code for the objc.io Core Data book | Learning library
kandi X-RAY | core-data Summary
kandi X-RAY | core-data Summary
This repository contains the sample code of our Core Data book:.
Support
Quality
Security
License
Reuse
Top functions reviewed by kandi - BETA
Currently covering the most popular Java, JavaScript and Python libraries. See a Sample of core-data
core-data Key Features
core-data Examples and Code Snippets
Community Discussions
Trending Discussions on core-data
QUESTION
I created a new .Net 5 project and want to use EF Core. I autogenerated multiple migration.cs files using
dotnet ef migrations add MyMigration
and want to apply them (for development and production). I know about the MigrateAsync
method so I read about how to call this method on startup
https://andrewlock.net/running-async-tasks-on-app-startup-in-asp-net-core-part-1/
but everywhere I read that this method should not be used for production since those migrations won't be executed in a single transaction (no rollback on errors).
Unfortunately there are not many resources on how to do it regardless of the environment, I found this article
One option could be a console app calling the migrations
but I wasn't able to understand the difference for this approach because it's not solving the transactional problem?
What are best practises to apply migrations during development/production?
After autogenerating migrations I'm a big fan of simplicity, does
dotnet ef database update
the job and I don't need to work with additional tools?Create a console app, generate .sql files from the migrations, install DbUp and use it for the migration part?
ANSWER
Answered 2021-Jun-01 at 23:42What works best heavily depends on how deployment pipeline works - how many environments are there before production, release cycle, what parts of deployment are automated. There are no universal "best practices" - each way of handling migrations has its own set of tradeoff to be concious about. Pick upgrade procedure according to what your needs and expectations are.
When setting up EF Core migrations for a mid-sized project (around 70 tables), I tried out few potential approaches. My observations from the process and what worked out in the end:
- You want to get a migration SQL somewhere between changing your models and deploying to production, if only to look at it in case there are any breaking changes that may cause issues on rollback. We decided on having migrations directly in project with dbcontext, and have a migration script (using
dotnet ef migrations script --idempotent
) be generated for every build that can potentially be deployed to any environment - in our case, a CI step for each push to trunk or release branch. - Putting migration SQL in version control and treating SQL as a source of truth in regards to database structure gives an ability to manually modify scripts when you want to keep some columns for backup or backwards compatibility purposes. Another option would be to consider your data model as a reference for database schema and treat migration SQL as intermediate step that is not preserved, which makes it easier to automate whole process, but requires you to handle special cases directly in your datamodel.
- Using
--idempotent
flag when generating migration script gives you a script you can reapply to a database schema regardless of what schema version it was at, having it execute only steps that were not yet executed. This means you can reapply same migration script to already migrated database without breaking schema. If you have different versions of your application running in parallel in separate environments (development, staging and production environment), it can save issues with tracking manually what migration scripts version you need to apply and in what order. - When you have migration SQL, you can use native for your database tools in order to apply them to target environment - such as
sqlcmd
for SQL Server,psql
for postgres. This also has a benefit of having separate user with higher privileges (schema modification) handle migrations, while your application works on limited privileges, that often can't touch the schema. - Applying database migrations is part of application deployment, not application startup - if you have deployment automation of some sorts, it's probably the best place to put executing migrations against target database, again - database native client is a good alternative to
DbUp
, pick whichever you prefer. Separating migrations from application startup also gives you ability to run an application against mismatched, but still compatible database schema - which comes handy when e.g. you're doing rollout deployments. - Most problems with schema upgrades come from breaking schema compatibility between versions - avoiding that requires being concious about backwards/forward compatibility when working on data model and splitting breaking changes into separate versions that keep at least single step of backwards/forwards compatibility - whether you need it depends on your project, it's something you should decide on. We run full integration test suite for previous version against current database schema and for current version against previous database schema to make sure no breaking changes are introduced between two subsequent versions - any deployment that moves multiple versions will roll out migrations one by one, with assumption that migration script or application startup can include data transformation from old to new model.
To sum up: generating migration SQL and using either native tools or DbUp on version deploy gives you a degree of manual control over migration process, and ease of use can be achieved by automating your deployment process. For development purposes, you may as well add automatic migrations on application startup, preferably applied only if environment is set to Development
- as long as every person on a team has its own development database (local SQL, personal on a shared server, filedb if you use SQL) there are no conflicts to worry about.
QUESTION
Need to split the 3rd row and have it in the below xml format.
My Excel data:
ID EMail UserGroupID Aravind Aravind@gmail.com Sports(12-34) Aravind2 Aravind2@gmail.com Sports(3-24-5),Health(5-675-85), Education(57-85-96)My XML data:
...ANSWER
Answered 2021-Jun-07 at 19:16Try something like this:
QUESTION
The problem has been solved by 'hackingwithswift' website
Just follow the steps on the link! "https://www.hackingwithswift.com/books/ios-swiftui/one-to-many-relationships-with-core-data-swiftui-and-fetchrequest"
original ask
I would like to use elements of [relationship]
But whatever I try, it doesn't work..
There is something, what I missed and, sadly I don't know what that would be until now :/
First of all, I want to show my core data model and code.
Entity: Page, Attribute: name(String), relationship: toCard
Entity: Card, Attribute: title(String), price(Double), relationship: toPage
Scenario: Page has multiple cards
One to Many (One page to many cards)
ANSWER
Answered 2021-May-02 at 13:44I solved this problem with help from this awesome 'hackingwithswift' website! Just follow the steps on the link!
Edited:
Goto .xcdatamodeld file
Click Entity -> Class -> Codezen to Manual/None
Editor -> Create NSManagedObject Subclass
Add in Page+CoreDataProperties.swift
QUESTION
I have an app that heavily uses Measurements. It is written in SwiftUI/iOS 14. I persist the data in a Core Data database. The app is working fine, until I try to implement secure coding. I made a sample app to strip everything away except the basics. It is based off of Xcode's default Core Data app with few changes. Again, I can save my measurement fine until I try to use secure coding.
The xddatamodel
, as well as the errors, is as follows:
I am using a manual codegen for the Entity and it looks like this:
...ANSWER
Answered 2021-Apr-20 at 20:24Thanks to @LeoDabus I was finally able to hammer this out. The key is to transform the Swift Measurements into NSMeasurement and use NSMeasurement in Core Data. You will need to set your custom class to an NSMeasurement
. Your transformer will be a custom value transformer. I called mine NSMeasurementValueTransformer
. Your .xcdatamodeld will look like this:
QUESTION
I have a class A that has a "to many" relationship with B. A and its Bs are fetched from a REST endpoint, we use this approach to create the objects.
A has a unique ID and some other properties (not shown in the example). B has an ID (bID) that is unique when combined with A, and a relationship to A called a. For A the constraint is "uniqueID". For B, the constraint is "a, bID" - we want each B to only exist for each combination of A and bID. The delete rule for A to its Bs is "Cascade". The merge policy for the context and all child contexts is NSOverwriteMergePolicy
.
The code looks something like this:
...ANSWER
Answered 2021-Apr-14 at 04:49I'm using the following extension to solve this problem:
QUESTION
The question is this: how do you group Core Data objects by attribute and then perform simple calculations (let's say, sum) on them?
I've found a few similar answers for this (like this one), but none seem conclusive and all are geared up towards Swift (whereas I am working on SwiftUI).
For example, I have a Core Data model that looks like this:
...ANSWER
Answered 2021-Apr-13 at 22:16You should not have brackets around the i in [i] in. Brackets like that capture a value, but you don't have a variable to capture -- you're just getting a parameter for the closure.
QUESTION
I am trying to put a CoreData model into a Swift Package to extract my model code / classes from my main codebase.
I have done the following to recreate the issue I'm having from scratch:
- Create a new swift package called SampleModelPackage (File -> New -> Swift Package)
- Created a Core Data Model Definition File (Create File -> New -> File | iOS -> Core Data -> Data Model)
- Saved it in the Sources/SampleModelPackage directory alongside the auto-generated SampleModelPackage.swift
Now - when I attempt to open the Model.xcdatamodelId file in Xcode, it just shows me a "Version" icon (see screenshot), whereas creating a Core Data Model file in a Project or Framework will allow me to edit the model.
The second / follow up question would be how would I then extend the code-gem'd model files as normal? And how would I load that model definition (normally I would use a Bundle ID and the model file name)
I have tried adding the model file to the resources array of the Package.swift file (using .process and using .copy) as described on this page: https://developer.apple.com/documentation/swift_packages/bundling_resources_with_a_swift_package
Or more generally, is this even possible? (this article https://ishabazz.dev/blog/2020/7/5/using-core-data-with-swift-package-manager seems to imply that packaging core data models like this is possible)
(I am using Xcode 12.4)
...ANSWER
Answered 2021-Apr-07 at 17:27You need to create your Core Data model in an ordinary Xcode project, then you can create your package and drag the model to it. (This is also what was done in the linked article if you look carefully even though it was not clearly described).
I believe the the best thing is to manually generate the source code files for your entities and add them to your package. You load the model using Bundle
but instead of Bundle.main
you use Bundle.module
QUESTION
I have a simple core-data/swiftui test app with 2 entities. 1: Item entity with a simple text and timestamp attributes. And a one-to-one relationship to an Editor entity. 2: editor entity with a name attribute and a one-to-many relationship to Item. In the item view I want to pick an editor from a list of stored Editors. However when I update the view (i.e. adding text to the text attribute), the list of editors to pick from grows with new and empty editor objects.
...ANSWER
Answered 2021-Mar-25 at 14:34You are creating an Editor
every time the ContentView
body
is reloaded.
Comment out this code and it won't happen anymore. How to fix it? Take persistence code out of any View
struct
it doesn't belong there.
Put it into a class
ViewModel
or a Manager
and just call methods/ pass variables with the View
actions.
QUESTION
I am a newbie to SwiftUI but making reasonable progress. I am using the latest version of Xcode 12.4 and running BigSur 11.2.1. I am at the stage where I want to use core-data but have run into an issue that I can't find a fix.
When I create the basic Xcode project I select App and macOS as the template Then I select Interface - SwiftUI, Life Cycle - SwiftUI App, Language - Swift and select Use Core Data
A new project is created and Builds and Runs without any issues. In the window that appears I can add a new item (a datestamp) by simply clicking the + Button on the top bar. So far so good. This is all vanilla apple code.
Where I am stuck :- The List - ForEach View in the ContentView won't allow any of the Entities (items) to be selected by clicking and therefore I can't find a way to delete an entry.
If I replace the Entities with an array of Text items then I can select them and delete them by using @State var selectKeeper = Set() with a selection: $selectKeeper in the List View.
Can someone please explain how to do it?
This is the vanilla code for the content view.
...ANSWER
Answered 2021-Feb-17 at 00:23You should add EditButton() and probably wrap all of this in NavitagionView might give you what you are looking for:
QUESTION
I'm trying to follow this tutorial:
https://www.avanderlee.com/swift/valuetransformer-core-data/
But I'm stumped on where (and how!) exactly to use
...ANSWER
Answered 2021-Jan-09 at 17:34Put it into init
, like
Community Discussions, Code Snippets contain sources that include Stack Exchange Network
Vulnerabilities
No vulnerabilities reported
Install core-data
Support
Reuse Trending Solutions
Find, review, and download reusable Libraries, Code Snippets, Cloud APIs from over 650 million Knowledge Items
Find more librariesStay Updated
Subscribe to our newsletter for trending solutions and developer bootcamps
Share this Page