编辑核心数据中的属性

时间:2012-06-27 07:19:38

标签: xcode core-data ios5

这是我的代码,用于编辑特定属性并将其保存到sqlite 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;
}

1 个答案:

答案 0 :(得分:0)

尝试将for循环替换为

for(Table1 * data in objects)
  data.member_id = memberKey;

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

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