Core Data更新后台位置的位置导致阻塞UI



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

- (void)viewDidLoad
{
    [super viewDidLoad];
    temporaryContext = [[NSManagedObjectContext alloc] initWithConcurrencyType:NSPrivateQueueConcurrencyType];
    temporaryContext.parentContext = [[CoreDataManager manager] managedObjectContext];
    temporaryContext.undoManager = nil;
    ...
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{   
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:PositionCellIdentifier forIndexPath:indexPath];
    [self configureCell:(PanelPositionCell *)cell atIndexPath:indexPath];
    return cell;
}
- (void)configureCell:(PanelPositionCell *)cell atIndexPath:(NSIndexPath *)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)checkAddress:(OpenPositionCD *)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;
}

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

你可能想要M. Zarra的设置

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

您将在工作上下文中执行后台工作,save将把更改推送到主上下文中。你可以在方便的时候保存主上下文,只有当写入上下文保存时,数据存储才会在后台被击中。

最后,你在另一个线程中使用position对象。您需要将调用包装到工作上下文的performBlock块中以安全地使用这些对象。

最新更新