【发布时间】:2016-07-12 20:23:06
【问题描述】:
我知道有几个问题可能重复出现,但是没有明确的解决方案可以解决我的问题,所以我决定发布我的具体案例。
我在我的应用程序中使用CoreData,并且一些对象被实例化而没有被有效地保存在地面上,我在这些情况下的启动代码如下:
-(id)initEntity:(NSManagedObjectContext*)context{
AppDelegate appDelegate * = [[UIApplication sharedApplication] delegate];
NSEntityDescription *entity = [NSEntityDescription entityForName:@"Endereco" inManagedObjectContext: appDelegate.managedObjectContext];
self = (Endereco*)[[Endereco alloc] initWithEntity:entity insertIntoManagedObjectContext:nil];
return self;
}
但是,这个对象的一个属性是已经保存在基础上的自治市,并由 ActionSheet 选择:
if (actionSheet == actionSheetMunicipios) {
Municipio *municipio = [municipios objectAtIndex:buttonIndex-1];
endereco.municipio = municipio;
[textMunicipio setText:endereco.municipio.nome];
}
排队
endereco.municipio = municipio;
我收到以下错误:
*** 由于未捕获的异常“NSInvalidArgumentException”而终止应用程序,原因:“非法尝试建立 不同上下文中对象之间的关系'municipio'。
错误很明显,我试图建立具有不同上下文的对象关系,但在我的情况下,父对象没有保存在基础上,并且子对象已经存在,我怎么能解决?
【问题讨论】:
标签: ios core-data nsmanagedobject nsmanagedobjectcontext