使用NSManagedObjectContext子类使用NSFetchedResultController插入数据

时间:2013-12-21 17:15:55

标签: ios objective-c core-data

我在iOS应用程序中使用NSFetchedResultsController。我需要插入一些数据。我正在尝试使用以下代码:

Shop *shop = (Shop *) [NSEntityDescription insertNewObjectForEntityForName:@"Shop" inManagedObjectContext:[self managedObjectContext]];
[shop setName:@"test"];
[self.managedObjectContext save:nil];
[self.fetchedResultsController performFetch:nil];

无。我没有任何错误,我的申请没有终止。也许我错误地插入了数据。

1 个答案:

答案 0 :(得分:1)

检查结果,传递并检查非零错误参数。

Shop *shop = [NSEntityDescription insertNewObjectForEntityForName:@"Shop"     inManagedObjectContext:[self managedObjectContext]];
[shop setName:@"test"];
NSLog(@"%@", shop);
NSError *error;
if (![self.managedObjectContext save:&error]) {
      NSLog(@"MOC save failed: %@ %@", error.localizedDescription, error.userInfo);
}
NSParameterAssert(self.fetchedResultsController.fetchRequest);
NSLog(@"fetchRequest: %@", self.fetchedResultsController.fetchRequest);
if (![self.fetchedResultsController performFetch:&error]) {
      NSLog(@"performFetch failed: %@ %@", error.localizedDescription, error.userInfo);
}