Jessica
Jessica

Reputation: 1498

Core Data Modelling Basics

I am developing an app for learning purposes. It's a feed parser using NSXMLParser. Almost everything is accomplished except for the persistence. I have learned archiving and implemented it in my project but to move to a better solution and make my learning curve better, I have decided to move my app to Core Data.

Below is my parser code. FeedChannel is handling the channel attribute inside feedburner's feed and also grabing the "items" that FeedItem file is handling.

FeedChannel.h

#import <Foundation/Foundation.h>

@interface FeedChannel : NSObject <NSXMLParserDelegate> {

    NSMutableArray *items;
    NSMutableString *currentString;
}

@property (nonatomic, assign) id parentParserDelegate;
@property (nonatomic, retain) NSString *title;
@property (nonatomic, retain) NSString *sDescription;
@property (nonatomic, readonly) NSMutableArray *items;

@end

FeedItem.h

#import <Foundation/Foundation.h>

@interface FeedItem : NSObject <NSXMLParserDelegate> {

    NSMutableString *currentString;

}

@property (nonatomic, assign) id parentParserDelegate;
@property (nonatomic, retain) NSString *title;
@property (nonatomic, retain) NSString *link;
@property (nonatomic, retain) NSString *creator;
@property (nonatomic, retain) NSString *pubDate;
@property (nonatomic, retain) UIImage *thumbnail;


@end

I have tried to learn CoreData but for new programmers it is a little complicated apparently.

Currently what I have done is that I have added the CoreData framework in my project, created a model file, added two entities named FeedChannel and FeedItem. What I don't know how to do is the following:

1) Should I use the "Create Managed Subclass" in Xcode to overwrite the existing files or just mention the class name in the data model for each entry in the inspector panel or both approaches are the same?

2) If I use "Create Managed Subclass", it also use dynamic instead of synthesise and I don't know whether it affects my project or not.

3) How to handle thumbnail attribute as there is UIImage type in the data model of Core Data.

and the most important question:

4) How to handle NSMutableArray "items" in the FeedChannel file that is holding the items in the FeedItem? Do i need to somehow make one-to-many relationship?

Hopefully it will answer all the basic question related to Core Data that newbies face. Thanks in advance.

Upvotes: 0

Views: 208

Answers (1)

Reid
Reid

Reputation: 1129

  1. The "create managed subclass" shortcut is just that. All it really does is subclass NSManagedObject (vs. NSObject) and create properties for all of your attributes and relationships. So no, you don't have to use it. You can do the same manually if you prefer.

  2. Yes, properties of managed objects need to be @dynamic. This tells the compiler that the property will be resolved at runtime by core data.

  3. You want the transformable type, which handles any object--your properties will be of type id, generic object.

  4. Core data will replace your array. You'll have a to-many relationship between the channel and the feed items. Make sure to create the inverse to-one relationship between the feed item and the channel, and mark the relationships as inverse.

There are a few good tutorials and books out there on using Core Data. It seems a little daunting at first, but is actually not that bad.

Upvotes: 1

Related Questions