首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >managedObjectContext问题

managedObjectContext问题
EN

Stack Overflow用户
提问于 2010-05-12 17:38:20
回答 1查看 1.1K关注 0票数 1

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

这两个选项卡在身份检查器中的类属性都设置为UINavigationController。

经过很长一段时间的试验,现在我已经用我的代码做到了这一步。

代码语言:javascript
复制
- (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();
}

}

我的应用程序委托看起来像这样:

代码语言:javascript
复制
- (void)applicationDidFinishLaunching:(UIApplication *)application {    

 [application setStatusBarStyle:UIStatusBarStyleBlackOpaque];


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

}

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

如有任何建议,我们将不胜感激!

谢谢

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2010-05-12 18:06:05

看起来你可能在问如何更新到CoreData?

如果是这样,您需要在NSManagedObjectContext上使用save:方法,如下所示:

代码语言:javascript
复制
NSError *error;
[managedObjectContent save:&error];
if (error) {
    ...
}
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/2817550

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档