开发者

Update Core Data records <fault>

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 allo开发者_运维百科c] 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]

  }
}


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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜