我有一个应用程序是一个UITabBarController,我定义了两个子视图
这两个选项卡在身份检查器中的类属性都设置为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();
}
}我的应用程序委托看起来像这样:
- (void)applicationDidFinishLaunching:(UIApplication *)application {
[application setStatusBarStyle:UIStatusBarStyleBlackOpaque];
[window addSubview:navigationController.view];
[window makeKeyAndVisible];
}现在我根本不能查询“用户”!虽然我没有收到任何错误或警告!
如有任何建议,我们将不胜感激!
谢谢
发布于 2010-05-12 18:06:05
看起来你可能在问如何更新到CoreData?
如果是这样,您需要在NSManagedObjectContext上使用save:方法,如下所示:
NSError *error;
[managedObjectContent save:&error];
if (error) {
...
}https://stackoverflow.com/questions/2817550
复制相似问题