Saving a Set into Core Data - swift

Hi I'm currently trying to set up core data in my project after previously using NSCoding.
I have the problem that I can't save a Set into Core Data. I've looked for days on ways to convert it to Binary data in Swift and saving that but I can't seem to find anything.
The set contains elements of a custom object I made.
PS: If possible, also, how would a generic be saved into Core Data?

Swift sets are bridged to the foundation class NSSet, and NSSet conforms to the NSCoding protocol. That means you can save a set as an attribute of a managed object if you
Treat the set as an NSSet (if it's not one already, use mySet as NSSet)
Adopt NSCoding on your custom objects in the set
Make the Core Data attribute type "transformable"
If you do all that, Core Data will automatically invoke NSCoding methods on the set, which will in turn invoke the same methods on your custom objects. You'll just assign the set to the attribute, and Core Data will do the rest.

Related

Add an array of custom objects to Core Data

I've searched all over StackOverflow already and it seems there are a few posts on this topic but either they are outdated or the solutions don't actually address the problem specifically.
I have a custom class called ProductDataModel. Then I have an array of these custom class objects of ProductDataModel. I want to save this array to coreData as an attribute. I have it set to the type of Transformable, but when I try to save it, the program crashes.
How do I save an array of custom objects to a core data property?
I have already made the class model inherit from NSObject.

Transformable Collection in Core Data with custom Objects

I have to store a collection of custom objects (Dictonary) in Core Data Database.
So far so good. The Dictonary is stored and can be loaded without problems as a "Transformable" object.
The custom Object holds properties, but these are nil after loading them from the Database.
After searching a lot, I haven't found anything for this problem.
It seems that the properties are not getting stored in this way. (Maybe because only the address is stored and not the data??)
Sure it would be better to store an object of Core Data supported datatypes, but in this case the transformable Object is just fine and saves me a lot work and time.
Thank U!
The whole idea of transformable objects is covered in the Core Data Guide. Note that this uses a keyed archiver / unarchiver to create a NSData object from your object or the reverse. This means your customer objects my adhere to NSCoding, and encode all the information in them when asked to as well as handle unencoding.
If your are not doing this now this is the root cause of your problem. What I suggest you do is adopt NSCoding in one custom object, then verify that in fact you can encode it to a NSData object, then from the object unencode it and get the same object back. When you have that working you can then test with Core Data.

what is the use with the transfarmable in the xcdatamodal extension

hi all just now i started database work for my app.my idea is to save some rectangles(x,y,width,hight individually)as int32 and nsstring as string upto that my work completed .Now my question here is, what is the use with the "transfarmable" type where we can use this. Thanks in advance for your answer.
I'm not sure about transfarmable, but Transformable is:
The idea behind transformable
attributes is that you access an
attribute as a non-standard type, but
behind the scenes Core Data uses an
instance of NSValueTransformer to
convert the attribute to and from an
instance of NSData. Core Data then
stores the data instance to the
persistent store.
By default, Core Data uses the
NSKeyedUnarchiveFromDataTransformerName
transformer, however you can specify
your own transformer if you want. If
you specify a custom transformer, it
must transform an instance of the
non-standard data type into an
instance of NSData and support reverse
transformation. You should not specify
a name if you are using the default
transformer.
as taken from http://developer.apple.com/library/mac/#documentation/Cocoa/Conceptual/CoreData/Articles/cdNSAttributes.html.
Essentially, if you want a custom construct to be serialized into the database and then reconstituted as your desired object/object graph, the Transformable type lets you access Core Data's framework for storing complex formats/objects in the database, letting you code the logic that does the serialization and deserialization.

iPhone: Core Data save Class object

I have an entity in core data called Location. Inside this I have a few fields, such as date. But, I would also like to save a class object in it that I created called Annotation. What type of attribute would I use for this, since it is a custom class object that I created?
Location (object)
|__ Date
|__ Annotation (MKAnnotation protocol)
You have two options:
If your Annotation class conforms to the NSCoding protocol (or if you're willing to write an NSValueTransformer to convert your custom class to an NSData instance, you can use a transformable attribute in your Core Data entity. Core Data will use the designated NSValueTransformer to automatically serialize/deserialize your Annotation instance for you.
You can create an Annotation entity in your Core Data model. You'll have to write your own code to assign a CLLocationCoordinate2D to the entity. You would probably create a persistent backing using two doubles and then write setters/accessors for the CLLocationCoordinate2D.
The advantage of (1) is that it's easier (if your class conforms to NSCoding). The advantage of (2) is that you can query against the data within the entity, even if using SQLite persistent stores. If you use (1), the data is opaque to the SQLite query engine, so you won't be able to query against it with a SQLite backend.
it would be easier to add the class as a core data class then you could just have a relationship between the two classes as a one-to-one relation. you can however use the type binary data as an attribute type and store whatever data you want in there. ( i use this to store c structs sometimes ) You just need to use the NSData class to wrap your object and set the property.

NSManagedObject subclass outside of managed object as a normal object

I have an entity object Country with country name and country code. It is a subclass of NSManagedObject and I am using it with core data model to store its value to a persistent store.
I have a place where the same Country object will used as a normal object i.e. I will use it to store some temporary country name.
For that I have initialized the Country as following
[NSManagedObject alloc] init]
Initialization successfully done, but I am not able to set any property to that object.
Hence I did an exploration. In that I found that, init for the NSManagedObject is not supported as per the documentation.
I don't know how to use the NSManagedObject Country with CoreData as well as a normal Object.
2nd paragraph of the NSManagedObject class documentation's overview:
A managed object is associated with an
entity description (an instance of
NSEntityDescription) that provides
metadata about the object (including
the name of the entity that the object
represents and the names of its
attributes and relationships) and with
a managed object context that tracks
changes to the object graph. It is
important that a managed object is
properly configured for use with Core
Data. If you instantiate a managed
object directly, you must call the
designated initializer
(initWithEntity:insertIntoManagedObjectContext:).
From the documentation of the method:
Important: This method is the
designated initializer for
NSManagedObject. You should not
initialize a managed object simply by
sending it init.
The documentation is actually very good.
You do not want to try to use an NSManagedObject outside of a viable CoreData stack. NSManagedObjects are quite explicitly designed to work within a correctly configured Core Data environment. If you need a temporary instance, you can either create an in-memory store or create one in your regular store and just don't save the changes without deleting it first.
Use initWithEntity:insertIntoManagedObjectContext: and pass nil for managed object context.