【问题标题】:PFQueryTableView Auto Refresh When New Data Updated or Refresh Every Minute Using ParsePFQueryTableView 在新数据更新或每分钟使用 Parse 刷新时自动刷新
【发布时间】:2014-03-12 04:22:29
【问题描述】:

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

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

或者也许每分钟刷新一次?

听说reloaddata,但是代码怎么实现呢?

- (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;
}

【问题讨论】:

  • 不确定我会每分钟检查一次,但您是否尝试过添加NSTimer
  • 您也许可以在云代码中安排一个后台作业,如果数据更新,也许会触发静默推送通知?

标签: ios refresh parse-platform pfquery


【解决方案1】:

在.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] 即可。这完全取决于您如何设计编码结构。

【讨论】:

    猜你喜欢
    • 2013-11-17
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2022-10-31
    • 1970-01-01
    • 2018-02-18
    • 2018-05-16
    相关资源
    最近更新 更多