1

This is my entity object in my iOS app that uses CoreData.

#import <Foundation/Foundation.h>
#import <CoreData/CoreData.h>

#import "Price.h"

@interface DataPrice : NSManagedObject

@property (nonatomic) NSInteger id;
@property (nonatomic, retain) NSNumber *ron95;
@property (nonatomic, retain) NSNumber *ron92;
@property (nonatomic, retain) NSNumber *dieselNormal;
@property (nonatomic, retain) NSNumber *dieselSpecial;
@property (nonatomic, retain) NSDate *postDate;

+ (instancetype)insertDataPriceWithPrice:(Price*)price
                  inManagedObjectContext:(NSManagedObjectContext *)managedObjectContext;

+ (instancetype)fetchDataPriceInManagedObjectContext:(NSManagedObjectContext *)managedObjectContext;

+ (Price*)fetchPriceInManagedObjectContext:(NSManagedObjectContext *)managedObjectContext;

@end


#import "DataPrice.h"

@implementation DataPrice

@dynamic id;
@dynamic ron95;
@dynamic ron92;
@dynamic dieselNormal;
@dynamic dieselSpecial;
@dynamic postDate;

+ (instancetype)insertDataPriceWithPrice:(Price*)price
             inManagedObjectContext:(NSManagedObjectContext *)managedObjectContext
{
    DataPrice* dataPrice = [NSEntityDescription insertNewObjectForEntityForName:self.entityName
                                               inManagedObjectContext:managedObjectContext];
    dataPrice.id = price.id;
    dataPrice.ron95 = price.ron95;
    dataPrice.ron92 = price.ron92;
    dataPrice.dieselNormal = price.dieselNormal;
    dataPrice.dieselSpecial = price.dieselSpecial;
    dataPrice.postDate = price.postDate;

    return dataPrice;
}

+ (NSString*)entityName
{
    return @"Price";
}

+ (instancetype)fetchDataPriceInManagedObjectContext:(NSManagedObjectContext *)managedObjectContext {
    NSEntityDescription *entity = [NSEntityDescription entityForName:self.entityName inManagedObjectContext:managedObjectContext];

    if (!entity) {
        return NULL;
    }


    NSSortDescriptor *sortDescriptor = [[NSSortDescriptor alloc] initWithKey:@"postDate" ascending:NO];

    NSFetchRequest *request = [[NSFetchRequest alloc] init];
    request.entity = entity;
    request.sortDescriptors = @[ sortDescriptor ];
    request.fetchLimit = 1;

    NSError *error;
    NSArray *fetchResults = [managedObjectContext executeFetchRequest:request error:&error];

    DataPrice *result = fetchResults.firstObject;

    return result;
}

+ (Price*)fetchPriceInManagedObjectContext:(NSManagedObjectContext *)managedObjectContext {
    DataPrice *dataPrice = [self fetchDataPriceInManagedObjectContext:managedObjectContext];

    Price *price = [[Price alloc] init];
    price.id = dataPrice.id;
    price.ron92 = dataPrice.ron92;
    price.ron95 = dataPrice.ron95;
    price.dieselNormal = dataPrice.dieselNormal;
    price.dieselSpecial = dataPrice.dieselSpecial;
    price.postDate = dataPrice.postDate;

    return price;
}


@end

I named the class DataPrice but want the entity to be Price.

Do I need an xcdatamodel for my application and how do I define one?

Sandah Aung
  • 6,156
  • 15
  • 56
  • 98

1 Answers1

1

A. Yes, you need a data model. You get it with File | New … | File and in the sheet iOS | Core Data | Data Model.

However it is easier to create a Core Data application from the very beginning.

B. Classes and entity (types) can have different names. You simply can assign a class with a different name to the entity type. (Ny default NSManagedObject is assigned, what is a different name for all entity types not called NSManagedObject. ;-)) But there is little reason to do so.

Amin Negm-Awad
  • 16,582
  • 3
  • 35
  • 50
  • Does `DenkoStation.xcdatamodeld` automatically get converted to `DenkoStation.momd`? – Sandah Aung Jul 23 '15 at 11:31
  • 1
    I am using a project posted on GitHub. ORM isn't a new concept to me I've used it since TopLink. So I thought I would have luck with a sample project. – Sandah Aung Jul 24 '15 at 09:48
  • ORMs very often have to make decisions between OOP and rDBMS because of the ORIM. Every implementation takes different decisions. Knowing one does not help very much for understanding another one. – Amin Negm-Awad Jul 24 '15 at 10:00