ios - UIRefreshControl 滞后

标签 ios objective-c uirefreshcontrol

我有一个包含用户帖子的表格 View 。每个帖子都有图片、用户名和帖子本身。刷新控件的操作是使用来自 Parse 的数据重新加载表。除了拉动刷新时的极度延迟外,一切都完美无缺。不知道是因为每个单元格里的图片还是别的原因。如果有人知道为什么会发生这种情况,请告诉我。如果有人需要,我会发布代码。

-(void)viewDidLoad {

    refreshControl = [[UIRefreshControl alloc] init];
[refreshControl addTarget:self action:@selector(retrieveFromParse)      forControlEvents:UIControlEventValueChanged];
refreshControl.backgroundColor = [UIColor whiteColor];
refreshControl.tintColor = [UIColor colorWithRed:0.3878 green:0.5686 blue:1.0 alpha:0.9];
[userPostsTable addSubview:refreshControl];

}


-(void) retrieveFromParse {

PFQuery *quoteQuery = [PFQuery queryWithClassName:@"Quotes"];
quoteQuery.cachePolicy = kPFCachePolicyCacheThenNetwork;
[quoteQuery orderByDescending:@"createdAt"];
[quoteQuery getFirstObjectInBackgroundWithBlock:^(PFObject *object, NSError *error) {

    if (!error) {

        NSString *category = [object objectForKey:@"Category"];

        if (category == nil) {

            quoteLabel.font = [UIFont fontWithName:@"Helvetica Neue Light Italic" size:15];

        }

        else {

            quoteLabel.font = [UIFont fontWithName:@"Helvetica Neue" size:15];

        }

        quoteString = [object objectForKey:@"quoteContent"];
        quoteLabel.text = quoteString;

        quoteIdString = object.objectId;
        [refreshControl endRefreshing];


    }

    else {

        [refreshControl endRefreshing];
    }
}];

PFQuery *userPostsQuery = [PFQuery queryWithClassName:@"userPosts"];
userPostsQuery.cachePolicy = kPFCachePolicyCacheThenNetwork;
[userPostsQuery setLimit:300];
[userPostsQuery whereKey:@"quoteObjectId" matchesKey:@"objectId" inQuery:quoteQuery];
if (segmentedControl.selectedSegmentIndex == 0) {

    [userPostsQuery orderByDescending:@"createdAt"];

}
else {

[userPostsQuery orderByDescending:@"likeCount"];

}
[userPostsQuery findObjectsInBackgroundWithBlock:^(NSArray *objects, NSError *error) {

    if (!error) {

        userPostsArray = [[NSArray alloc] initWithArray:objects];
        [userPostsTable reloadData];
        [refreshControl endRefreshing];

    }

    else {

        [refreshControl endRefreshing];
    }
}];


}

最佳答案

您正在后台线程中检索对象。

getFirstObjectInBackgroundWithBlock:

您还试图从后台线程更新您的用户界面。您只需要从主线程更新 UI:

调用所有与 UI 相关的调用,例如:

[userPostsTable reloadData];
[refreshControl endRefreshing];

仅来自主线程。

使用:

dispatch_async(dispatch_get_main_queue(), ^{
  // Update UI
});

关于ios - UIRefreshControl 滞后,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27673599/

相关文章:

swift - 如何将静态参数传递给 addTarget #Selector 中 UIRefreshControl() 的本地函数

ios - Swift -如何在具有多个部分的 collectionView 上执行批量更新

iphone - 使用 touchesMoved 缩放 :

ios-charts:无法将库导入新项目

objective-c - 你如何使用 NSDecimalNumber 获得除法的整数和模数(mod)

objective-c - 如何使用 route-me 创建透明层

objective-c - 分割音频文件

react-native - 在下拉之前显示 React Native refreshControl

ios - 导航栏中的自定义图标 Swift 3

UITableView 和 UIRefreshControl