无法更新核心数据中的位置

时间:2014-07-24 15:00:50

标签: ios objective-c core-data

我需要更新核心数据库中的位置。当我添加对象或从该基础中删除它时一切正常。当我想更新该基础中的对象时出现问题。问题是程序引发了我:

Thread 1: EXC_BAD_ACCES(code = 2, adres = 0x38)

它很奇怪,因为它会抛出此错误,但会更新核心数据对象。我在Xcode 5上使用了模拟器。我已经尝试在代码中逐行评论,但它已经没有帮助了。查看错误的地址和屏幕截图,没有相互兼容的地址。有什么问题?

代码就是这样:

[self insertPositionRightAfterChangeValues:name rating:[changedRate stringValue] urlMain:[newInsert urlMain] contentUrl:[newInsert contentUrl] coverUrl:[newInsert coverUrl] date:[newInsert date] type:[newInsert type] int:integer];

这个方法的其余部分:

-(void)insertPositionRightAfterChangeValues:(NSString *)name rating:(NSString *)rating urlMain:(NSString *)urlMain contentUrl:(NSString *)contentUrl coverUrl:(NSString *)coverUrl date:(NSString *)date type:(NSString *)type int:(int)position
{
    int motherPosision = position;

    //insert new one
    NSManagedObjectContext *context2 = [self managedObjectContext];
    Kwejki * newEntry = [NSEntityDescription insertNewObjectForEntityForName:@"Kwejki" inManagedObjectContext:context2];

    newEntry.name = [[NSString alloc] initWithString: name];
    newEntry.rating = [[NSString alloc] initWithString: rating];
    newEntry.urlMain = [[NSString alloc] initWithString: urlMain];
    newEntry.contentUrl = [[NSString alloc] initWithString: contentUrl];
    newEntry.coverUrl = [[NSString alloc] initWithString: coverUrl];
    newEntry.date = [[NSString alloc] initWithString: date];
    newEntry.type = [[NSString alloc] initWithString: type];


    NSLog(@"%@", newEntry.name);
    NSLog(@"%@", newEntry.rating);
    NSLog(@"%@", newEntry.urlMain);
    NSLog(@"%@", newEntry.contentUrl);
    NSLog(@"%@", newEntry.coverUrl);
    NSLog(@"%@", newEntry.date);
    NSLog(@"%@", newEntry.type);

    NSError *error2;
    if (![context2 save:&error2]) {
        NSLog(@"Whoops, couldn't save: %@", [error2 localizedDescription]);
    }
    else{
        NSLog(@"SAVED!!!");
    }

}

带有此错误的屏幕截图:

enter image description here

更新:

当我评论我调用插入新对象的方法的行时,我得到以下错误:CoreData: error: Failed to call designated initializer on NSManagedObject class 'Kwejki'

更新2:

@interface Kwejki : NSManagedObject<NSCopying>

@property (nonatomic, strong) NSString * type;
@property (nonatomic, strong) NSString * contentUrl;
@property (nonatomic, strong) NSString * coverUrl;
@property (nonatomic, strong) NSString * rating;
@property (nonatomic, strong) NSString * urlMain;
@property (nonatomic, strong) NSString * date;
@property (nonatomic, strong) NSString * name;

@end


@implementation Kwejki

@synthesize name;
@synthesize date;
@synthesize urlMain;
@synthesize rating;
@synthesize coverUrl;
@synthesize contentUrl;
@synthesize type;

-(Kwejki *)copyWithZone:(NSZone *)zone
{
    Kwejki *copyModel = [[Kwejki allocWithZone:zone] init];

    if(copyModel)
    {
        copyModel.name = [self name];
        copyModel.date = [self date];
        copyModel.urlMain = [self urlMain];
        copyModel.rating = [self rating];
        copyModel.coverUrl = [self coverUrl];
        copyModel.contentUrl = [self contentUrl];
        copyModel.type = [self type];
    }
    return  copyModel;
}


@end



-(void)addNewPosition:(ScrollViewViewController *)ScrollController recentlyDownloadedItem:(KwejkModel *)modelTmp
{

    if(modelTmp == nil)
    {
        NSLog(@"YES NULL");

    }
    else
    {
        NSLog(@"Not null");
    }


    Kwejki * newEntry = [NSEntityDescription insertNewObjectForEntityForName:@"Kwejki" inManagedObjectContext:self.managedObjectContext];
    NSLog(@"%@", [modelTmp getNameOfItem]);

    newEntry.name = [[NSString alloc] initWithString:[modelTmp getNameOfItem]];
    newEntry.rating = [modelTmp getRateOfPosition];
    newEntry.urlMain = [modelTmp getUrlAdress];
    newEntry.contentUrl = [modelTmp getContentUrl];
    newEntry.coverUrl = [modelTmp getCoverImage];
    newEntry.date = [modelTmp getDateOfItem];

    if([modelTmp getType] == photo)
    {
        newEntry.type = @"0";

    }
    else if([modelTmp getType] == gif)
    {
        newEntry.type = @"1";

    }
    else if ([modelTmp getType] == video)
    {
        newEntry.type = @"2";

    }


    NSLog(@"%@", newEntry.name);
    NSLog(@"%@", newEntry.rating);
    NSLog(@"%@", newEntry.urlMain);
    NSLog(@"%@", newEntry.contentUrl);
    NSLog(@"%@", newEntry.coverUrl);
    NSLog(@"%@", newEntry.date);
    NSLog(@"%@", newEntry.type);

    NSError *error;
    if (![self.managedObjectContext save:&error]) {
        NSLog(@"Whoops, couldn't save: %@", [error localizedDescription]);
    }
    else{
        NSLog(@"UDALO SIE!!!");
    }
    [modelArray insertObject:newEntry atIndex:0];
    [self.tableView reloadData];


}

我试图克服这个问题,只是再次添加新对象作为更新,但它也没有帮助。我如何添加新对象:我点击长位置,新窗口打开,在那里我添加新值,从主ViewController从该视图方法调用,并在这里崩溃。当我如上所述添加正常对象但在单独的窗口中一切正常。我真的不知道出了什么问题。

1 个答案:

答案 0 :(得分:1)

当我看到此错误时,通常意味着我没有正确设置我的NSManageObject。

要检查的事项:

  1. 在insertPositionRightAfterChangeValues方法中添加一个断点,然后逐步查找抛出异常的位置。 (我的猜测是你创建你的Kwejki对象的时候。)
  2. Kwejki是NSManageObject的子类。
  3. Kwejki实体和班级名称是正确的。 (你能否展示Kwejki的.xcdatamodeId实体属性?)
  4. 如果一切正确,那么对于精神错乱的检查,我会很长时间地创建Kwejki对象。

    NSEntityDescription *entity = [NSEntityDescription entityForName:NSStringFromClass([Kwejki class]) inManagedObjectContext:context2];
    Kwejki* newEntry = [[Kwejki alloc] initWithEntity:entity insertIntoManagedObjectContext:context2];
    

    然后你可以看到实体是一个问题还是你的Kwejki。

    P.S。作为一个有用的建议,而不是输入实体名称。

    Kwejki * newEntry = [NSEntityDescription insertNewObjectForEntityForName:@"Kwejki" inManagedObjectContext:context2];
    

    我会做这样的事情。

    Kwejki * newEntry = [NSEntityDescription entityForName:NSStringFromClass([Kwejki class]) inManagedObjectContext:context2];
    

    这为您提供了实体名称的编译器检查。