2010-07-26 50 views
0
關於CoreData,我一直無法找到答案

希望兩個小問題:更新和性能不點火故障

1)我有一個故障的對象。作爲屬性訪問屬性不會觸發故障,通過KVC IS訪問相同的屬性來觸發故障。任何想法爲什麼?

即object.title返回nil並且對象仍然存在錯誤,但[object valueForKey:@「title」]返回標題並且該對象不再是故障。

2)更新現有的記錄已經停止工作。添加/刪除作品。添加/更新共享相同的代碼路徑(一個傳遞現有對象,另一個傳入新插入的對象)。但更新不會工作。更新對象中的數據是正確的,並設置爲新值,並且保存成功且沒有錯誤,但數據庫中的記錄保持不變。任何想法?

注意:只有一個的NSManagedObjectContext

乾杯

回答

0

無法從您的描述多說沒有代碼。

但是看起來您已經更新了ram中的對象,但更新沒有提交給數據庫層進行物理更改。

編輯:

是,「添加」和「刪除」是從「編輯/更新」記錄不同。

性能原因當您對NSManagedObjectContext進行操作時,映射的對象將作爲實體保存在內存中,而不是完全針對數據庫編碼。

檢查下面的鏈接: http://cocoawithlove.com/2010/02/differences-between-core-data-and.html

正常工作流程:從數據庫

  • 負載相應的行從這些行
  • 實例化對象
  • 修改圖形對象 那現在在內存
  • 提交更改回 數據庫
+0

增加其經過完全相同的代碼路徑作品的新紀錄。更新現有記錄不會。 對象在內存絕對更新和保存絕對是被稱爲被管理對象上下文。我希望有人知道一個明顯的「陷阱」。 目前它對我來說絕對沒有意義。 :\ – 2010-07-26 04:16:54

+0

@Ryan布克見上面編輯 – 2010-07-26 05:04:06

+0

是的。這很好。這就是我已經在做的事情。我解決了它。正如我所預料的那樣愚蠢。我將項目轉換爲LLVM 1.5(或Xcode 4中的2),並添加了「-Xclang -fobjc-nonfragile-abi2」標誌以允許完全自動生成屬性。但是,在刪除所有@synthesize調用項目時,我還從模型對象中刪除了@dynamic調用。在將數據保存到數據庫時,CoreData不再知道如何處理內存對象。所以這些變化都失去了。 這也是固定點1)以上。錯誤現在按預期發射。 感謝您的幫助 – 2010-07-26 06:12:41

0

這是我保存的核心數據。

AppDelegate *app = (AppDelegate*)[[UIApplication sharedApplication] delegate]; 
Tweet *newTweet = (Tweet *)[NSEntityDescription insertNewObjectForEntityForName:@"Tweet" inManagedObjectContext:app.managedObjectContext]; 
newTweet.status = status; 
newTweet.post_date = theDate; 
newTweet.post_id = post_id; 
newTweet.sent_error = sent_error; 
newTweet.sent_status = sent_status; 
newTweet.screen_name = [Settings getActiveScreenName]; 

// SAVE 
NSError* error = nil; 
if (![app.managedObjectContext save:&error]) {NSLog(@"did this work?? = %@ with userInfo = %@", error, [error userInfo]);} 

我在我的應用程序委託

,這有這個問題,以及在AppDelegate中

/** 
Returns the persistent store coordinator for the application. 
If the coordinator doesn't already exist, it is created and the application's store added to it. 
*/ 
- (NSPersistentStoreCoordinator *)persistentStoreCoordinator { 

    if (persistentStoreCoordinator != nil) { 
     return persistentStoreCoordinator; 
    } 
    // ~/Library/Application Support/iPhone Simulator/User/ 
    NSURL *storeUrl = [NSURL fileURLWithPath: [[self applicationDocumentsDirectory] stringByAppendingPathComponent: @"tweetv12.sqlite"]]; 

    NSError *error; 
    persistentStoreCoordinator = [[NSPersistentStoreCoordinator alloc] initWithManagedObjectModel: [self managedObjectModel]]; 
    if (![persistentStoreCoordinator addPersistentStoreWithType:NSSQLiteStoreType configuration:nil URL:storeUrl options:nil error:&error]) { 
     // Handle error 
     NSLog(@"cannot save data, change db name."); 
    }  

    return persistentStoreCoordinator; 
} 

我也能保存刪除和更新數據與此有關。