【问题标题】:managedObjectContext questionmanagedObjectContext 问题
【发布时间】:2010-05-12 09:38:20
【问题描述】:

我有一个应用程序,它是一个 UITabBarController,我定义了两个子视图

两个选项卡在 Identity Inspector 中的 Class 属性都设置为 UINavigationController。

经过非常长时间的试验,现在我的编码已经达到了这一步。

- (void)viewDidLoad {
[super viewDidLoad];

myAppDelegate *appDelegate = (myAppDelegate *)[[UIApplication sharedApplication] delegate];
self.managedObjectContext = appDelegate.managedObjectContext;

{



  NSError *error = nil;
  NSFetchRequest *fetchRequest = [[NSFetchRequest alloc] init];
  [fetchRequest setEntity:[NSEntityDescription entityForName:@"User" inManagedObjectContext:self.managedObjectContext]];
  NSArray *fetchedItems = [self.managedObjectContext executeFetchRequest:fetchRequest error:&error];

  NSEntityDescription *entityDesc =
  [NSEntityDescription entityForName:@"User" inManagedObjectContext:self.managedObjectContext];

  // replace the old data with new. this DOESNT WORK
  if (fetchedItems.count > 0)
  {
   Usr *newUsr;
   for (newUsr in fetchedItems)
   {
    if ([newUsr.name isEqualToString:@"Line One"])
    {

     newUsr.uName = @"Line One (new)";

    }
   }
  }

  //add a new default data. THIS ADDS DATA TO MY TABLEVIEW BUT IT DOESNT SAVE THEM TO THE SQLITE
  User *addedDefaultdata = nil;
  addedDefaultdata = [[User alloc] initWithEntity:entityDesc insertIntoManagedObjectContext:self.managedObjectContext];
  addedDefaultdata.name = @"Added new 1";
  [addedDefaultdata release];
 }


NSError *error = nil;
if (![User.managedObjectContext save:&error]) {

    NSLog(@"Unresolved error %@, %@", error, [error userInfo]);
    abort();
}

}

我的 appdelegate 看起来像这样:

- (void)applicationDidFinishLaunching:(UIApplication *)application {    

 [application setStatusBarStyle:UIStatusBarStyleBlackOpaque];


 [window addSubview:navigationController.view];
 [window makeKeyAndVisible];

}

现在我根本无法查询“用户”!虽然我没有收到任何错误或警告!

任何建议将不胜感激!

谢谢

【问题讨论】:

    标签: iphone sqlite core-data


    【解决方案1】:

    您可能会问如何更新到CoreData?

    如果是这样,你需要在NSManagedObjectContext上使用save:方法,像这样:

    NSError *error;
    [managedObjectContent save:&error];
    if (error) {
        ...
    }
    

    【讨论】:

    • 是的,我想更新我的 coredata 应用程序。但我不明白为什么我得到一个 nil managedobjectcontext 并且因此没有“保存或更新?它与 managedobjctcontext 有关,因为我在某处读到这是错误的:myAppDelegate *appDelegate = (myAppDelegate *)[[UIApplication sharedApplication] delegate ]; self.managedObjectContext = appDelegate.managedObjectContext; 但它现在让我发疯了......更新:我不知道 NSError *error;... 行是如何到达那里的......它不在我的原始代码上...... (我什至不能复制粘贴)大声笑但是问题是一样的!有什么帮助吗?
    • 如果您使用标准模板之一创建了应用程序,那么您的应用程序委托将拥有一个您可以使用的有效 MOC。您没有在原始问题中说您的 MOC 为零。是吗?你仍然需要调用 save:,无论哪个。
    • 是的,它确实有一个有效的 MOC。但是,当我运行代码时出现此错误(已在上面更新): ...Classes/UsrRootViewController.m:109:0 错误:预期为 ':' before '.'标记任何想法?
    • 这是一个语法错误,您应该在尝试运行之前更正。
    • NSError *error = nil; if (![User.managedObjectContext save:&error]) { NSLog(@"Unresolved error %@, %@", error, [error userInfo]);退出(-1); } 我在上面找不到语法错误。
    猜你喜欢
    • 2012-08-04
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-07-17
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多