更新核心数据记录<故障>

发布于 2024-12-03 10:22:07 字数 1802 浏览 0 评论 0原文

我正在尝试更新核心数据中的一些记录。我采用以下步骤来完成它

  1. 带有谓词的获取函数从核心数据存储中检索记录
  2. 对象数组中的结果
  3. 集 循环遍历数组并更新每个记录
  4. 调用保存上下文

我遇到了两个问题

  • 初始运行后我得到<故障>在日志中
  • 我不确定保存上下文是否会实际保存对象

我的代码:

- (void)fetchExpenses {     
    // Define our table/entity to use
    NSEntityDescription *entity = [NSEntityDescription entityForName:@"Expense" inManagedObjectContext:managedObjectContext]; 

    // Setup the fetch request
    NSFetchRequest *request = [[NSFetchRequest alloc] init];
    [request setEntity:entity]; 

    // Define how we will sort the records
    NSSortDescriptor *sortDescriptor = [[NSSortDescriptor alloc] initWithKey:@"timestamp" ascending:NO];
    NSArray *sortDescriptors = [NSArray arrayWithObject:sortDescriptor];
    [request setSortDescriptors:sortDescriptors];
    [sortDescriptor release]; 

    NSPredicate *predicate = [NSPredicate predicateWithFormat:@"publishTimestamp == nil"];
    [request setPredicate:predicate];


    // Fetch the records and handle an error
    NSError *error;
    NSMutableArray *mutableFetchResults = [[managedObjectContext executeFetchRequest:request error:&error] mutableCopy]; 

    if (!mutableFetchResults) {
        // Handle the error.
        // This is a serious error and should advise the user to restart the application
    } 

    // Save our fetched data to an array
    [self setExpenseArray: mutableFetchResults];
    [mutableFetchResults release];
    [request release];
} 

- (void) save: {
[self fetchExpenses];
  int i = 1;
  int max = [expenseArray count];

  for(i=1; i<=max; i++) {
      // Get the expense selected.
      Expense *expense = [expenseArray objectAtIndex: i];
      // Do your updates here
      [expense setTimestamp:2]

  }
}

I m trying to update some records in Core Data. I m adopting following steps to get it done

  1. Fetch function with predicate retrieves the records from the Core Data
  2. Store the result set in a Object Array
  3. Loops through the array and update each record
  4. Call save context

I m running into two problems

  • After Initial run i get < fault > in the log
  • I m not sure whether the save context will actually save the object

My code:

- (void)fetchExpenses {     
    // Define our table/entity to use
    NSEntityDescription *entity = [NSEntityDescription entityForName:@"Expense" inManagedObjectContext:managedObjectContext]; 

    // Setup the fetch request
    NSFetchRequest *request = [[NSFetchRequest alloc] init];
    [request setEntity:entity]; 

    // Define how we will sort the records
    NSSortDescriptor *sortDescriptor = [[NSSortDescriptor alloc] initWithKey:@"timestamp" ascending:NO];
    NSArray *sortDescriptors = [NSArray arrayWithObject:sortDescriptor];
    [request setSortDescriptors:sortDescriptors];
    [sortDescriptor release]; 

    NSPredicate *predicate = [NSPredicate predicateWithFormat:@"publishTimestamp == nil"];
    [request setPredicate:predicate];


    // Fetch the records and handle an error
    NSError *error;
    NSMutableArray *mutableFetchResults = [[managedObjectContext executeFetchRequest:request error:&error] mutableCopy]; 

    if (!mutableFetchResults) {
        // Handle the error.
        // This is a serious error and should advise the user to restart the application
    } 

    // Save our fetched data to an array
    [self setExpenseArray: mutableFetchResults];
    [mutableFetchResults release];
    [request release];
} 

- (void) save: {
[self fetchExpenses];
  int i = 1;
  int max = [expenseArray count];

  for(i=1; i<=max; i++) {
      // Get the expense selected.
      Expense *expense = [expenseArray objectAtIndex: i];
      // Do your updates here
      [expense setTimestamp:2]

  }
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

电影里的梦 2024-12-10 10:22:07

您在日志中看到的 fault 并不表示错误,而是表示托管对象未完全加载到内存中,而是 由故障对象表示。这是正常行为。当您尝试访问或更改对象属性时,整个对象将“出错”或读入内存。这是一个令人困惑的老式数据库术语,可以追溯到 20 世纪 60 年代。

您的代码不保存任何对象。在对托管对象上下文调用 save 之前,对内存中托管对象的更改不会保留。

您也不想使用这样的可变副本:

NSMutableArray *mutableFetchResults = [[managedObjectContext executeFetchRequest:request error:&error] mutableCopy];

...因为它浪费内存并可能导致重复的托管对象。 Apple 文档中有一些代码可以启动此操作,但它是错误的。相反,只需使用:

NSArray *fetchResults=[managedObjectContext executeFetchRequest:request error:&error];

... 它将返回与获取匹配的托管对象的自动释放数组。

The fault you are seeing in the log doesn't indicate an error but means that the managed object is not fully loaded into memory but is instead represented by a fault object. This is normal behavior. When you try to access or change an object attribute the full object will be "faulted" or read-in to memory. It's a confusing old-fashion database terminology that dates back to 1960s.

Your code does not save any objects. Changes to managed objects in memory will not be persisted until you call a save on the managed object context.

You also do not want to use a mutable copy like this:

NSMutableArray *mutableFetchResults = [[managedObjectContext executeFetchRequest:request error:&error] mutableCopy];

… because it waste memory and can lead to duplicate managed objects. There was some code in Apple docs that got this started but its erroneous. Instead, just use:

NSArray *fetchResults=[managedObjectContext executeFetchRequest:request error:&error];

… which will return an autoreleased array of the managed objects matching the fetch.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文