从Parse上的缓存UITableView数据加载

时间:2014-10-01 08:18:53

标签: ios objective-c uitableview caching parse-platform

我有一个嵌套查询,如下所示,即使我使用(或者我认为我使用)解析缓存,也需要3-4秒才能加载。它没有像我预期的那样工作。是否有可能在首次启动时从缓存加载,然后从解析云更新。我是否至少需要核心数据或sqlite3来与本地解析同步?

当我使用“query.cachePolicy”tableview得到重复的结果时,我怎样才能以正确的方式使用它。

- (void) retrieveSnapsFromParse {

NSString *currentUserName = [PFUser currentUser].username;

PFQuery *retrieveSnaps = [PFQuery queryWithClassName:@"Snap"];
[retrieveSnaps whereKey:@"from" equalTo:[PFUser currentUser].username];

PFQuery *retrieveSnaps2 = [PFQuery queryWithClassName:@"Snap"];
[retrieveSnaps2 whereKey:@"to" equalTo:[PFUser currentUser].username];

PFQuery * query = [PFQuery orQueryWithSubqueries:@[retrieveSnaps, retrieveSnaps2]];
query.cachePolicy = kPFCachePolicyCacheThenNetwork;
[query orderByDescending:@"createdAt"];
[query findObjectsInBackgroundWithBlock:^(NSArray *objects, NSError *error) {
    if (!error) {
    for (PFObject *object in objects) {

            Snap *snap = [Snap new];

     if([currentUserName isEqualToString:[object objectForKey:@"to"]]) {
                snap.userName = [object objectForKey:@"from"];
                snap.sentOrReceived = @"Received";

                PFFile *snapImageFile = [object objectForKey:@"snappedimage"];
                [snapImageFile getDataInBackgroundWithBlock:^(NSData *data, NSError *error) {
                    if (!error) {
                        UIImage *snapImage = [UIImage imageWithData:data];
                        snap.snapImage = snapImage;
                    }
                }];

            } else if([currentUserName isEqualToString:[object objectForKey:@"from"]]) {
                snap.userName = [object objectForKey:@"to"];
                snap.sentOrReceived = @"Sent";

            }


            snap.opened = [object[@"opened"]boolValue];
            snap.counter = [object[@"second"]intValue];
            snap.objectId = [NSString stringWithFormat:@"%@",object.objectId];

            NSDate *createdAt = object.createdAt;
            snap.createdDate = createdAt;

    // Profile image for UICustomTableViewCell
      PFQuery *userQuery = [PFUser query];
            [userQuery whereKey:@"username" equalTo:snap.userName];
             userQuery.cachePolicy = kPFCachePolicyCacheElseNetwork;
            [userQuery getFirstObjectInBackgroundWithBlock:^(PFObject *object, NSError *error) {
                if (object) {
                    snap.userFirstName = [object objectForKey:@"firstname"];
                    PFFile *imageFile = [object objectForKey:@"profileimage"];
                    [imageFile getDataInBackgroundWithBlock:^(NSData *data, NSError *error) {
                        if (!error) {
                            UIImage *image = [UIImage imageWithData:data];
                            snap.userProfileImage = image;

                        }

                    }];

                    [snapsTemp addObject:snap];
                }

        snapsTable = snapsTemp;
                    [self.tableView reloadData];

            }];
     }
    }
}];

}

我在下面的cellForRowAtIndexPath上使用的代码;

cell.cellUserProfileImage.image = snap.userProfileImage;

也可以在.m文件中获取可变数组

    @implementation SnapsVC
{
    NSMutableArray *snapsTable;
}

1 个答案:

答案 0 :(得分:0)

在我将下面的代码放到cellForRowAtIndexPath而不是retrieveSnapsFromParse之后,它开始按预期工作

PFQuery *userQuery = [PFUser query];
[userQuery whereKey:@"username" equalTo:snap.userName];
userQuery.cachePolicy = kPFCachePolicyCacheElseNetwork;
[userQuery getFirstObjectInBackgroundWithBlock:^(PFObject *object, NSError *error) {
    if (object) {

        PFFile *imageFile = [object objectForKey:@"profileimage"];
        [imageFile getDataInBackgroundWithBlock:^(NSData *data, NSError *error) {
            if (!error) {
                cell.cellUserProfileImage.image = [UIImage imageWithData:data];
            }

        }];

        cell.cellUserFirstNameLabel.text = [object objectForKey:@"firstname"];
    }
}];