0

这是我编辑特定属性并将其保存到 sqlite 数据库的代码,但我无法将更改保存到数据库。

-(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;
}
4

1 回答 1

0

尝试将for循环替换为

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

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

不确定它是否会起作用,只是尝试一下。无论如何,它更干净。

于 2012-06-27T10:32:25.197 回答