2010-05-12 31 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

相關問題