core-data

Swift - Sorting an array retrieved from Core Data [duplicate]

只愿长相守 提交于 2021-02-07 10:32:39
问题 This question already has an answer here : Sorting Array received from Core Data in Swift (1 answer) Closed 6 years ago . I want to sort the array with usernames that I retrieve from my core data. I retrieve it like this: override func viewDidAppear(animated: Bool) { let appDel:AppDelegate = UIApplication.sharedApplication().delegate as AppDelegate let context:NSManagedObjectContext = appDel.managedObjectContext! let freq = NSFetchRequest(entityName: "User") let en = NSEntityDescription

How to Save a Generic Measurement<Unit> in Core Data?

十年热恋 提交于 2021-02-07 09:28:57
问题 How does one save and retrieve a generic Measurement in Core Data? What I'm looking to do is save either a Measurement<UnitMass> or a Measurement<UnitVolume> . As can be seen in the image below CoreData is set to accept a Generic Measurement<Unit> However I'm getting an error when I go to set the value of the measure. Saying that I'm not allowed to do this. I thought the purpose of generics was to support uses like this. What am i missing? 回答1: The error about mismatched types isn't the

SwiftUI reorder CoreData Objects in List

霸气de小男生 提交于 2021-02-07 07:16:53
问题 I want to change the order of the rows in a list that retrieves objects from the core data. Moving rows works, but the problem is that I can't save the changes. I don't know how to save the changed Index of the CoreData Object. Here is my Code: Core Data Class: public class CoreItem: NSManagedObject, Identifiable{ @NSManaged public var name: String } extension CoreItem{ static func getAllCoreItems() -> NSFetchRequest <CoreItem> { let request: NSFetchRequest<CoreItem> = CoreItem.fetchRequest()

SwiftUI reorder CoreData Objects in List

限于喜欢 提交于 2021-02-07 07:16:39
问题 I want to change the order of the rows in a list that retrieves objects from the core data. Moving rows works, but the problem is that I can't save the changes. I don't know how to save the changed Index of the CoreData Object. Here is my Code: Core Data Class: public class CoreItem: NSManagedObject, Identifiable{ @NSManaged public var name: String } extension CoreItem{ static func getAllCoreItems() -> NSFetchRequest <CoreItem> { let request: NSFetchRequest<CoreItem> = CoreItem.fetchRequest()

Crash when adopting NSSecureUnarchiveFromDataTransformer for a Transformable property

喜你入骨 提交于 2021-02-07 06:47:15
问题 In iOS 12 Apple introduced NSSecureUnarchiveFromDataTransformerName for use on CoreData model entities' Transformable properties. I used to keep the Transformer Name field empty, which implicitly used NSKeyedUnarchiveFromDataTransformerName . This transformer is now deprecated, and keeping the field empty in the future will mean NSSecureUnarchiveFromDataTransformerName instead. In iOS 13, if that field is empty, you now get a runtime warning telling you the aforementioned. I couldn't find any

Forcing NSPersistentContainer change Core Data

痞子三分冷 提交于 2021-02-06 09:13:44
问题 I just added the option for a user to toggle Cloud sync inside my app where I save whether or not the user wants to use iCloud sync in UserDefaults under "useCloudSync". I load my persistentContainer when the app runs with: class CoreDataManager { static let sharedManager = CoreDataManager() private init() {} lazy var persistentContainer: NSPersistentContainer = { var useCloudSync = UserDefaults.standard.bool(forKey: "useCloudSync") let containerToUse: NSPersistentContainer? if useCloudSync {

Forcing NSPersistentContainer change Core Data

人走茶凉 提交于 2021-02-06 09:13:32
问题 I just added the option for a user to toggle Cloud sync inside my app where I save whether or not the user wants to use iCloud sync in UserDefaults under "useCloudSync". I load my persistentContainer when the app runs with: class CoreDataManager { static let sharedManager = CoreDataManager() private init() {} lazy var persistentContainer: NSPersistentContainer = { var useCloudSync = UserDefaults.standard.bool(forKey: "useCloudSync") let containerToUse: NSPersistentContainer? if useCloudSync {

Problem saving data, using CoreData with SwiftUI

你离开我真会死。 提交于 2021-02-05 11:15:29
问题 I just started using CoreData with SwiftUI . After following this series of tutorial. And I am facing a situation where I cannot save data as I expect. Here is the relevant code: struct MyView: View { ...... @Environment(\.managedObjectContext) var managedObjectContext func setData() { print(#function) ..... let myData = SomeEntity(context: self.managedObjectContext) myData.name = "myName" myData...... do { try self.managedObjectContext.save() } catch let error { // Handle the Core Data error

CoreData: why not refresh UI when book.isPublic toggled

我与影子孤独终老i 提交于 2021-02-05 07:48:26
问题 '@Environment(.managedObjectContext) var context' will monitor the changes of context. When I click the button to toggle isPublic, I think it will cause to UI refreshing, Button text will change, But not, WHY? struct BookInfoView: View { @Environment(\.managedObjectContext) var context var isPublic: Bool { return book.isPublic } let book: Book var body: some View { print("refresh"); return Group { Button(action: onPublicToggled) { Text(isPublic ? "private" : "public") } } } func

CoreData: why not refresh UI when book.isPublic toggled

為{幸葍}努か 提交于 2021-02-05 07:46:36
问题 '@Environment(.managedObjectContext) var context' will monitor the changes of context. When I click the button to toggle isPublic, I think it will cause to UI refreshing, Button text will change, But not, WHY? struct BookInfoView: View { @Environment(\.managedObjectContext) var context var isPublic: Bool { return book.isPublic } let book: Book var body: some View { print("refresh"); return Group { Button(action: onPublicToggled) { Text(isPublic ? "private" : "public") } } } func