One attribute storing likle array [duplicate] - ios

This question already has answers here:
Core Data with Array Attribute
(2 answers)
Closed 7 years ago.
Please I really discouraged,
Is it possible to make a single entity like array attribute?
For example :
entity = person
attribute = courses
And the entity able to contain a few courses(attribute).
i have a many persons, and i need storing many courses on one person
and i don't know where to store them, and how store them
and after to get the specific person and show the all courses...

It would be a more common design to make both person and course entities and then have a many-to-many relationship between them. That way, if something about a course changes, you don't have to make the change in every person's array.

Related

Core Data Attributes

If I'm making an Entity in Core Data to handle possible values a person can select in a questionnaire form do I have to create an attribute for every possible selectable question? For example my Entity named Person has attributes for name, date, time, and than a bunch of possible answers to select radio-button style that should be added to the Person Entity. Is it better to use a separate Entity for the questionnaire portion.
Edit for better clarity:
The app/survey form is a list of questions with a radio button style check box. If the question applies to them they touch the circle button and it fills in the circle. So its a boolean value. However I'm just not sure if I have to make each one of those questions an boolean attribute or not? This seems like a simple enough project to start learning Core Data which is the purpose of using Core Data instead of some other modeling and persistence solution.
If your properties are y/n answers, that would be boolean attribute e.g.:
Person.licensed = y/n
If your properties have more than y/n possible answer you might use a number attribute:
Person.licensed = 1(y), 0(n), -1(Unknown), -2(Ineligible)
You may need more flexible properties. Maybe there are many types of licenses:
Person.licenses --> Related Entity License with attributes- license.type, license.issueDate, license.expireDate
Then, if you are doing something like a survey, there are many other potential paths. You'll need to elaborate on what you are doing for more help.
Separating the two entities is a good idea as it avoids confusion and keeps your code cleaner. When you step away from the project and return to it 4 months later, there will be no issues discerning where data is saved and from where to retrieve it.
Your Person entity will have its attributes (name, email, etc) and the Questionnaire entity will have its attributes- yes, one for each question with a Boolean type.
Based on your comment you don't need a Boolean or any other attribute. You have a set of questions, which are just instances in the data store. You could group them into a questionnaire if you want, which would be another entity and a relationship between them. Your person is another entity, and has a to-many relationship to question. As the user ticks questions that apply to them you add those questions to the relationship.

How to store data sequentially in Core Data? [duplicate]

This question already has answers here:
How can I preserve an ordered list in Core Data
(3 answers)
Closed 6 years ago.
I have an entity called OperationEntity. It has a to-many relationship to OperationInput.
I have a table view controller that displays all the OperationEntity that are saved. When the user taps on one of the cells, a segue that goes to another table view controller is performed. That table view controller displays all the associated OperationInput of OperationEntity.
In the NSManagedObject subclasses that Xcode has generated for me, the availableInputs property is declared to be of type NSSet?. I know that sets don't maintain the order of its elements. This means that I might save the inputs in this order:
a
b
c
But when I fetch it back, I might get it in this order instead
c
a
b
but I want the inputs to be in the same order as they were saved!
How do I solve this problem?
I have thought of adding an id property to OperationInput. Each time I save a new one, I increment the id. When I fetch it, I sort the array according to the id. But I am sure there's a better way to do it than this.
Maybe changing NSSet? to NSArray? with brute force? That can't work, can it?
It is surprising that I have not found anyone asking the question on Stack Overflow!
If you select the relationship and look in the Data Model Inspector you have an option arrangement. If you set it you will have an NSOrderedSet which maintains order.

iOS Swift - Core Data examples with a complex data relationship [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
We don’t allow questions seeking recommendations for books, tools, software libraries, and more. You can edit the question so it can be answered with facts and citations.
Closed 7 years ago.
Improve this question
I'm trying to understand how to model complex data relationships with Core Data.
With my app, I currently have an entity of Recipe, Ingredient and RecipeIngredient which bind ingredients to a recipe.
I have not come across any example of fetching data out of this joint entity. I'd appreciate it if someone could give an example of an entity like my RecipeIngredient in Swift.
The reason you haven't seen examples similar to your RecipeIngredient entity is that you don't need joint entities like that in Core Data. You're treating Core Data as if it were a relational database, where you'd typically use a join table in order to create efficient many-to-many relationships between entities. As explained in the Many-to-Many Relationships sub-section of the Core Data Programming Guide, with Core Data all you need to do is to specify a to-many relationship in both directions between two entities. Note the parenthetical remark in the docs:
(If you have a background in database management and this causes you concern, don't worry: if you use a SQLite store, Core Data
automatically creates the intermediate join table for you.)
Here's an illustration of the relationship as you should model it, ripped straight from Xcode's model editor:
If you'd still like to see examples of how to do this, search for something like "Core Data many to many relationships" and you'll find plenty. You could start here on StackOverflow; a quick search turned up a number of examples, including How do you manage and use "Many to many" core data relationships?.
Update: From your comment, I understand that you want to use an intermediate object to add information about the relationship between recipes and ingredients. That is a case where another entity is warranted. So let's say your model looks like this:
It seems unlikely that you'd want to fetch one of these RecipeIngredient objects directly; you'd probably just follow the appropriate relationship. So, you might create a fetch request to find all the Recipes whose name matches #"chocolate cake". (There are plenty of examples of fetch requests using a predicate in the docs and all over the net, so I won't do that here.) Your fetch request will return an array of recipes that we could call cakeRecipes, but you're probably only interested in one:
Recipe *cake = cakeRecipes.firstObject;
Now, what do you want to know about the ingredients for your cake? Here's a list of the ingredients:
NSArray *ingredientNames = cake.ingredients.ingredient.name;
If you'd like to log the ingredient names and amounts:
for (RecipeIngredient *i in cake.ingredients) {
NSLog(#"%# %#", i.amount, i.ingredient.name);
}
Or, you could use a fetch request to find the ingredients matching "celery", storing the result in celeries. After that, you might look for recipes including celery:
Ingredient *celery = celeries.firstObject;
NSArray *recipes = celery.recipes.recipe
If this doesn't help, perhaps you could be more specific about the problem. Also, I know you asked for Swift, but my fingers are still used to Obj-C, and the language specifics don't really come into play here -- Core Data works the same in both languages.

Core Data datatypes and lists [duplicate]

This question already has an answer here:
CoreData - one-to-many modeled relationship comes out as one-to-one
(1 answer)
Closed 8 years ago.
I am new to Core Data and am in the process of integrating it into an existing iPhone application. Here is my question:
Along with the standard data types available for entity attributes, is there any way to store multiple values for an attribute, such as a list? Or can I create some custom list class and have it available as an attribute data type?
Thanks, Viv!
Along with the standard data types available for entity attributes, is
there any way to store multiple values for an attribute, such as a
list?
If you have multiple values to store for a single property, make a separate entity that holds one instance of such a value and use a to-many relationship.
If you want to have a list type attribute, of other records to store in core data, you can use a too many relationship. Otherwise, if you just want a custom style attribute, it is possible to wrap any class inside of an NSData object and then store it as a Binary Data Attribute. This being said, you will not be able to use a fetch request to search the database using properties of this data, just whether or not it exists.

Core data NSFetchedResultsController and sorting

I have two core data entities named Assignment and Question. Assignment entity has assignment details like its name, id etc. Question table has question name, id etc. Assignment has one to many relationship with Question. When I try to fetch the assignments details from server, I get assignment and question data and both Assignment and Question entities are updated in the core data store. Note that two different assignments might share same questions.
Now lets consider, assignment 1 has questions with id 1,2 and 3. Where as assignment 2 has questions with id 4, 5 and 1. In my question table it has only 5 entries as the question with id 1 is common in both the assignments.
I am using a NSFetchedResultsController to poulate this on the UI. I have an issue with fetching the questions in the same order it is added to the core data store(database)
For example: For assignment 2, I need questions in oder 4,5 and 1. There is no unique key using which I can create the sort descriptor.Without sort descriptor I can not create NSFetchRequest and without that it is not possible to create NSFetchedResultsController. Any thoughts on solving this issue?
To my mind, the best pattern would be to create a new entity, AssignmentQuestion, that has a has many-to-one relationship to Assignment and a many-to-one relationship to Question, as well as a sequence number attribute.
Assignment <--------->> AssignmentQuestion <<--------> Question
|--name |--sequence |--text
|--id |--id
|--etc. |--etc

Resources