• 设为首页
  • 点击收藏
  • 手机版
    手机扫一扫访问
    迪恩网络手机版
  • 关注官方公众号
    微信扫一扫关注
    迪恩网络公众号

ios - 后台位置的核心数据更新位置导致阻塞 UI

[复制链接]
菜鸟教程小白 发表于 2022-12-12 15:55:24 | 显示全部楼层 |阅读模式 打印 上一主题 下一主题

我正在使用 3 个托管对象上下文架构(为背景创建临时上下文,其父对象是 managedObjectContext - UI,并且其父 writerObjectContext 应该在后台写入数据库)并且在更新对象时遇到阻塞 UI 的问题。最好有例子。所以我的数据库中有数千个点,我使用 NSFetchedResultsControllertableView 来获取它们。这是我的代码:

- (void)viewDidLoad
{
    [super viewDidLoad];

    temporaryContext = [[NSManagedObjectContext alloc] initWithConcurrencyType:NSPrivateQueueConcurrencyType];
    temporaryContext.parentContext = [[CoreDataManager manager] managedObjectContext];
    temporaryContext.undoManager = nil;

    ...
}

- (UITableViewCell *)tableViewUITableView *)tableView cellForRowAtIndexPathNSIndexPath *)indexPath
{   
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifierositionCellIdentifier forIndexPath:indexPath];
    [self configureCellPanelPositionCell *)cell atIndexPath:indexPath];
    return cell;
}

- (void)configureCellPanelPositionCell *)cell atIndexPathNSIndexPath *)indexPath {
    // Fetch Record
    NSManagedObject *record = [self.fetchedResultsController objectAtIndexPath:indexPath];
    OpenPositionCD *position = (OpenPositionCD *)record;

    // Update Cell
    [cell setValuesByOpenPositionCD:position];

    dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_BACKGROUND, 0), ^{
        [self checkAddress:position];
    });
}

- (void)checkAddressOpenPositionCD *)openPosition {
    if (openPosition.latitude == 0 && openPosition.longitude == 0) {
        return;
    }

    if ([openPosition hasAddress]) {
        return;
    }

    CLLocation *location = [[CLLocation alloc]initWithLatitude:[openPosition.latitude doubleValue] longitude:[openPosition.longitude doubleValue]];
    [[LocationManager manager] getPlacemarksForLocation:location withCompletion:^(NSArray *placemarks, NSError *error) {
        if (!error) {
                openPosition.address = placemarks[0];
                            NSError *error = nil;
                if (![temporaryContext save:&error]) {
                    NSLog(@"Unresolved error %@, %@", error, [error userInfo]);
                }
        }
    }];
}

当我滚动到没有地址的单元格时,UI 经常卡住,这取决于我滚动的速度。那么我该如何解决呢?我正在尝试使用/不使用 dispatch_async 和使用/不使用 temporaryContext performBlock 但看起来没有什么可以帮助我。非常感谢您的帮助。

我正在 CoreDataManager 中添加上下文初始化,但我希望没问题:

// Returns the managed object context for the application.
// If the context doesn't already exist, it is created and bound to the persistent store coordinator for the application.
- (NSManagedObjectContext *)managedObjectContext{
    if (_managedObjectContext != nil) {
        return _managedObjectContext;
    }

    _managedObjectContext = [[NSManagedObjectContext alloc] initWithConcurrencyType:NSMainQueueConcurrencyType];
    _managedObjectContext.parentContext = [self writerManagedObjectContext];

    return _managedObjectContext;
}

// Writer context for database
- (NSManagedObjectContext *)writerManagedObjectContext{
    if (_writerManagedObjectContext != nil) {
        return _writerManagedObjectContext;
    }

    NSPersistentStoreCoordinator *coordinator = [self persistentStoreCoordinator];
    if (coordinator != nil) {
        _writerManagedObjectContext = [[NSManagedObjectContext alloc] initWithConcurrencyType:NSPrivateQueueConcurrencyType];
        [_writerManagedObjectContext setPersistentStoreCoordinator:coordinator];
    }
    return _writerManagedObjectContext;
}



Best Answer-推荐答案


您正在使用过时的 API。使用多个上下文的推荐方法是将相同的持久存储协调器分配给子上下文,而是为其分配一个 parentContext

你可能想要 M. Zarra 的设置

WriterContext (background)
MainContext (main thread, parent is WriterContext)
WorkerContext (background, parent is MainContext, create and destroy as needed)

您将在工作人员上下文中进行后台工作,然后 save 将更改推送到主上下文。您可以在方便的时候保存主上下文,并且只有在编写器上下文保存时才会在后台访问数据存储。

最后,您在不同的线程中使用了位置对象。您需要将调用包装到工作上下文的 performBlock block 中以安全地使用这些对象。

关于ios - 后台位置的核心数据更新位置导致阻塞 UI,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31703719/

回复

使用道具 举报

懒得打字嘛,点击右侧快捷回复 【右侧内容,后台自定义】
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

关注0

粉丝2

帖子830918

发布主题
阅读排行 更多
广告位

扫描微信二维码

查看手机版网站

随时了解更新最新资讯

139-2527-9053

在线客服(服务时间 9:00~18:00)

在线QQ客服
地址:深圳市南山区西丽大学城创智工业园
电邮:jeky_zhao#qq.com
移动电话:139-2527-9053

Powered by 互联科技 X3.4© 2001-2213 极客世界.|Sitemap