我有一个奇怪的错误。我有一个调用对象和状态对象之间的关系。调用对象的关系名称是statusForCall。
数据以编程方式从JSON导入核心数据。如果没有状态,它将不会创建呼叫。用户无法将statusForCall更改为nil。
然而,经过一段时间(似乎是几天),当通过提取请求访问数组访问数组时,当我访问aCall.StatusForcall.statusID时,它开始返回nil。代码无法将statusForCall更新为nil。任何想法可能导致这种情况以及从哪里开始寻找?
在整个申请过程中,cachename是零。 有趣的是,如果用户重新下载应用程序,问题就解决了。代码不会改变,数据也不会改变(没有迁移 - 它是相同的版本) - 但由于某种原因,这总是解决了这个问题。我真的很难知道该怎么看才能找到底线。
用于设置下面状态的代码(为简洁而编辑)。正如我所说,这份工作最初有一个状态,但似乎在一段时间后失去了关系(我不知道这是多长时间,因为用户对此不可靠)
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);
}
答案 0 :(得分:0)
结果表明,一个关系属性被意外地改变了 - 它正在从众多的级联中删除 - 这导致一端被删除。