CoreDataStack | The Big Nerd Ranch Core Data Stack
kandi X-RAY | CoreDataStack Summary
kandi X-RAY | CoreDataStack Summary
The Big Nerd Ranch Core Data Stack
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 CoreDataStack
CoreDataStack Key Features
CoreDataStack Examples and Code Snippets
Community Discussions
Trending Discussions on CoreDataStack
QUESTION
I want to perform a background fetch and pass the result to closure. Currently I'm using performBackgroundTask
method from NSPersistentContainer
which is giving a NSManagedObjectContext
as a closure. Then using that context I'm executing fetch request. When fetch is done I'm, passing the result to the completion handler.
ANSWER
Answered 2022-Feb-13 at 03:16No, it is not
Do not pass NSManagedObject instances between queues. Doing so can result in corruption of the data and termination of the app. When it is necessary to hand off a managed object reference from one queue to another, use
NSManagedObjectID
instances.
You would want something like:
QUESTION
[Xcode 12.4, Catalina 10.15.6, MacOS Project using SwiftUI and CoreData]
I'm working on an app to display some data I have. The business logic of the app is working fine, and now I've turned my attention to the UI.
In order to tidy up the UI, I would like to get the PreviewProvider
working for each view. I would also like the previews to display data I already have in the CoreData datastore.
After much frustration, I finally managed to get the preview to compile and display, however I'm not seeing any actual data in the preview. It appears that whilst the NSManagedObjectContext is there, the preview has not selected any entry to display.
How can I select, say the 3rd entry in my datastore to be displayed in the preview?
Below is my code for the view that includes the PreviewProvider
at the end (as normal) with some comments explaining it following.
ANSWER
Answered 2021-Nov-17 at 05:01I worked out what was missing in my PreviewProvider
struct:
- I needed to perform a NSFetchrequest in order to get the data.
- Then, I was able to access entries in the datastore using the array index.
Note that the index is a CoreData internal representation, and if you want a specific item, useSQLite
to browse the datastore to find the one you want to see.
QUESTION
I'm trying to fethch coredata in widget.
In viewcontroller, I succeed fetch coredata, but in widget.swift i can't get my core data.
here is my code
widget.swift
...ANSWER
Answered 2021-Sep-05 at 12:31Your context
is defined in function parameters, thus it is not a CoreData context, but a TimelineProviderContext
for the timeline.
Try fetch data with a following code as you do before adding a predicate:
QUESTION
I want to save data at viewcontroller.swift, and fetch data at widget.swift
But I don't know how to access core data in widget.
How can I access/Fetch core data in widget?
Here is my code:
in viewController.swift
...ANSWER
Answered 2021-Aug-29 at 15:49Make this "context" global
QUESTION
I have a vacation app where in the initial view controller I can get the weather of cities from an API, and when I select a row it segues to another view controller where I can add a to do list to an itinerary. I got everything working, I just can't figure out how to handle deleting rows from the itinerary. When I try to delete a row it says:
Invalid update: invalid number of rows in section 0.
Heres my code:
...ANSWER
Answered 2021-Aug-22 at 20:51I solved the issue:
QUESTION
Currently, the following is my implementation regarding CoreData.
...ANSWER
Answered 2021-Aug-17 at 16:26It's not enough to just use a background context. You need to use that context on its own queue. You checked that you're not running on the main queue, but you could be on any queue, and the background context only works on one of them. The error message you see is what Core Data says when you're using it on the wrong queue.
Any time you use backgroundContext
, you need to wrap the code in a call to perform
or performAndWait
, to ensure that your code runs on the background context's queue. Since your isExist
function is synchronous, it needs to use performAndWait
so that it can get a result before returning.
QUESTION
I know that when updating multiple rows of data, NSBatchUpdateRequest
is a recommended way, as it is faster and consumed less memory.
However, what if we are only updating 1 row? Should we choose to update using NSBatchUpdateRequest
or NSManagedObject
? Is there any rule-of-thumb to decide the choice?
NSManagedObject
...ANSWER
Answered 2021-Jun-25 at 08:06For one/few objects (that can be easily pulled into memory without issues), it is usually easier/recommended to -
- fetch
NSManagedObject
intoNSManagedObjectContext
. - Perform your updates.
- Save the context.
This saves you from having to merge the same set of changes to all other contexts in the app (which may have reference to the object being updated).
This works because NSManagedObjectContext
fires notifications on save()
calls that can be automatically observed by other contexts (if needed).
QUESTION
Is there a way, to make multiple NSBatchUpdateRequest
calls executed within a DB transaction, so that either all DB rows is updated or none is updated (When exception thrown)?
The following code illustrate the problem.
...ANSWER
Answered 2021-Jun-24 at 17:26CoreData.framework
doesn't open up SQLite
level controls to the user, it provides you NSManagedObjectContext
.
How does it work in a similar manner?
- You pull as many objects in many as you need and do your changes on them.
- When you are done with your changes, you do
context.save()
. - In that way, you save all of your changes in one shot.
In all cases, pulling all objects in memory might not be possible or a good idea, so then you need to implement your own solution around how to send all of these changes to disk.
From the NSBatchUpdateRequest
docs -
A request to Core Data to do a batch update of data in a persistent store without loading any data into memory.
When you execute this, you are doing the changes in store that you can't roll back. For a large data-set, you can do following -
- Say you have to perform a series of updates (5 different steps) on 100k records as an operation.
- Start in a background thread, pull objects in memory in batches of 1k at a time.
- You can load 1k objects easily in memory, mutate them - go through all of your changes/steps one by one and save these changes on this batch. If this is successful, you move on to the next batch.
- In case one intermediate step fails on a batch, you can then use either
NSManagedObjectContext.rollback()
orNSManagedObjectContext.reset()
depending on your implementation.
Here's a popular SO post on the differences between the two in case official docs don't provide enough clarity.
QUESTION
I'm trying to change the default commands in a macOS SwiftUI app. I would like to append some custom commands to the 'View' menu, but I can't get it to work.
This is what I tried:
...ANSWER
Answered 2021-Jan-03 at 21:33Use CommandGroup, which has init options to append or replace existing menus:
QUESTION
I want to give the user the option to toggle iCloud sync on and off.
After researching for a while, I saw that one way one could achieve this is by setting the cloudKitContainerOptions
.
So I would set it to nil
if I don't want my database to be synched.
ANSWER
Answered 2020-Dec-18 at 16:05I have been able to make it work!
My problem specifically was that I haven't updated my already fetched objects (with the old context) after reinitializing the persistenceContainer (which created a new context).
So, directly after calling setupContainer()
, a simple fetch (with the new context) for all my objects was enough.
Community Discussions, Code Snippets contain sources that include Stack Exchange Network
Vulnerabilities
No vulnerabilities reported
Install CoreDataStack
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