Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What can I do with an Transformable attribute type in Core Data on the iPhone?

There's this Transformable data type for attributes. What is it good for? Are there good examples?

I'd like to play around with this. So after searching a while I came across this: NSValueTransformer. Seems to be something I need for this.

So how would I get started with this? For example, if I wanted to store an UIColor object, would I make an transformer for that?

What exactly is this thing transforming to or from? An NSData? And must the object which I pass to the transformer follow any protocol?

like image 713
dontWatchMyProfile Avatar asked Jun 10 '10 12:06

dontWatchMyProfile


People also ask

What is the use of Core Data in iOS?

Overview. Use Core Data to save your application's permanent data for offline use, to cache temporary data, and to add undo functionality to your app on a single device. To sync data across multiple devices in a single iCloud account, Core Data automatically mirrors your schema to a CloudKit container.

What is attribute in Core Data?

You can think of attributes as the columns of a table in a database. Attributes store the values of a Core Data record. There are several types of attributes, such as String, Date, Integer, Float, and Boolean. Select the Note entity in the data model editor and click the + button at the bottom of the Attributes table.

Should I use Core Data iOS?

The next time you need to store data, you should have a better idea of your options. Core Data is unnecessary for random pieces of unrelated data, but it's a perfect fit for a large, relational data set. The defaults system is ideal for small, random pieces of unrelated data, such as settings or the user's preferences.

What are Core Data type?

Core Data provides different attributes, including those common for all databases, such as Date or Decimal type, and non-standard attributes handled with Transformable type. It also provides Transient and Derived attributes so apps can derive one piece of data from the other.


1 Answers

Transformable attributes are useful for storing nonstandard object types within Core Data. For example, I provide code in this answer that lets you store UIImages as an attribute within Core Data. The image data is converted to and from an NSData instance that contains the image's PNG representation. This is all handled transparently for you by a custom NSValueTransformer.

You may also wish to encrypt individual attributes within your Core Data model, as I describe here. Using a transformable attribute for this makes this trivial to code.

like image 145
Brad Larson Avatar answered Oct 22 '22 01:10

Brad Larson