我正在對應用程序進行收尾處理,並且在批量刪除記錄時遇到困難。在一個按鈕的點擊一組約。需要將3500條記錄添加到數據庫中。這不是問題,需要大約。 3-4秒。刪除/刪除核心數據中的記錄非常非常慢
但有時(不經常,但選項需要在那裏)所有這些記錄需要刪除。我剛剛執行了這個操作,花了20分鐘。這裏有什麼可能是錯的?只有一個依賴關係,所有記錄都是特定集合的子集。
我將所有項目添加到一個集合中,將它們從集合中刪除,然後逐個刪除。每5%我更新一次對話,當一切完成時,我會提交更改。但在取出物品只需要在年齡DOItemController(我可以看到進度對話的進展非常緩慢)
- (void) deleteList:(DOCollection *) collection {
// For the progress dialogue
NSMutableDictionary* dict = [NSMutableDictionary dictionaryWithObject:@"Clearing vocabulary list!" forKey:@"message"];
float totalItems = [collection.items count];
float progress = 0;
float nextProgressRefresh = 0.05;
NSMutableSet* itemsSet = [NSMutableSet set];
for (DOItem* item in collection.items) {
[itemsSet addObject:(NSNumber*)[NSNumber numberWithInt:[item.itemId intValue]]];
}
// Remove all of them from the collection
[managedObjectContext performBlockAndWait:^{
[collection setItems:[NSSet set]];
}];
for (NSNumber* itemId in itemsSet) {
DOItem* item = [itemController findItem:[itemId intValue]];
if (item != nil) {
[[self itemController] removeItem:item];
}
progress++;
if((nextProgressRefresh < (progress/totalItems))){
NSString* sProgress = [NSString stringWithFormat:@"%f", (progress/totalItems) * 0.85];
//[dict setValue:@"Saving the database...!" forKey:@"message"];
[dict setValue:sProgress forKey:@"progress"];
[[NSNotificationCenter defaultCenter] postNotificationName:kUpdatePleaseWaitDialogue object:dict];
nextProgressRefresh = nextProgressRefresh + 0.05;
}
}
dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
[managedObjectContext performBlockAndWait:^{
[[self collectionController] commitChanges];
}];
[[NSNotificationCenter defaultCenter] postNotificationName:kSavingDataComplete object:nil];
});
//NSLog(@"Wait 2!");
[NSThread sleepForTimeInterval:1];
}
:
- (void) removeItem: (NSManagedObject*) item {
[[self managedObjectContext] deleteObject:item];
}
DOCollection是一個託管對象嗎?或者不僅僅是一組管理對象? – timthetoolman 2012-01-07 21:27:06
是的都是管理對象 – 2012-01-08 07:44:04
你有核心數據模型中級聯刪除的設置嗎? – timthetoolman 2012-01-08 08:30:35