1

重复:

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

已经使用 Parse.com 实现了一个 iOS 应用程序

试图从缓存中检索。

从缓存中检索数据时,出现如下错误:

由于未捕获的异常“NSInternalInconsistencyException”而终止应用程序,原因:“此查询具有未完成的网络连接。你必须等到它完成。

当我浏览问题时,我发现:

一些人认为这可能是由于对同一个查询对象进行两次查询调用而没有等待第一个完成。

如何在这个应用程序中避免这些同时调用

  1. 查询 setLimit:limit]; [查询集跳过:跳过];

        //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);
            }
        }];
    
4

2 回答 2

0

为了避免同时调用一个 PFQuery,调用[query cancel]beforefindObjects被调用:

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
}];
于 2014-01-24T05:53:00.470 回答
0

小心:

[query cancel];

如果您期待回调,则不起作用。

解决方案:

@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;

    }];
}
于 2015-07-01T15:20:45.673 回答