Good afternoon. It's not the first day I've been thinking about this task. I have a one-to-many CoreData table.
I was able to link Property to Folders. That is, folders contain many properties. A piece of code at the bottom, how to link the tables
var selectedFolder: Folders?
do {
arrayPropertys = try context.fetch(Property.fetchRequest())
arrayPropertys[indexPath.row].parentFolders = selectedFolder
}
On the main screen, I need to show the properties that are in a specific folder that is pressed. The amount I can take:
arrayPropertys[indexPath.row].parentFolders?.propertys!.count
Shows the quantity correctly. For example, there are 5 properties in one folder, and 2 properties in another.
The question is that I can't pull the attributes. There is a Name attribute, but because of the type of the NSSet object, it does not want to be shown.
I can't get the values out in any way, I've tried everything, I've read on the Internet. It doesn't work. I can see it and I can't reach it
use (parentFolders?.propertys?.allObjects as? [Property]) to access the objects as an Array<Property>
Related
I have a Core Data ManagedObject (Lease) that has a one to many relationship with another object (LineItems).
I'm trying to detect when the LineItem object changes ie is added, deleted or attributes within it modified so I can recalculate a field in Lease.
I'd like to do this from the standard Lease+CoreDataClass file.
ive seen a number of solutions and doco using KVO, NotificationCenter, Combine etc. but they all seem to be done from a swiftui view and I haven't been able to make sense of it enough to implement in my CoreDataClass file. I'd like to have this independent of the view as there are many parts of my application that could change a LineItem record.
Anyone know how to do this? maybe some example code so I can understand how it works?
Edit: some extra info:
Lease has 1 attribute and a one to many relationship:
paymentDueDate (attribute)
lineItems (one to many relationship to LineItem)
LineItem has 2 attributes and an inverse relationship back to Lease:
date (attribute)
amount (attribute)
lease (inverse relationship back to Lease)
This is the code for my generated Lease+CoreDataClass file:
import Foundation
import CoreData
#objc(Lease)
public class Lease: {
func recalculatePaymentDueDate() {
//some complex code to recalculate paymentDueDate field
let result = someCalc()
self.paymentDueDate = result
}
}
Basically want to call recalculatePaymentDueDate() whenever a LineItem gets added to or removed from Lease or the amount modified.
I'm new to Swift/CoreData and SQl databases. I have a CoreDatabase with over 7000 items. I want to create an entity (or any other way) to store how often a certain entry in the DB has been used. I need this in order to create a weighted sorting algorithm that suggests certain entries.
The catch is that I do not want to store this on the entries themselves, they need to remain generic in order for me to be able to update them every now and again via my own Node server. So all users have the same DB. Whenever the user picks one of the items it's counter increments by one. Whenever I query an item the frequency should come with it so I can perform a sorting algorithm on it.
I've been reading up on articles, it seems like this can be done, but none so far have been really useful. I've also looked a SQLite articles on this but haven't found what I was looking for.
I'm thinking something along these lines:
FrequencyList { Item_1 { ...7000 items....
item_1_freq : 0, ------------> frequency : 0,
item_2_freq : 12, name: "lala"
item_3_freq : 3 ...
... };
...
7000?!?!
};
Or would a separate 'meta' entity in a one-to-one relationship with it's respective Item be a good solution?
How can I tackle this?
In Core Data it would probably be better like this:
Put the selection in an entity with a count property, and have a relationship between the selection and the item. The Selection --> Item relationship could be to-one or to-many depending on your needs; I have it as to-many here but that might not be best in your case.
If you want to get the number of selections for an Item, use the value of selection.count. Update selection.count when a selection occurs.
I would really appreciate some help with using NSFetchResultsController.
I think what I am need to do should be simple for some people, but I am a bit stuck!
I will try to explain what I am doing, or a simplified version of it.
I have 2 viewControllers, each displaying an NSTableView. I am using Core Data, with an entity Clubs. One of the viewControllers displays a list of Clubs. So for this one, I create an NSFetchResultsController, passing to it a fetchRequest which is basically NSFetchRequest<Clubs>(entityName: “Clubs”).
Each club has a list of Members, with a one-many relationship). I want to display the members using the 2nd viewController. So when I tap a club in the first table, I want the second table to show its members.
I have an NSFetchResultsController connected to the second table. I am stuck at what fetchRequest to send to it. If I use a similar one to the first, i.e. NSFetchRequest<Members>(entityName: “Members”), as expected, I end up with one table showing all of the clubs stored, and the second table showing ALL members stored, whereas I want only the members in the club selected in the first table. I hope that makes sense. So my question is, what fetchRequest should I send to the 2nd tables NSFetchResultsController?
You should add the following predicate to the fetch request underlying the FRC:
fetchRequest.predicate = NSPredicate(format:"club == %#", theClubSelectedInTheFirstViewController)
I have 2 entries
1. category (main one)
2. info
the relationship is 1 to many , for every category there is several info's
after i added the several info classes to each categoty
i want to get each category relationship list in the detailsviewcontroller
I created a temp class in the details view the contain the selected category
how do i get access to the list of INFO'S ??
Without seeing your code or model classes, I can't give you the exact answer. But when you made your model, you probably named your relationship something like "infosForCategory". When you generated the model it made a NSMutableSet for that 1-to-many relationship. In that case, you can access the list of infos with:
NSMutableSet *myInfos = myCategory.infosForCategory;
I'm not exactly sure what you're asking but it sounds like you already have an instance of Category and you want to retrieve all the related instances of Info. In that case, Category should have an automatically generated info property that is of the NSSet type. That set will contain all of the related Info objects.
I have used CoreData a couple of times but data modeling was very simple. This time I am supposed to implement the following:
User can create documents and folders (they don't have to be real folders/directories).
Folders can contain documents or other folders
Documents have:
1 Title,
1 Description and
8 children
Each child also has a Title and a Description and could have 8 children
(optionally)
This could be as many levels (recursive) as the user desires.
I am not sure how to express this in CoreData. Could someone give a hand here?
Right now I think of:
"Child" entity with "Title" "Description" as attributes and "Children" as a relationship (one to many).
"File" entity with "IsFolder" as a boolean attribute and "Documents" as a relationship (one to many, pointing to "Child")
I am not sure if this well implements above structure in CoreData.
I am in the right path? Re-modeling data structure in CoreData could be painful (I've heard) so i would like to have a good structure from the beginning. I hope I can get some advice from you;)
Thanks in advance.
You probably want something like this (pseudocode):
Folder{
parent<<--(required,nullify)-->Folder.folders
folders<--(optional,cascade)-->>Folder.parent
documents<--(optional,cascade)-->>Document.folder
}
Document{
title:string
descriptionText:string
parent<<--(optional,nullify)-->Document.children
children<--(optional,cascade)-->>Document.parent
folder<<--(optional,nullify)-->Folder.document
}
(Word of warning: Don't ever use "description" as an attribute name. NSObject has a description method so every subclass of NSObject responds to the description message. It will cause all kinds of ugly problems if you have an attribute accessor of the same name.)