开发者

Failed to call designated initializer on NSManagedObject class 'Item'

开发者 https://www.devze.com 2023-03-03 18:58 出处:网络
I am using core-data to add/delete \'Items\' (NSManagedObject) to the model. However, I am receiving the error in the title:

I am using core-data to add/delete 'Items' (NSManagedObject) to the model.

However, I am receiving the error in the title:

Code: Failed to call designated initializer on NSManagedObject class 'Item' I would really appreciate it if you could tell me where I am going wrong. I presume that the problem is to do with initialising Item.

RootViewController.m

- (void)AddNew {
    CDManager *manager = [[CDManager alloc] initWithManagedObjectContext:[self managedObjectContext] andDelegate:self];
    [manager addNewObject:[Item itemWithDescription:@"testing" dateSet:[NSDate date] fullfillBy:[NSDate date]]];
    [manager release];
}

CDManager.m

- (id) initWithManagedObjectContext:(NSManagedObjectContext *)context andDelegate:(id<CDManagerDelegateProtocol>)delegate {
    if ((self = [super init])) {
        [self setDelegate:delegate];
        [self setContext:context];
        [self setItems:[[NSMutableArray alloc] init]];
        [self updateItems];
    }
    return self;
}

- (void)addNewObject:(Item *)item {
    NSManagedObjectContext *context = _context;
    Item *items = [NSEntityDescription
                            insertNewObjectForEntityForName:@"Item" 
                            inManagedObjectContext:_context];
    [items setDateSet:[item dateSet]];
    [items setDateToFullfill:[item dateToFullfill]];
    [items setItemDescription:[item itemDescription]];
    NSError *error;
    if (![context save:&error]) {
        NSLog(@"Couldn't save due to : %@", [error localizedDescription]);
    }
    [_delegate manager:self didAddNewItem:items];
    [self update];
}

Item.m

static Item *shared = nil;
@implementation Item
......

+ (Item *)itemWithDescription:(NSString *)d dateSet:(NSDate *)date fullfillBy:(NSDate *)dates {
    @synchronized(shared) {
        if (!shared || shared == NULL) {
            shared = [[Item alloc] init];
        }
        [shared setItemDescription:d];
        [shared setDateSet:date];
        [shared setDateToFu开发者_StackOverflowllfill:dates];
        return shared;
    }
}


The Item class is defined as a singleton. There is no provision for singleton NSManagedObject subclasses. The context won't understand how to handle it.

This code makes very little sense. Here you initialize a singleton Item object:

[manager addNewObject:[Item itemWithDescription:@"testing" dateSet:[NSDate date] fullfillBy:[NSDate date]]]

... but you don't attach it to a context before passing it to the addNewObject: method. In turn that method creates another instance of Item and then populates it with the values of the presumed singleton. Why? If your singleton code actually works, every time you create an Item instance, you will get the same object back. What is the point of creating yet another reference to the singleton and setting its own values to itself. If the singleton code doesn't work, why use a singleton in the first place?

Uses like this is why singletons have such a bad rep. Don't use singletons unless you have a lot of experience with them and they are absolutely needed. There is nothing in this code that suggests that you do need a singleton and Core Data definitely does not like them.


Maybe you do something like

Item * items = [[Item alloc]initWithEntity:[NSEntityDescription entityForName:@"Item" inManagedObjectContext:context]insertIntoManagedObjectContext:context];

Edit -

because you aren't calling a good init method of NSManaged object in your custom init method. you could also clean that up to that to take a Context then call initWithEntity... on super there.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号