1
votes

Duplicate of :

https://stackguides.com/questions/17037149/ios-parse-com-app-crashes-while-retrieve-data-from-cache-nsinternalinconsiste

have implemented an iOS App using Parse.com

Trying to retrieve from cache.

While retrieve data from cache i got an error like this:

Terminating app due to uncaught exception 'NSInternalInconsistencyException', reason: 'This query has an outstanding network connection. You have to wait until it's done.'

When i browse for the issues i found that:

Some suggested that it may happens due to making two query calls on the same query object without waiting for the first to complete.

how to avoid these simultatanius calls in this app

  1. query setLimit: limit]; [query setSkip: skip];

        //RETRIEVING FROM CACHE
         query.cachePolicy = kPFCachePolicyCacheThenNetwork;
    
        [query findObjectsInBackgroundWithBlock:^(NSArray *objects, NSError *error) {
            if (!error)
            {
                [allObjects removeAllObjects]; //Added regarding cache ******
    
                // The find succeeded. Add the returned objects to allObjects
                [allObjects addObjectsFromArray:objects];
    
                if (objects.count == limit) {
                    // There might be more objects in the table. Update the skip value and execute the query again.
                    skip += limit;
                    [query setSkip: skip];
                    // Go get more results
                    weakPointer();
                }
                else
                {
                    // We are done so return the objects
                    block(allObjects, nil);
                }
    
            }
            else
            {
                block(nil,error);
            }
        }];
    
2

2 Answers

0
votes

To avoid simultaneous calls of a PFQuery, call [query cancel] before findObjects is called:

query.cachePolicy = kPFCachePolicyCacheThenNetwork;
[query cancel]; //cancels the current network request being made by this query (if any)
[query findObjectsInBackgroundWithBlock:^(NSArray *objects, NSError *error) {
    //do stuff
}];
0
votes

Careful:

[query cancel];

Does not work if you're expecting the callback.

Solution:

@property (nonatomic) BOOL isCurrentlyFetching; // Makes sure Parse is called only

-(void)myQuery
{

    if (_isCurrentlyFetching) { return; } // Bails if currently fetching
    _isCurrentlyFetching = YES;

    //RETRIEVING FROM CACHE
     query.cachePolicy = kPFCachePolicyCacheThenNetwork; // Whatever your cache policy is

    [query findObjectsInBackgroundWithBlock:^(NSArray *objects, NSError *error) {

        if (!error){
            // Your code...
        }
        else{
            // Your code...
        }

        _isCurrentlyFetching = NO;

    }];
}