core data and arrays swift 2.0 - ios

I want to be able to save an array to core data. I am aware that this can't be done.
In this case, I have an entity named 'List' and an attribute called 'items'. You can see why an array would be helpful, I am able to save it with the Transformable type as an NSObject, but I can't access the individual elements once it has been pulled out of core data as an NSObject.
Can anybody tell me either how to go about saving an array in Core Data or how to use my NSObject to fix my problem

You do not want to save an array to Core Data, you want to create an entity called Item (or even ListItem) that has a one to many relationship with your entity called List. You would call the one -> many (List -> ListItem) property items and the many -> one (ListItem -> List) would be called list.
From there you can access both sides of the relationship via those properties.
That is how you should be handling this situation in Core Data.

Related

i have a doubly linked list kind of data structure in my project, how can i store it in coredata?

I have a doubly-LinkedList kind of data structure in my project:
for simplicity assume it is: like doubly LinkedList where data is some Random Id (Integer). Again the purpose of this question is not where should I use doubly LinkedList and where should I not. the purpose is how to store them in core data.
how can we store doubly LinkedList it in core data?
You can just simply have a table (ManagedObject) called ListItem with properties for previousItem and nextItem. The fist item's previousItem and the last's nextItem would be nil. You can also have another table called DoubleLinkedList or someting like it, which is referenced from ListItem.

Is it possible to fetch an object without its relationships in Core Data?

Let's say I have one entity "Person" with many attributes and one relationship "shoes" to another entity "Shoe".
The "Person" entity has thousands of "Shoe"s. I have sometimes the need to retrieve just the attributes of this person but I don't need to download all the shoes.
Is it possible to fetch only a "Person" without its relationship "Shoe"?
And consequently, is it possible to fetch the relationship "Shoe" once we already fetched the "Person"?
What you are describing is how Core Data works by default. If you use a fetch request to get a Person, then the returned object's shoes property is a “fault”. A fault is an empty shell. When you first try to access a fault's contents (its properties or, in the case of a collection, its member objects), the fault “fires” by loading its contents from the database.
Read about faults in the Core Data Programming Guide.

iOS persistent store options

would like to ask for the community's suggestions.
I need a persistent store in my iOS app.
I have considered Core Data and SQLite and both are not ideal for the following reasons.
Core Data:
unable to model certain object relationships, such as Object A has a one-to-many relationship with NSString.
//Added:
unable to model a Dictionary(or map) as an attribute for a one-to-one relationship. i.e. Object A has a one-to-one relationship to Object B (and Object B behaves like a dictionary)
SQLite:
poor interface for schema management
no clean and elegant solution for data migration between schema versions
//Added:
unable to model a Dictionary(or map) as an attribute for a one-to-one relationship. i.e. Object A has a one-to-one relationship to Object B (and Object B behaves like a dictionary)
the persistent store has to be able to support search. If anyone can suggest ways to circumvent the problems, that would help too.
Based on your limited description, Core Data would work just fine. Core Data can easily do one to many relationships. No matter what technology that you use, you still need table to table (SQLite) or Object to Object (Core Data) relationships. Nothing is going to give you an Object to String relationship.
You can do Object A to Object B where Object B has only one property which happens to be a string.

Reason for setting relationships among entities in Core Data

After learning about relationships between entities in Core Data. I don't see the real reason for setting up relationship between two entities. They can be connected separately if one of the entities contains a property that can hold another entity by having a property of type NSManagedObject.
#property (nonatomic, strong ) NSManagedObject *AssetType;
This is a concept you must understand: Core Data is not a database but it is an object graph manager and, as a second functionality, offers persistence (e.g using for example a Sqlite store).
Said this, if you have two separated entities and you need to retrieve values based on the conditions that belong to the other entity, you need to run two requests and filter the results in memory. On the contrary if you set up a relationship you can just create a request wih a specific predicate and let Core Data to retrieve the correct results for you. In addition, through relationships you can access objects that belong to another entity as simple as accessing a property object. For example, the following snippet says that based on entityA I can access a property calles someRelationship that allows to retrieve one (or more) entities of type EntityB. If someRelationship has been set up as to-many you'll receive one or more EntityB entities.
entityB = entityA.someRelationship;
The real advice is to think in terms of objects graph!!!
Further reference: Core Data Overview by objc.io.
Update 1
The other big advantage is that relationships allow you to take advantage of deletion rules and, through inverse relationships, you are able to maintain the integrity of your graph.
See Relationships and Fetched Properties.

Search by properties of two Entities (fetch from two Entities)

Let's say I have two entities, A and B. My goal is to implement the search by all properties of A and just one property of B, and to display sum of search results in one tableView. Naturally I can set fetchRequest only by one entity. But if I am understand right I can fetch not only from A but from B too if they are connected with relationships.
So what do I need to do to implement this? Does default fetch fetches all properties from A and B? If not, how can I specify fetch of all properties from A and one property of B in one fetch?
Issue solved.
At the stage of preloading data from JSON file into Core Data I am define getters for properties (prefetching data from entity)in B and setting entity properties of A equal to returned results of this getters (all this in NSManagedObject subclasses inherited from entities). So after that I am able to fetch all what I need using this getters through appropriate properties.

Resources