iosobjective-ccore-dataxcdatamodel

Is xcdatamodel needed?


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?


Solution

  • 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.