我的核心數據代碼的時間太長,所以我評論了大部分的線路,它簡化了這個:爲什麼添加單個實體後,我的核心數據需要很長時間才能保存?
NSString *storePath = [[self applicationDocumentsDirectory] stringByAppendingPathComponent:@"CoreDataStore.sqlite"];
NSString *backupSourceStorePath = [[self applicationDocumentsDirectory]
stringByAppendingPathComponent:@"CoreDataStoreBackup.sqlite"];
NSManagedObjectContext *oldContext = [self version1ManagedObjectContext];
TICDSSynchronizedManagedObjectContext *newContext = [self managedObjectContext];
NSFetchRequest *oldFetchRequest = [[NSFetchRequest alloc] init];
NSEntityDescription *oldEntryEntity = [NSEntityDescription entityForName:@"Entry"
inManagedObjectContext:oldContext];
[oldFetchRequest setEntity:oldEntryEntity];
int numberOfEntries = [oldContext countForFetchRequest:oldFetchRequest error:nil];
int batchSize = 10;
[oldFetchRequest setFetchLimit:batchSize];
int offset = 0;
while (numberOfEntries - offset > 0) {
[oldFetchRequest setFetchOffset:offset];
NSError *error;
NSArray *entries = [oldContext executeFetchRequest:oldFetchRequest error:&error];
int i = 0;
for (NSManagedObject *entry in entries) {
Entry *newEntry = [NSEntityDescription insertNewObjectForEntityForName:@"Entry"
inManagedObjectContext:newContext];
i++;
NSLog(@"i: %i", i);
}
[newContext save:&error];
NSLog(@"Save error: %@", error);
offset = offset + batchSize;
}
如果我沒有在那裏insertNewObjectForEntityForName
線,節省用不了多久在所有。但是就這一點而言,節省大約需要一分鐘。在此之前,數據存儲中沒有別的東西。任何可能發生這種情況的原因?
你的意思是行'[newContext節省:錯誤]'走大約一分鐘?另外,你能否評論你正在做的事情的上下文 - 將託管對象從一個上下文複製到另一個上下文? – FluffulousChimp
正確,該行需要大約一分鐘的時間。基本上,我正在複製它們。這是使用大型數據庫進行標準遷移的唯一方法。如果通過自動映射完成,它會耗盡內存並在一段時間後死亡。所以我必須手動完成。 – Andrew