2012-08-30 84 views
0

我試圖將數據添加到用戶表和項目表。添加到用戶很好,但不知何故它不會添加到項目表通過核心數據向sqlite添加數據 - 可以添加到一個表,但不能添加到其他

這兩個都沒有錯誤。

E.g.當我NSFetchRequest上的項目,直接添加到表後,我可以看到數據。但是,我重新構建並再次運行應用程序後。它消失了,但用戶記錄仍然存在。

怎麼回事?

而且,我手動檢查了sqlite數據庫。用戶記錄存在但不包含項目。

+5

核心數據可以由sqlite存儲支持,但不是數據庫。因此,從核心數據提供的抽象級別來看,術語「表」不適用。我們確實需要看到您的代碼提供很多建議 - 主要是您如何將託管對象添加到持久性存儲以及如何保存託管對象上下文。 – FluffulousChimp

回答

0

比方說

我有一個創建帳戶的viewController

NSManagedObjectContext *context = [(BDOAppDelegate *) [[UIApplication sharedApplication] delegate] managedObjectContext]; 

    User *newUser = [NSEntityDescription insertNewObjectForEntityForName:@"User" inManagedObjectContext:context]; 

    newUser.firstName = firstName; 
    newUser.lastName = lastName; 
    newUser.emailAddress = email; 
    newUser.password = password; 

這部分是好的。它實際上添加到用戶模型中。

我還存儲日誌和註銷的用戶倍另一視圖控制器

的NSManagedObjectContext *上下文= [(BDOAppDelegate *)[[UIApplication的sharedApplication]委託] managedObjectContext];

- (void)signLoggedInLoggedOut:(User *)getUser:(NSNumber *)login 
{  
    LoggedInLoggedOut *newRecord = [NSEntityDescription insertNewObjectForEntityForName:@"LoggedInLoggedOut" inManagedObjectContext:context]; 

    newRecord.user = getUser; 
    newRecord.login = [NSNumber numberWithInt:1]; 
    //input the time (NSDATE) as well 
    newRecord.created_at = [NSDate date]; 
    //Create relationship with User - LoggedInLoggedOut 
    [getUser addLoggedInLoggedOutsObject:newRecord]; 
} 

所以這種關係是一對多的關係,即用戶可以有多個登錄註銷條目。

當我立即獲取它時,我實際上可以檢索數據。

但是,當我退出時,建立然後再次運行。登錄和註銷數據不存儲。但用戶實際上是存儲的。

所以我不知道如何解決這個問題