i'm trying to store my api data in core data entity. I have some custom data types array also in them. So i made those attributes transformable. Like this,
Now when i created the NSManagedObject class of my entity, it's showing me error,
#NSManaged property cannot have an initial value.
My SingleChat+CoreDataProperties.swift class is this,
extension SingleChat {
#nonobjc public class func fetchRequest() -> NSFetchRequest<SingleChat> {
return NSFetchRequest<SingleChat>(entityName: "SingleChat")
}
#NSManaged public var name: String?
#NSManaged public var roomSID: String?
#NSManaged public var isGroup: Bool
#NSManaged public var lastMessage: String?
#NSManaged public var lastMsgTime: String?
#NSManaged public var lastMsgTimeActual: String?
#NSManaged public var profilePic: String?
#NSManaged public var lastMsgRead: Bool
#NSManaged public var unReadMsgsCount: Int16
#NSManaged public var actualNameFor_1_2_1_chat: String?
#NSManaged public var isNewGroup: Bool
#NSManaged public var members = [TCHMember]()
#NSManaged public var messages = [TCHMessage]()
#NSManaged public var twChannelObj: TCHChannel?
#NSManaged public var group_info: [String:JSON]?
}
I'm new to core data and does not know how i can get custom data types. How i can get resolve this issue, I want to store data in members, messages and group_info. These are arrays.
Related
I may be simply understanding this incorrectly, but when I created an Exercise entity and looked at it contents in the debugger I get this repeating reference to the Entity type.
The Exercise+CoreDataProperties file is standard:
import Foundation
import CoreData
extension Exercise {
#nonobjc public class func fetchRequest() -> NSFetchRequest<Exercise> {
return NSFetchRequest<Exercise>(entityName: "Exercise")
}
#NSManaged public var exerciseDuration: Int16
#NSManaged public var repeatNo: Int16
#NSManaged public var restDuration: Int16
#NSManaged public var exerciseName: String
#NSManaged public var id: UUID
#NSManaged public var associatedWorkout: NSSet?
}
// MARK: Generated accessors for associatedWorkout
extension Exercise {
#objc(addAssociatedWorkoutObject:)
#NSManaged public func addToAssociatedWorkout(_ value: WorkoutSet)
#objc(removeAssociatedWorkoutObject:)
#NSManaged public func removeFromAssociatedWorkout(_ value: WorkoutSet)
#objc(addAssociatedWorkout:)
#NSManaged public func addToAssociatedWorkout(_ values: NSSet)
#objc(removeAssociatedWorkout:)
#NSManaged public func removeFromAssociatedWorkout(_ values: NSSet)
}
extension Exercise : Identifiable {
}
And here is the xcdatamodeld config:
Any ideas why I cannot view the values of the object in debug mode once I've created it? Have I done something in my code to cause this repetition?
The variables view you're using can't see into the values of properties of managed objects (or of most objects). If you want to inspect the property values, use the debug console. When you're stopped on the line in your screenshot, you should be able to use po newExercise to see properties of newExercise, or you can use commands like po newExercise.exerciseName to see individual property values.
I'm storing data in a CoreData entity that uses relationships. By default a toMany type relationship will be of typeNSSet. In order to load this NSSet into a tableview in the order in which the items where added to this NSSet I need to convert it to an array. How do I go about achieving this?
NSManaged Subclass generated:
extension Node {
#nonobjc public class func fetchRequest() -> NSFetchRequest<Node> {
return NSFetchRequest<Node>(entityName: "Node")
}
#NSManaged public var value: String?
#NSManaged public var children: NSSet?
#NSManaged public var parent: Node?
#objc(addChildrenObject:)
#NSManaged public func addToChildren(_ value: Node)
#objc(removeChildrenObject:)
#NSManaged public func removeFromChildren(_ value: Node)
#objc(addChildren:)
#NSManaged public func addToChildren(_ values: NSSet)
#objc(removeChildren:)
#NSManaged public func removeFromChildren(_ values: NSSet)
}
Note: As CloudKit doesn't support ordered relationships, I can't use ordered arrangement as a part of the solution.
My suggestion is to declare the relationship as non-optional native Swift type
#NSManaged public var children: Set<Node>
To get an array just sort the set. This is pretty easy with a Swift Set. As a set is unordered by definition you have to do that anyway to get a fixed order. Assuming you have a dateAdded attribute you can write
let sortedChildren = children.sorted{$0.dateAdded > $1.dateAdded}
I in my current project I want to save a custom data type in core data but I don't know to do it.
The Core Data Entity:
extension WorkoutBuy {
#nonobjc public class func fetchRequest() -> NSFetchRequest<WorkoutBuy> {
return NSFetchRequest<WorkoutBuy>(entityName: "WorkoutBuy")
}
#NSManaged public var beschreibung: String?
#NSManaged public var dauerInterval: Int64
#NSManaged public var dauerPause: Int64
#NSManaged public var dauerPauseDurchgang: Int64
#NSManaged public var durchgaenge: Int64
#NSManaged public var id: UUID?
#NSManaged public var intensitaet: String?
#NSManaged public var name: String?
#NSManaged public var price: Double
#NSManaged public var catagory: Catagory? !!!//here is my own data type !!!
}
extension WorkoutBuy : Identifiable {
}
My own data type:
struct Catagory: Codable, Identifiable{
let id: Int
let localizedString: String
let engishName: String?
}
When I do it this way I get this error:
Property cannot be declared public because its type uses an internal type
And:
Property cannot be marked #NSManaged because its type cannot be represented in Objective-C
Does anyone have a solution? Thank you in advance.
PS: I use the AppDelegate to create the persistentContainer
EDIT
When Catagory is a own core data entity the app is starting but when I add an object I got the following error like 10 times in the console:
2020-12-04 21:58:31.586858+0100 IntervallTraining[4869:1923447] [error] error: SQLCore dispatchRequest: exception handling request: <NSSQLSaveChangesRequestContext: 0x2812c9380> , threw while encoding a value. with userInfo of (null)
CoreData: error: SQLCore dispatchRequest: exception handling request: <NSSQLSaveChangesRequestContext: 0x2812c9380> , threw while encoding a value. with userInfo of (null)
Ein Core Data-Fehler ist aufgetreten.
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 2 years ago.
Improve this question
I have two entities Transaction and Account. one Account holds many transactions. I use this piece of code to calculate the sum of all transactions (of all accounts):
let sum = transactions.map { $0.value }.reduce(0, +)
But I don't know how to sum transactions that belongs to each account. I have a ForEach that shows each account and I want to put also the sum of their transactions. Can someone point me to the right direction?
Edit:
Heres the core data model:
Account+CoreDataProperties.swift
#NSManaged public var bank: String?
#NSManaged public var name: String?
#NSManaged public var iban: String?
#NSManaged public var city: String?
#NSManaged public var postal: String?
#NSManaged public var street: String?
#NSManaged public var country: String?
#NSManaged public var transaction: NSSet?
public var wrappedName: String
name ?? "Unknown Account"
}
//...
Transaction+CoreDataProperties.swift
#NSManaged public var category: String?
#NSManaged public var comment: String?
#NSManaged public var date: Date?
#NSManaged public var name: String?
#NSManaged public var value: Double
#NSManaged public var account: Account?
public var wrappedName: String {
name ?? "Unknown Transaction"
}
public var wrappedComment: String {
comment ?? "Unknown Comment"
}
//...
It appears that each account records all of its transactions; iterate through the accounts and sum their transactions.
for account in accounts {
let sumForThisAccount = account.transactions.map { $0.value }.reduce(0, +)
}
How can the #NSManaged properties be in an extension when extensions can't contain stored properties? Why are they not the same as stored properties?
A popular format for core data files is a core data class, and then an extension containing the core data properties.
//User+CoreDataClass
#objc(User)
public class User: NSManagedObject {
var fullName: String {
return "\(firstName) \(lastName)"
}
}
//User+CoreDataProperties
extension User {
// Why can these #NSManaged properties live in an extension?
#NSManaged public var id: Int64
#NSManaged public var firstName: String
#NSManaged public var lastName: String
var storedProp: String // Error: Extensions must not contain stored properties
}
Technically #NSManaged properties are computed properties.
Core Data synthesizes getter and setter.