gpt4 book ai didi

ios - moveRowAtIndexPath - 将订单号保存到核心数据 block UI

转载 作者:行者123 更新时间:2023-11-28 18:30:28 25 4
gpt4 key购买 nike

在我的应用程序中,我使用了一个填充有核心数据对象的 UITableView。我想让改变单元格的顺序成为可能。所以我实现了

-(BOOL)tableView:(UITableView *)tableView canMoveRowAtIndexPath:(NSIndexPath *)indexPath {

return YES;
}

-(void)tableView:(UITableView *)tableView moveRowAtIndexPath:(NSIndexPath *)sourceIndexPath toIndexPath:(NSIndexPath *)destinationIndexPath {

我将每个对象的订单号保存到 Core Data,以将其用于排序描述符,从而使重新排序持久化。所以我在做

- (void)tableView:(UITableView *)tableView moveRowAtIndexPath:(NSIndexPath *)sourceIndexPath toIndexPath:(NSIndexPath *)destinationIndexPath {

NSMutableArray *allFRCObjects = [[self.fetchedResultsController fetchedObjects] mutableCopy];

NSString *stringToMove = [allFRCObjects objectAtIndex:sourceIndexPath.row];
[allFRCObjects removeObjectAtIndex:sourceIndexPath.row];
[allFRCObjects insertObject:stringToMove atIndex:destinationIndexPath.row];

NSUInteger countInt = [allFRCObjects count];

float f = (float) countInt;

f = f -1;

for (NSManagedObject *mo in allFRCObjects) {

[mo setValue:[NSNumber numberWithFloat:f] forKey:@"orderNumberFloat"];

f--;

}
}

这在模拟器中工作,但是如果我在 TableView 中有许多对象的旧 iPad 3 上测试,UI 会被阻塞几秒钟,很明显

case NSFetchedResultsChangeUpdate:

[self configureCell:[tableView cellForRowAtIndexPath:indexPath] atIndexPath:indexPath];

被多次调用是什么导致了 UI 的阻塞我猜...

我不是很擅长这些异步的东西等等。对调度做了一些尝试和错误,但没有帮助。

我该怎么办?

感谢您的帮助!

编辑

- (void)controller:(NSFetchedResultsController *)controller didChangeObject:(id)anObject
atIndexPath:(NSIndexPath *)indexPath forChangeType:(NSFetchedResultsChangeType)type
newIndexPath:(NSIndexPath *)newIndexPath
{

case NSFetchedResultsChangeUpdate:

在我的 For-Loop 中被调用得太频繁了

moveRowAtIndexPath:

我想知道我是否可以避免这种情况!?

谢谢

最佳答案

从运行 Instruments 和使用 Time Profiler 开始。一旦您可以在 Instruments 中看到该 block ,请关闭系统库并反转调用堆栈。这将向您显示哪段代码正在耗费时间。

很可能是您的 UITableViewCell 的填充或绘图。

无论哪个方法显示占用时间,双击该方法,它都会显示代码行。

这将是您需要更正的代码。

关于ios - moveRowAtIndexPath - 将订单号保存到核心数据 block UI,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32761125/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com