save CMTime in core data + Xcode8 + swift3 - ios

I declared two attributes (currentTime and fullTime) as Transformable in data model as shown below.
How to save data in this attribute? Do I need to convert to NSData first? or any other way?

Transformable need to be convertible to NSData. When you're using a type that conforms to the NSCoding protocol, that happens automatically. When you're not (as with CMTime), you can't use a transformable unless you create your own custom transformer by subclassing NSValueTransformer.
You may find it easier to simply save the CMTime properties in Core Data and reconstruct the CMTime from those. The properties are all numeric types that Core Data knows how to handle.

Related

NSCoding to CKRecord

I have a custom class with which i made an (swift-y) struct NSCodable. Now I want to convert that into a CKRecord in order to use CloudKit. Even though I set 'key value'-pairs when encoding my struct, it is in my understanding that the struct is converted into NSData and that you can't convert it to a Dictionary (or another key-value object). So I get the feeling that this is not the way to go.
Is there a way to make this conversion directly? Or with a step in-between (for instance converting the Data into a [String: String]- dictionary)?
NSCoding is only for going from/to the same class. Instead, write a toServerDictionary method on your custom class, including only the properties you want to send to CloudKit, and then use the result to call setValuesForKeys on a CKRecord.
You'll likely find there are properties that need to be specific types, in which case its better to make your method toServerRecord and create a CKRecord and return it. You can also have a updateWithServerRecord to set anything you receive back.

Saving custom object to NSUserDefaults - can't set encoder and decoder methods

How would you be able to save a custom object to NSUserDefaults??
NSUserDefaults.standardUserDefaults().setObject(obj, forKey: "object")
NSUserDefaults.standardUserDefaults().synchronize()
The object is of the SPTAuthViewController class in the Spotify iOS SDK, so I can't edit it to add encoder and decoder methods. How can I encode the object into an NSData object to store it in NSUserDefaults?
You can't store custom objects into user defaults. You can only store "property list objects" (NSString, NSData, NSDate, NSNumber, NSArray, or NSDictionary objects).
In order to save a custom object to user defaults you have to convert it to one of those types.
You can make your custom object conform to the NSCoding protocol. To do that you have to implement the init(coder:) and encode(coder:) methods. (I believe that's the swift form of the method names - I've been working in Objective-C lately so my Swift is getting rusty.) How you implement those methods depends on your class. Typically you make multiple calls to encodeObject:forKey: to encode your object's properties.
Once your custom object conforms to NSCoding you can convert it to NSData using the NSKeyedArchiver method archivedDataWithRootObject(). You then convert it from NSData back to the original object using the NSKeyedUnarchiver method unarchiveObjectWithData().
EDIT:
I just realized that you were trying to save a view controller object (SPTAuthViewController to user defaults. You should not serialize and save view controller objects. Controller objects should not be used to save application data.

Add NSDictionary to NSManagedObject Category

I would like to add a NSDictionary into a NSManagedObject Category class (or the NSManagedObject class itself)
When I do this, and I try to access the property, an exception is thrown.
Is this actually possible? I can't add this property as transient in the model because there is no NSDictionary Data Type, of course.
Thanks!
You don't say how you have currently created the property or what the exception is, but from the description you give it sounds like you should be setting the attribute in the Core Data model to be transformable. Setting it to be transformable will cause the NSDictionary to be archived (and unarchived) as you use it using the standard NSCoding protocol. Be sure that everything you put into the dictionary supports the NSCoding protocol so that it is properly archived and restored.
Using transformable is the way. Below are few more insights on the transformable property.
The Transformable data type is a special data type that allows us to
create attributes based on an Objective-C class (custom objects). This
data type is heavily used for storing instances of UIImage, UIColor,
and so on. As the information stored in the persistent store has to be
in the form of NSData instance, while using Transformable data type,
we need to create Value Transformers to convert the custom object
(information in attribute of Transformable data type) into an instance
of NSData (before storing in the persistent store) and to convert the
instance of NSData back to custom object while retrieving from the
persistent store.

Creating a core data model

I want to create a core data model. I have a pameter of type
CFUUIDRef
How will I represent this object in coredata model? I can see only basic types in cdatamodelling screen.
How can I do this with coredata?
You would have to use a string or binary data type in your model and NSManagedObject subclass. Then use the CFUUID methods CFUUIDGetUUIDBytes and CFUUIDCreateFromUUIDBytes whenever you need to set / retrieve the value.

Core Data not automatically calling value transformer when getting / setting attribute directly in code

If I understand correctly, the idea behind Core Data transformable attributes is:
implement an NSValueTransformer subclass with returns [NSData
class] in +transformedValueClass along with its implementation
for transformation
register the transformer in +load or +initialize
set an entity's attribute as transformable
set a name for your transformer (the name you used to register it)
in the xcode model editor for the attribute.
At this point, I'd expect that accessing or setting the attribute in a managedObject of the appropriate entity type would trigger the value transformer. However, I'm testing this in an app that uses AFIncrementalStore and I get the following behavior:
A - registering the transformer in +load or +initialize doesn't
seem necessary; Core Data finds it anyway (though read ahead).
B - fetch requests via AFIncrementalStore do trigger the
transformer. For example, I get JSON back from a fetch request and
when mapping the response dictionary to the managedObject, the
transformer is triggered and coverts the appropriate dictionary key
to NSData in the object.
C - HOWEVER, if I try to set or get the attribute via code, the
transformer is not called. That is doing something like
myManagedObject.myAttribute = #"hello" does not trigger the
conversion from NSString to NSData and neither does NSString
*myString = myManagedObject.myAttribute trigger the conversion from NSData to NSString.
So what am I missing? I thought the idea was that CoreData would automatically call the transformer. Am I wrong?
According to this question: Why is my transformable Core Data attribute not using my custom NSValueTransformer?
this seems to be a bug in the Apple frameworks. But what throws me off is that via AFIncrementalStore the value transformer does get called. Maybe the key is that by setting just an attribute via code I am not really triggering AFIncrementalStore and so the change is merely in-memory ?
(From the comment above:) The inverse transformer is called when you save the context,
not when you set an attribute.

Resources