2010-05-12 43 views
1

我有一个应用程序,其是一个的UITabBarController,我已经定义了两个子视图managedObjectContext问题

两个翼片具有在Identity检查他们的类属性设置为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]; 

} 

,现在我不能一刀 “用户” 在所有!尽管我没有得到任何错误或警告!

任何建议将不胜感激!

谢谢

回答

0

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

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

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

是的,我想更新我的coredata应用程序。但我不明白为什么我得到一个零的managedobjectcontext,因此没有“保存或更新?它与managedobjctcontext有关,因为我读过这是错误的地方:myAppDelegate * appDelegate =(myAppDelegate *)[[UIApplication sharedApplication] delegate ]; self.managedObjectContext = appDelegate.managedObjectContext;但它现在让我疯狂... 更新:我不知道如何NSError *错误; ... 线到那里... 它不是我的原始代码... (我甚至不能复制粘贴)LOL 但是问题是一样的! 任何帮助? – treasure 2010-05-12 14:21:10

+0

如果你从一个标准模板创建应用程序,那么你的应用程序委托将有一个有效的MOC你可以使用,你不会在你原来的问题中说你的MOC是零。是吗?你仍然需要调用save :,无论哪一个。 – 2010-05-12 14:27:35

+0

是的,它确实有一个有效的MOC。然而,当我运行代码(更新上面)时,出现此错误: ...类/ UsrRootViewController.m:109:0 错误:预计':'之前。代币 有什么想法? – treasure 2010-05-12 14:48:00

相关问题