iOS 13错误:主线程检查器:在后台线程上调用了UI API:-[UITableView reloadData]应用内购买[重复]

问题描述 投票:0回答:1

此问题已经在这里有了答案:

仅在转换为iOS 13.1重大问题后才发现此错误,因为它会导致应用内购买视图崩溃。五年来一直在我们的应用程序中使用此应用程序内Apple代码,因此需要帮助以进行分类。如果您有特定的代码解决方案,请回复。谢谢!

- (void)viewDidLoad
{
    [super viewDidLoad];

    appCode = [Plis getSetting :@"AppCode"];

    NSLog (@"InApp...VDL...appCode %@",appCode);

    [self.tabBarController.tabBar setHidden:NO];

    self.title = @"In-App Store";

    [_detailView setDelegate:self];
    [_detailView setDataSource:self];

    self.refreshControl = [[UIRefreshControl alloc] init];
    [self.refreshControl addTarget:self action:@selector(reload) forControlEvents:UIControlEventValueChanged];
    [self reload];
    [self.detailView reloadData];
    [self.refreshControl beginRefreshing];

    _priceFormatter = [[NSNumberFormatter alloc] init];
    [_priceFormatter setFormatterBehavior:NSNumberFormatterBehavior10_4];
    [_priceFormatter setNumberStyle:NSNumberFormatterCurrencyStyle];

    self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithTitle:@"Restore" style:UIBarButtonItemStylePlain target:self action:@selector(restoreTapped:)];
    }

- (void)restoreTapped:(id)sender {
    [[InAppProdID sharedInstance: appCode] restoreCompletedTransactions];
}

- (void)viewWillAppear:(BOOL)animated {
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(productPurchased:) name:IAPHelperProductPurchasedNotification object:nil];
    [self.tabBarController.tabBar setHidden:NO];
}

- (void)viewWillDisappear:(BOOL)animated {
    [[NSNotificationCenter defaultCenter] removeObserver:self];
}

- (void)productPurchased:(NSNotification *)notification {

    NSString * productIdentifier = notification.object;
    [productArr enumerateObjectsUsingBlock:^(SKProduct * product, NSUInteger idx, BOOL *stop) {
        if ([product.productIdentifier isEqualToString:productIdentifier]) {

            self.productPurchased = productIdentifier;
            [self unlockProduct];

            [self.detailView reloadRowsAtIndexPaths:@[[NSIndexPath indexPathForRow:idx inSection:0]] withRowAnimation:UITableViewRowAnimationFade];
            *stop = YES;
        }
    }];

}

- (void)reload {
    productArr = nil;
    [self.detailView reloadData];
    [[InAppProdID sharedInstance: appCode] requestProductsWithCompletionHandler:^(BOOL success, NSArray *products) {
        if (success) {
            productArr = products;
            // HERE IS WHERE THE ERROR OCCURS!
            [self.detailView reloadData];
        }
        [self.refreshControl endRefreshing];
    }];
}
ios objective-c in-app-purchase app-store ios13
1个回答
0
投票

您只需要从主线程操作UI。只需使用以下命令切换到主线程:

dispatch_async(dispatch_get_main_queue(), ^{
    [self.detailView reloadData];
});

此外,您还需要以相同的方式重写以下调用:

[self.refreshControl endRefreshing];
[self.detailView reloadRowsAtIndexPaths:@[[NSIndexPath indexPathForRow:idx inSection:0]] withRowAnimation:UITableViewRowAnimationFade];
© www.soinside.com 2019 - 2024. All rights reserved.