PFQueryTableView 在新数据更新时自动刷新或使用 Parse 每分钟刷新一次 [英] PFQueryTableView Auto Refresh When New Data Updated or Refresh Every Minute Using Parse

查看:21
本文介绍了PFQueryTableView 在新数据更新时自动刷新或使用 Parse 每分钟刷新一次的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

目前我正在关注本教程 http://www.appcoda.com/ios-programming-app-backend-parse/

Currently Im following this tutorial http://www.appcoda.com/ios-programming-app-backend-parse/

但是当来自 parse.com 的数据自动更新时,我希望 tableviewcontroller 用新数据刷新.我不希望用户总是需要拉来刷新.

But I want the tableviewcontroller to be refresh with new data when data from parse.com is update automatically. I don't want the user to always have to pull to refresh.

或者每分钟刷新一次时间?

or maybe having time refresh every minute?

我听说过 reloaddata 但如何实现代码?

I heard about reloaddata but how to implement the code?

- (id)initWithCoder:(NSCoder *)aCoder
{
 self = [super initWithCoder:aCoder];
 if (self) {
     // The className to query on
     self.parseClassName = @"Recipe";

     // The key of the PFObject to display in the label of the default cell style
     self.textKey = @"name";

     // Whether the built-in pull-to-refresh is enabled
     self.pullToRefreshEnabled = YES;

     // Whether the built-in pagination is enabled
     self.paginationEnabled = NO;
 }
 return self;
}

- (PFQuery *)queryForTable
{
 PFQuery *query = [PFQuery queryWithClassName:self.parseClassName];

 return query;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath object:(PFObject *)object
{
 static NSString *simpleTableIdentifier = @"RecipeCell";

 UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:simpleTableIdentifier];
 if (cell == nil) {
     cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:simpleTableIdentifier];
 }

 // Configure the cell
 PFFile *thumbnail = [object objectForKey:@"imageFile"];
 PFImageView *thumbnailImageView = (PFImageView*)[cell viewWithTag:100];
 thumbnailImageView.image = [UIImage imageNamed:@"placeholder.jpg"];
 thumbnailImageView.file = thumbnail;
 [thumbnailImageView loadInBackground];

 UILabel *nameLabel = (UILabel*) [cell viewWithTag:101];
 nameLabel.text = [object objectForKey:@"name"];

 UILabel *prepTimeLabel = (UILabel*) [cell viewWithTag:102];
 prepTimeLabel.text = [object objectForKey:@"prepTime"];

 return cell;
}

推荐答案

在 .h 文件中

@property (nonatomic, strong) NSTimer *fetchTimer;
@property (nonatomic, strong) NSArray *items;

在 .m 文件中

- (void)viewDidLoad
{
    NSTimer *timer = [NSTimer timerWithTimeInterval:3600
                                             target:(id)self
                                           selector:@selector(fetch)
                                           userInfo:nil
                                            repeats:YES];
    self.fetchTimer = timer;
}

- (void)fetch
{
    PFQuery *query = [PFObject query];
    [query whereKey:@"" equalTo:@""];
    [query findObjectsInBackgroundWithBlock:^(NSArray *objects, NSError *error) {

        self.items = [NSArray arrayWithArray:objects];
        [self.tableView reloadData];
    }];
}

1) 设置一个间隔时间为 3600 的 NSTimer 并触发获取数据功能.2) 取数据后调用 [self.tableView reloadData] 即可.这完全取决于您如何设计编码结构.

1) Set a NSTimer with 3600 intervalTime and trigger the fetch data function. 2) Just call [self.tableView reloadData] when the data has been fetch. It is all depends on how you design your coding structure.

这篇关于PFQueryTableView 在新数据更新时自动刷新或使用 Parse 每分钟刷新一次的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆