core-data

Handling duplicate entries in Core Data

为君一笑 提交于 2020-12-28 07:39:32
问题 I have an app that allows users to save favorites. I am using Core Data to store the favorites as managed objects. I have written some code to prevent the possibility of storing duplicates, but am wondering if there is a better way to do so. Each favorite object has an ID field that is unique. In the following code I am simply looping through and checking the ID field, and if the value already exists, setting a flag value to true, and breaking out of the loop. -(BOOL)addFavorite{ BOOL

Handling duplicate entries in Core Data

一曲冷凌霜 提交于 2020-12-28 07:38:42
问题 I have an app that allows users to save favorites. I am using Core Data to store the favorites as managed objects. I have written some code to prevent the possibility of storing duplicates, but am wondering if there is a better way to do so. Each favorite object has an ID field that is unique. In the following code I am simply looping through and checking the ID field, and if the value already exists, setting a flag value to true, and breaking out of the loop. -(BOOL)addFavorite{ BOOL

NSPersistentCloudKitContainer: How to check if data is synced to CloudKit

末鹿安然 提交于 2020-12-27 08:39:05
问题 I have implemented NSPersistentCloudKitContainer to get my data synced to CloudKit , I would like to know that the sync is finished and there is no other change pending to be synced. When I tried reinstalling the app, I start getting my data back from CloudKit and it started printing certain logs in the console. It takes around 30 seconds to get all my data back from the CloudKit. Some of the logs mention about NSCloudKitMirroringDelegate . It looks like NSCloudKitMirroringDelegate knows

Using SwiftUI, Core Data, and one-to-many relationships, why does the list not update when adding a row on the Many side

流过昼夜 提交于 2020-12-26 10:59:07
问题 I have a SwiftUI project with Core Data. The data model is a simple one-to-many and two primary views which each have a textfield at the top and a button to add a new item to the list view below. The first view is for the One side of the relation and the second for the Many. So, the NavigationLink in the first opens the second and passes the One object. Pretty standard stuff, it would seem. The methodology for creating the One works and the list below gets updated immediately when the managed

Using SwiftUI, Core Data, and one-to-many relationships, why does the list not update when adding a row on the Many side

笑着哭i 提交于 2020-12-26 10:58:13
问题 I have a SwiftUI project with Core Data. The data model is a simple one-to-many and two primary views which each have a textfield at the top and a button to add a new item to the list view below. The first view is for the One side of the relation and the second for the Many. So, the NavigationLink in the first opens the second and passes the One object. Pretty standard stuff, it would seem. The methodology for creating the One works and the list below gets updated immediately when the managed

CoreData merge conflict shows managed object version change not data

时光怂恿深爱的人放手 提交于 2020-12-26 07:19:17
问题 I am have an entity in core data and I try to update it from two different context. I am storing managedObjectID of my managed object which I need to update as it is thread safe. Before updating my object I refresh the object to avoid merge conflict. Here is my code: context1.performBlock { let myObject = context1.objectWithID(managedObjectId) context1.refreshObject(myObject, mergeChanges: true) myObject.property1 = newValue } Notice that I haven't saved the context here, as I want to commit

Fetch data from CoreData for iOS 14 widget

浪尽此生 提交于 2020-12-22 06:26:20
问题 I want to display data fetched from Core Data in a widget. But @FetchRequest doesn’t work on widgets. As I understand, we have to create an app group and make a shared persistent container. What I want to know is how to read (fetch) data on widgets from that shared persistent container or simply, how to display data fetched from Core Data in widgets . 回答1: First you need to create an AppGroup which will be used to create a Core Data Persistent Container (here is a good explanation how to do

Fetch data from CoreData for iOS 14 widget

若如初见. 提交于 2020-12-22 06:26:13
问题 I want to display data fetched from Core Data in a widget. But @FetchRequest doesn’t work on widgets. As I understand, we have to create an app group and make a shared persistent container. What I want to know is how to read (fetch) data on widgets from that shared persistent container or simply, how to display data fetched from Core Data in widgets . 回答1: First you need to create an AppGroup which will be used to create a Core Data Persistent Container (here is a good explanation how to do

SwiftUI: Cannot dismiss sheet after creating CoreData object

谁说我不能喝 提交于 2020-12-15 07:40:26
问题 I am trying to create a very basic app with two views. The main view displays CoreData objects in a NavigationView and has a toolbar button to open an "add item" modal sheet. The "add item" sheet should be able to insert a new CoreData object and dismiss itself. What I am observing is that the modal sheet can be dismissed as long as it doesn't insert a CoreData object, but once it does interact with CoreData, all my attempts to dismiss the sheet no longer work. Also, when the sheet creates a

Deleting CoreData Item which is also an @ObservedObject in DetailView causing App Crash in Swift 5

落爺英雄遲暮 提交于 2020-12-15 05:27:54
问题 i am currently working with SwiftUI and CoreData . Situation: I have a User Detail View with a Delete Button at the Bottom. When pressed, the Core Data Entry of the User is getting deleted and the App Navigation goes back to the Root Navigation View, which is a List of all Users. Problem: Every Time the Delete Button is clicked the App crashes with the following Error Message : *** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[User timeCreated]: