【发布时间】:2014-12-02 14:47:31
【问题描述】:
我有一个奇怪的错误。我在调用对象和状态对象之间有关系。调用对象的关系名称是 statusForCall。
数据以编程方式从 JSON 导入核心数据。它不会创建没有状态的呼叫。用户无法将 statusForCall 更改为 nil。
但是经过一段时间(似乎是几天)后,当我通过 fetch 请求访问数组时,当我访问 aCall.StatusForcall.statusID 时,它开始返回 nil。代码不可能将 statusForCall 更新为 nil。
任何想法可能导致这种情况以及从哪里开始寻找?
整个应用程序中的缓存名称为零。
有趣的是,如果用户重新下载应用程序,问题就解决了。代码没有改变,数据也没有改变(没有迁移 - 它是相同的版本) - 但出于某种原因,这总是可以解决问题。
我真的很难知道该看什么才能深入了解。
用于设置以下状态的代码(为简洁起见进行了编辑)。正如我所说,这份工作最初有一个状态,但似乎在一段时间后失去了这种关系(我不知道这是多长时间,因为用户对此不可靠)
Call *theCall;
//see if we have the call on the device - if not create a new one, if we do update that one
BOOL contentIsInCoreData = NO;
for (Call *thisCall in existingCalls)
{
if ([thisCall.callID isEqualToNumber:[checkDic objectForKey:@"callID"]])
{
theCall=thisCall;
contentIsInCoreData = YES;
}
}
NSError * error = nil;
NSDateFormatter *dateFormat =[[NSDateFormatter alloc] init];
[dateFormat setDateFormat:@"dd-MMM-yyyy"];
if (contentIsInCoreData==NO)
{
[self.postDelegate updateGetJobsAndTimesProgress:[NSString stringWithFormat:@"Adding new job: %@",[checkDic objectForKey:@"woRef"]]];
//new call add bits that will not change
theCall = (Call*)[NSEntityDescription insertNewObjectForEntityForName:@"Call" inManagedObjectContext:self.managedObjectContext];
}
//set various properties from the JSON dictrionary – these have been cut down for the example code
theCall.woRef=[checkDic objectForKey:@"woRef"];
theCall.shortAddress=[checkDic objectForKey:@"shortAddress"];
theCall.address=[[checkDic objectForKey:@"address"] stringByReplacingOccurrencesOfString:@"\\n" withString:@"\n"];
theCall.postCode=[checkDic objectForKey:@"postCode"];
theCall.callID=[checkDic objectForKey:@"callID"];
//****THIS IS WHERE THE STATUS IS SET – PLEASE NOTE THE STATUS WILL EXIST IN CD ALREADY and the JSON WILL have a valid statusID
NSFetchRequest *request2=[NSFetchRequest fetchRequestWithEntityName:@"CallStatus"];
request2.predicate=[NSPredicate predicateWithFormat:@"callStatusID ==%@",[checkDic objectForKey:@"callStatusID"]];
error = nil;
NSArray * existingStatus = [self.managedObjectContext executeFetchRequest:request2 error:&error];
CallStatus *selectedStatus;
selectedStatus=[existingStatus firstObject];
theCall.statusForCall = selectedStatus;
error = nil;
if ([self.managedObjectContext save:&error])
{
//NSLog(@"saved job");
}
else
{
NSLog (@"***BROKEN GET JSON!!!\r\r%@",error);
}
【问题讨论】:
-
我愚蠢的问题:你保存上下文吗?更好的是,你可以看看它是否保存在磁盘中。
-
嗨,是的,我有一个上下文。我通过 aCall.statusForCall.statusID 访问它,在此之前的行中我得到 aCall.callID,其他呼叫属性都可以正常工作。状态对象应该是错误的,因为我正在访问它的属性而不仅仅是对象。使用 [context save&error]; 显式保存数据
-
这只会在一段时间后发生(看起来像是最初加载应用程序后的几天)。最棒的是我无法在开发环境中复制它!
-
向我们展示您是如何设置关系的。看起来问题可能就在那里。
-
hi mundi - 我只是获得一个有效的状态对象并将其设置为 call.statusForCall - 我将添加到 OP 中
标签: ios objective-c core-data