Xcode 编辑核心数据中的属性

Xcode 编辑核心数据中的属性,xcode,core-data,ios5,Xcode,Core Data,Ios5,下面是我编辑特定属性并将其保存到sqlite DB的代码,但我无法将更改保存到DB -(void)changeMemberKey { NSEntityDescription *entityDesc=[NSEntityDescription entityForName:@"Table1" inManagedObjectContext:context]; NSFetchRequest *request=[[NSFetchRequest alloc] init]; NSPredicate

下面是我编辑特定属性并将其保存到sqlite DB的代码,但我无法将更改保存到DB

-(void)changeMemberKey
{
  NSEntityDescription *entityDesc=[NSEntityDescription entityForName:@"Table1" inManagedObjectContext:context];
  NSFetchRequest *request=[[NSFetchRequest alloc] init];
  NSPredicate *predicate=[NSPredicate predicateWithFormat:@"(member_id=Null)"];
  [request setPredicate:predicate];
  [request setEntity:entityDesc];
  Table1 *matches;
  NSError *error;

  NSArray *objects=[context executeFetchRequest:request error:&error];
  NSLog(@"Object count===%d",[objects count]);
  for(int i=0;i<[objects count];i++)
  {
    matches=[objects objectAtIndex:i];
    Table1 *data=(Table1 *)matches;
    NSLog(@"Data before===%@",data);
    [data setValue:memberKey forKey:@"member_id"];
    [context save:&error];
    NSLog(@"Data after====%@",data);
    data=nil;
  }

  entityDesc=nil;
  request=nil;
  matches=nil;
  error=nil;
  objects=nil;
}
尝试将for循环替换为


不确定它是否有效,试一试。无论如何,它更干净。

尝试替换[上下文保存:&错误];如果![上下文保存:&错误]NSLog@Couldn“不要保存数据!错误:%@,[错误描述];。然后你就可以看到你是否成功保存了。如果没有,请编辑您的问题以添加日志。实际上,您可以在for循环之后只保存一次数据,是的谢谢你的建议。我像这样编辑我的代码![上下文保存:&错误]NSLog@Couldn“不要保存数据!错误:%@,[错误描述];但是没有错误,上下文已保存但未反映在数据库中是否有任何日志输出,如“Data before”或其他内容?顺便说一句,您如何声明您的上下文?是的,正确获取日志输出,这意味着前面的数据使用null memberid给出输出,后面的数据使用新memberid给出输出。上下文的声明如下:NSManagedObjectContext*context;in.h和appDelegate=appDelegate*[[UIApplication sharedApplication]delegate];in.m files context=[appDelegate managedObjectContext];
for(Table1 * data in objects)
  data.member_id = memberKey;

if (! [context save:&error])
  NSLog(@"Couldn't save data! Error:%@", [error description]);