Apollo
Apollo

Reputation: 9064

Using Core Data and NSManagedObject to save lists

I'm looking for some advice on how to manage data for a list saving application. Essentially, I have two object types: List, ListItem. A List object looks like this:

@interface List : NSObject

@property (nonatomic, copy) NSString *name;
@property (nonatomic, copy) NSMutableArray *contents;
@property (nonatomic, copy) NSString *completed;
@property (nonatomic, strong) NSDate *creationDate;


-(id)initWithName:(NSString *)name contents:(NSMutableArray *)contents completed:(NSString *)completed date:(NSDate *)creationDate;

A ListItem:

@interface ListItem : NSObject

@property (nonatomic, copy) NSString *name;
@property (nonatomic, copy) NSString *completed;
@property (nonatomic, strong) NSDate *creationDate;

-(id)initWithName:(NSString *)name completed:(NSString *)completed date:(NSDate *)date;

@end

Currently, I'm saving these lists in an array in NSUserDefaults, however I envision performing actions like "sort my list by creation date, type, or name" and I feel as though I'll run into difficulty. So, before moving on and writing these methods myself, I'd like to pose some questions to you all: Does it make sense for me to learn how to use Core Data? Will it really provide me with better functionality/a more reliable way of persisting data?

Thanks in advance for the advice.

Upvotes: 0

Views: 212

Answers (2)

Stakenborg
Stakenborg

Reputation: 2940

If your app's primary focus is storing lists based on these objects, I'd say it's probably worth looking into Core Data. It'll allow you to more easily fetch multiple lists based on criteria (if you decide to do something like that later) and just gives you overall more control over manipulation of your stored data.

There are some tools to help making the transition into Core Data a little less overwhelming, namely mogenerator and Magical Record.

The Core Data models you would need are pretty basic, and setting up a many-to-one relationship between ListItem and List would be perfect for what it appears you're doing.

Good luck!

Upvotes: 2

Mundi
Mundi

Reputation: 80273

Definitely go for Core Data. You will see that this will result in a much more robust and scalable app.

The most important drawback is that you can only save the whole list which is very inefficient. Eventually you will run out of memory. With Core Data you can make incremental updates.

Second, the NSUserDefaults infrastructure is not very flexible in terms of types and was really meant for something different. You are not storing preferences, but data.

Third, as you have pointed out already, sorting and filtering becomes both a strain on the resources and requires a lot of coding. You can sort in and filter in memory pretty efficiently, but it won't result in very maintainable code.

Upvotes: 1

Related Questions