简体   繁体   English

iOS + Parse.com:kPFCachePolicyCacheThenNetwork,检索数据两次

[英]iOS+ Parse.com : kPFCachePolicyCacheThenNetwork, retrieve data two times

I have build an iOS app using Parse.com 我使用Parse.com构建了一个iOS应用程序

I want to retrieve data from cache initially & then from network 我想先从缓存中检索数据然后从网络中检索数据

For this i have used kPFCachePolicyCacheThenNetwork cache policy wchich is opt for my requirement. 为此,我使用了kPFCachePolicyCacheThenNetwork缓存策略,选择我的要求。

PFQuery *employesquery = [PFQuery queryWithClassName:@"employesTable"];
    [employesquery whereKey:@"UserID" equalTo:[PFUser currentUser]];
    [employesquery includeKey:@"empID"];
    [employesquery includeKey:@"empID.user"];

    MBProgressHUD *hud = [MBProgressHUD showHUDAddedTo:self.navigationController.view  animated:YES];


//APPLIES CACHE... ********
        employesquery.cachePolicy = kPFCachePolicyCacheThenNetwork;

    [employesquery findObjectsInBackgroundWithBlock:^(NSArray *objects, NSError *error) {
        [hud hide:YES];
        NSMutableArray *empData = [[NSMutableArray alloc] init];
        for (PFObject *object in objects)
        {
            //Getting Data For Item
            PFObject *employeeObject = [object objectForKey:@"empID"];
            [empData addObject:employeeObject];
        }
        [self fetchEmployeeData:empData];

    }];

But using this Each data retrieve two times, repeatable data. 但是使用这个每个数据检索两次,可重复的数据。

How to avoid this repeatable data, 如何避免这种可重复的数据,

Once the data is getting from networks the previously shown data (using cache) gets cleared/ hidden. 一旦数据从网络获得,先前显示的数据(使用缓存)将被清除/隐藏。

I have tried with [PFQuery clearAllCachedResults]; 我试过[PFQuery clearAllCachedResults];

It was cleared all the cache so that there no data in cache for the next iteration. 它被清除了所有缓存,因此下一次迭代的缓存中没有数据。

When using kPFCachePolicyCacheThenNetwork as your caching policy your callback is always triggered twice. 使用kPFCachePolicyCacheThenNetwork作为缓存策略时,您的回调始终会被触发两次。 This is because the policy is to use the cache and then go to the network, not to use the cache exclusively. 这是因为策略是使用缓存然后转到网络,而不是专门使用缓存。

The solution is to keep track of how many times your block has been called, so you can establish whether you're getting the cached result or the fresh result from the network. 解决方案是跟踪块被调用的次数,这样您就可以确定是从网络获取缓存结果还是新结果。 Parse have more information about this here . Parse 在这里更多相关信息

You must clear the data array which is used for store objects queried from parse in the block findObjectsInBackgroundWithBlock:^(NSArray *objects, NSError *error). 您必须清除用于在块findObjectsInBackgroundWithBlock中解析的存储对象的数据数组:^(NSArray * objects,NSError * error)。

- (void)refreshData
{
    PFUser *currentUser = [PFUser currentUser];

    NSPredicate *predicate = [NSPredicate predicateWithFormat:[NSString stringWithFormat:@"%@ = '%@' OR %@ = '%@'",
                                                           kMessageUserSendIdKey, currentUser.objectId,
                                                           kMessageUserReceiveIdKey, currentUser.objectId]];

PFQuery *query = [PFQuery queryWithClassName:kMessageClassKey predicate:predicate];
[query addDescendingOrder:kMessageTimeCreatedKey];
[query includeKey:kMessageUserSendKey];
[query includeKey:kMessageUserReceiveKey];
[query setCachePolicy:kPFCachePolicyCacheThenNetwork];

[query findObjectsInBackgroundWithBlock:^(NSArray *objects, NSError *error) {
    if (!error) {
        // TODO: Clear the data array
        self.messagesDataArray = [NSMutableArray new];

        if (objects.count > 0) {
            [self.messagesDataArray addObject:objects[0]];
            for (int i = 1; i < objects.count; i++) {
                PFObject *messageChat = [objects objectAtIndex:i];
                if (![self isExistWithMessageChat:messageChat]){
                    [self.messagesDataArray addObject:messageChat];
                }
            }
            [self.tableView reloadData];
        }
     }
     else {
        NSLog(@"Error: %@ %@", error, [error userInfo]);
     }
   }];
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM