我现在面临的一个问题是,当我尝试保存对象时,Core Data似乎只保存第一个。
保存对象的代码
- (void)savingResCore {
AppDelegate *delegate = [[UIApplication sharedApplication]delegate];
NSManagedObjectContext *context = [delegate managedObjectContext];
ResultMO *resEntity = [NSEntityDescription insertNewObjectForEntityForName:@"Result" inManagedObjectContext:context];
if(indCheck == 1) {
resEntity.result1 = pena1;
resEntity.resuName1 = penaNam1;
}
if(indCheck == 2) {
resEntity.result2 = pena2;
resEntity.resuName2 = penaNam2;
}
if(indCheck == 3) {
resEntity.result3 = pena3;
resEntity.resuName3 = penaNam3;
}
_seqNameChk = [NSNumber numberWithInt:[_seqNameChk intValue] - 1];
if([_seqNameChk isEqual:@(0)]){
NSError *error = nil;
if (context != nil) {
if ([context hasChanges] && ![context save:&error]) {
NSLog(@"Unresolved error %@, %@", error, [error userInfo]);
abort();
}
}
}
}
获取对象的代码
- (void)fetchResCore {
AppDelegate *delegate = [[UIApplication sharedApplication]delegate];
NSManagedObjectContext *context = [delegate managedObjectContext];
NSEntityDescription *descriptor = [NSEntityDescription entityForName:@"Result" inManagedObjectContext:context];
NSFetchRequest *request = [[NSFetchRequest alloc]init];
request.entity = descriptor;
NSError *error;
NSArray *contArr = [context executeFetchRequest:request error:&error];
if(contArr == nil){
NSLog(@"Problems fetching data.");
}
else if(contArr != nil) {
if(contArr.count == 0) {
NSLog(@"No objects saved");
}
else {
NSManagedObject *resu = (NSManagedObject *)[contArr objectAtIndex:0];
NSLog(@"1 - %@", resu);
pena1 = [resu valueForKey:@"result1"];
penaNam1 = [resu valueForKey:@"resuName1"];
NSLog(@"%@", pena1);
pena2 = [resu valueForKey:@"result2"];
penaNam2 = [resu valueForKey:@"resuName2"];
NSLog(@"%@", pena2);
pena3 = [resu valueForKey:@"result3"];
penaNam3 = [resu valueForKey:@"resuName3"];
NSLog(@"%@", pena3);
NSLog(@"2 - %@", resu);
}
}
}
例如,我有一个按钮,每按一次按{1}},每次调出indCheck
方法时都会增加fetchResCore
。假设seqNameChk = 2
。第一次,属性result1
& resuName1
成功保存。连续按下该按钮后,它不再保存数据。因此,属性result2
,resuName2
,result3
&在resuName3
方法检索时,fetchResCore
不包含数据。
在resu
resuName1 = "(\n Alan\n)";
resuName2 = nil;
resuName3 = nil;
result1 = "(\n 100\n)";
result2 = nil;
result3 = nil;
在contArr
result1 = \"(\\n 100\\n)\";\n result2 = nil;\n result3 = nil;\n
resuName1 = \"(\\n Alan\\n)\";\n resuName2 = nil;\n resuName3 = nil;\n
答案 0 :(得分:0)
正如Jon Rose指出的那样,
我会假设它们都在数组
contArr
中,但您只看第一个:NSManagedObject *resu = (NSManagedObject *)[contArr objectAtIndex:0];
我打印出contArr.count
,结果是3.我还使用for
循环打印出数组的内容,并再次显示已填充的3个元素。
要解决这个问题,我必须纠正[contArr objectAtIndex:0]
,因为它只显示数组中的第一个元素。
在fetchResCore
方法
for(int i = 0; i < contArr.count; i++){
NSManagedObject *resu = (NSManagedObject *)[contArr objectAtIndex:i];
NSLog(@"1 - %@", resu);
if(pena1.count == 0 && penaNam1.count == 0){
pena1 = [resu valueForKey:@"result1"];
penaNam1 = [resu valueForKey:@"resuName1"];
NSLog(@"%@", pena1);
}
if(pena2.count == 0 && penaNam2.count == 0){
pena2 = [resu valueForKey:@"result2"];
penaNam2 = [resu valueForKey:@"resuName2"];
NSLog(@"%@", pena2);
}
if(pena3.count == 0 && penaNam3.count == 0){
pena3 = [resu valueForKey:@"result3"];
penaNam3 = [resu valueForKey:@"resuName3"];
NSLog(@"%@", pena3);
}
}
再次感谢Jon Rose指出了一个简单的错误,这个错误花了我几天时间才弄明白。