UIRefreshControl滞后

时间:2014-12-28 04:01:12

标签: ios objective-c uirefreshcontrol

我有一个包含用户帖子的tableview。每个帖子都有一张图片,用户名和帖子本身。刷新控件的操作是使用来自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];
    }
}];


}

2 个答案:

答案 0 :(得分:1)

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

getFirstObjectInBackgroundWithBlock:

此外,您正在尝试从后台线程更新UI。您只需要从主线程更新UI:

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

[userPostsTable reloadData];
[refreshControl endRefreshing];

仅限主线。

使用:

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

答案 1 :(得分:0)

假设您的代码段是UITableViewController的一部分,则不应将refreshControl添加为subview的{​​{1}}。

tableView