作者热门文章
- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我在使用 Core Data 时遇到了一些性能问题,我希望有人能给我一些关于如何改进它的提示。调用save:
时对于我的NSManagedObjectContext
,单个实体的保存时间超过 1 秒。我正在主线程上执行保存,因此在此期间它会锁定 GUI,这是 Not Acceptable 。这是我正在执行保存的代码;此代码在修改 WTSRecord
的“描述”字段后启动:
- (void)saveRecord:(WTSRecord *)record
success:(void (^)(WTSRecord *record))success
failure:(void (^)(NSError *error))failure {
DDLogVerbose(@"saveRecord: %@", record.objectID);
if (record.recordId != nil) {
//mark this record as a pending modify if it has a database id
record.pendingModify = [NSNumber numberWithBool:YES];
}
NSError *error;
NSManagedObjectContext *context = record.managedObjectContext;
NSTimeInterval startTime = [[NSDate date] timeIntervalSince1970];
if (![context saveToPersistentStore:&error]) {
failure(error);
} else {
NSLog(@"time to persist record: %f", ([[NSDate date] timeIntervalSince1970] - startTime));
...do other stuff here...
}
}
2014-08-13 11:25:32.528 Identify[5395:60b] CoreData: sql: BEGIN EXCLUSIVE
2014-08-13 11:25:32.530 Identify[5395:60b] CoreData: sql: UPDATE ZWTSRECORD SET ZDESC = ?, Z_OPT = ? WHERE Z_PK = ? AND Z_OPT = ?
2014-08-13 11:25:32.531 Identify[5395:60b] CoreData: details: SQLite bind[0] = "ffffffffffffuuuuiiiiuu"
2014-08-13 11:25:32.532 Identify[5395:60b] CoreData: details: SQLite bind[1] = (int64)48
2014-08-13 11:25:32.533 Identify[5395:60b] CoreData: details: SQLite bind[2] = (int64)306
2014-08-13 11:25:32.534 Identify[5395:60b] CoreData: details: SQLite bind[3] = (int64)47
2014-08-13 11:25:32.535 Identify[5395:60b] CoreData: sql: COMMIT
2014-08-13 11:25:32.538 Identify[5395:60b] time to persist record: 1.376321
WTSRecord
s 在这个场景中的 sqllite 数据库中。如果我删除了 sqllite 存储并且只修改了一个
WTSRecord
,持续时间不到 1/20 秒。
controllerDidChangeContent:
在我的
UITableViewController
,即绘制表格 View 单元格。为什么要花那么长时间?
NSFetchedResultsControllerDelegate
方法。他们并没有真正改变样板 Apple 代码:
- (void)controllerWillChangeContent:(NSFetchedResultsController *)controller
{
DDLogVerbose(@"FRC calling beginUpdates in controllerWillChangeContent");
[self.tableView beginUpdates];
DDLogVerbose(@"FRC done calling beginUpdates in controllerWillChangeContent");
}
- (void)controller:(NSFetchedResultsController *)controller didChangeSection:(id <NSFetchedResultsSectionInfo>)sectionInfo
atIndex:(NSUInteger)sectionIndex forChangeType:(NSFetchedResultsChangeType)type
{
switch(type) {
case NSFetchedResultsChangeInsert:
DDLogVerbose(@"FRC inserted section");
[self.tableView insertSections:[NSIndexSet indexSetWithIndex:sectionIndex] withRowAnimation:UITableViewRowAnimationFade];
break;
case NSFetchedResultsChangeDelete:
DDLogVerbose(@"FRC deleted section");
[self.tableView deleteSections:[NSIndexSet indexSetWithIndex:sectionIndex] withRowAnimation:UITableViewRowAnimationFade];
break;
}
}
- (void)controller:(NSFetchedResultsController *)controller didChangeObject:(id)anObject
atIndexPath:(NSIndexPath *)indexPath forChangeType:(NSFetchedResultsChangeType)type
newIndexPath:(NSIndexPath *)newIndexPath
{
UITableView *tableView = self.tableView;
switch(type) {
case NSFetchedResultsChangeInsert:
DDLogVerbose(@"FRC inserted object");
[tableView insertRowsAtIndexPaths:@[newIndexPath] withRowAnimation:UITableViewRowAnimationFade];
break;
case NSFetchedResultsChangeDelete:
DDLogVerbose(@"FRC deleted object");
[tableView deleteRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationFade];
break;
case NSFetchedResultsChangeUpdate:
DDLogVerbose(@"FRC updated object");
[self configureCell:[tableView cellForRowAtIndexPath:indexPath] atIndexPath:indexPath];
break;
case NSFetchedResultsChangeMove:
DDLogVerbose(@"FRC moved objects");
[tableView deleteRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationFade];
[tableView insertRowsAtIndexPaths:@[newIndexPath] withRowAnimation:UITableViewRowAnimationFade];
break;
}
}
- (void)controllerDidChangeContent:(NSFetchedResultsController *)controller
{
DDLogVerbose(@"FRC calling endUpdates in controllerDidChangeContent");
[self.tableView endUpdates];
DDLogVerbose(@"FRC done calling endUpdates in controllerDidChangeContent");
}
[self.tableView endUpdates]
上。 .这是我的日志的打印输出:
2014-08-14 10:44:39:663 Identify[5718:60b] Saving to context(<NSManagedObjectContext: 0x145b82c0>)
2014-08-14 10:44:39:666 Identify[5718:60b] FRC calling beginUpdates in controllerWillChangeContent
2014-08-14 10:44:39:666 Identify[5718:60b] FRC done calling beginUpdates in controllerWillChangeContent
2014-08-14 10:44:39:668 Identify[5718:60b] FRC updated object
**2014-08-14 10:44:39:671 Identify[5718:60b] FRC calling endUpdates in controllerDidChangeContent
**2014-08-14 10:44:40:889 Identify[5718:60b] FRC done calling endUpdates in controllerDidChangeContent
2014-08-14 10:44:41:018 Identify[5718:60b] Time to save in context(<NSManagedObjectContext: 0x145b82c0>): 1.355229
最佳答案
现在您已经确定您的问题不在于您的保存,而在于您的绘图代码,因此有一些选项。
关于ios - Core Data 保存时性能不佳 : and [UITableView endUpdates],我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25291301/
我是一名优秀的程序员,十分优秀!