ddolce
ddolce

Reputation: 815

iOS: Practices for storing a view

My app allows users to customize (add UILabels and UIImages to a UIView. I want the user to be able to retrieve the same view (everything remains at the same location where it was defined). I'm thinking CoreData. I guess I can store everything necessary in a Dictionary<String, AnyObject> including a subview's frame and whatever the user puts inside that view (can be an image or text depending on whether it is a UILabel or UIImage). The storage structure I have in my head is the following:

Dictionary<String, AnyObject>

Once I have this ready I can just convert the dictionary into NSData and stuff it into core data. But is this the correct way of storing views? Any alternative/suggestion on this? Thanks!

=================================UPDATE================================= The structure I ended up using looks something like this:

NSDictionary

Since I'm using CoreData, I only needed to make my "view" object extend NSManagedObject and have a dictionary property. This way, the encoding process was no longer needed.

Upvotes: 0

Views: 340

Answers (1)

Wain
Wain

Reputation: 119031

Your approach to creating a dictionary is fair. In a number of ways it would be better to create a custom class (or set of) as these are more specific and provide more compiler checks.

In either case, archiving the store to data is the correct approach (using NSCoding).

Once you have the data you should save it directly to disk rather than using Core Data. Core Data doesn't provide any advantages when storing a single large binary data blob.

Using Core Data properly will only help you if you have many views that you want to store and you don't need to reload them all at the same time. If doing this you wouldn't use a dictionary or data, you would define entities with appropriate attributes.

Upvotes: 1

Related Questions