解析'此查询具有出色的网络连接'

时间:2015-01-23 11:19:05

标签: ios objective-c parse-platform fetch pfquery

我在我的应用中弹出此错误并导致崩溃。

  

此查询具有出色的网络连接。你必须等到它完成。

当我检查网络连接然后通过本地数据库或在线查询Parse时,就会出现这种情况。

您可以在下面的代码中看到,我调用fetchPrivateRecordsForType创建一个PFQuery,其中包含我传入的数据,例如排序描述符和谓词。

从这个方法我然后调用testInternetConnection,它返回设备是否有网络连接。如果我没有告诉查询提取fromLocalDatastore,则从在线提取。

我将加载我的应用程序,从在线获取数据,然后切换飞行模式并再次获取,但查询转到香蕉并告诉我没有网络,随后是上面看到的最终崩溃声明。

我在isTestingConnection的BOOL检查中添加了以确保查询未执行,直到确认我们是否已连接,但它似乎没有帮助。

-(void)testInternetConnection:(void(^)(BOOL isConnected))complete
{
    _isTestingConnection = YES;
    __weak typeof(self) weakSelf = self;

    internetReachableFoo = [Reachability reachabilityWithHostname:@"www.google.com"];

    // Internet is not reachable
    internetReachableFoo.unreachableBlock = ^(Reachability*reach)
    {
        // Update the UI on the main thread
        dispatch_async(dispatch_get_main_queue(), ^{
            NSLog(@"Someone broke the internet :(");
            weakSelf.isTestingConnection = NO;
            complete(NO);
        });
    };

    // Internet is reachable
    internetReachableFoo.reachableBlock = ^(Reachability*reach)
    {
        // Update the UI on the main thread
        dispatch_async(dispatch_get_main_queue(), ^{
            NSLog(@"Yayyy, we have the interwebs!");
            weakSelf.isTestingConnection = NO;
            complete(YES);
        });
    };

    [internetReachableFoo startNotifier];
}

#pragma mark - Fetching

-(void)fetchPrivateRecordsForType:(NSString *)recordType predicate:(NSPredicate *)predicate sortDescriptor:(NSArray *)sortDescriptors includeKeys:(NSArray *)includeKeys page:(NSInteger)skipMultiplier complete:(void(^)(NSError *error, BOOL didFetchSuccessfully, NSArray *results))completeBlock
{
    PFQuery *query = nil;
    if (!predicate) query = [PFQuery queryWithClassName:recordType];

    //PREDICATE
    else query = [PFQuery queryWithClassName:recordType predicate:predicate];

    //INCLUDE KEYS
    if (includeKeys) for (NSString *key in includeKeys) [query includeKey:key];

    //SORT DESCRIPTORS
    if (sortDescriptors && sortDescriptors.count > 0) [query orderBySortDescriptors:sortDescriptors];

    NSInteger limit = [self queryLimit];
    query.limit = limit;
    query.skip = (limit * skipMultiplier); //starts on page zero

    //fetch locally if no network connection
    [self testInternetConnection:^(BOOL isConnected) {
        if (!isConnected)
        {
            [query fromLocalDatastore];
        }

        NSLog(@"querying");
        if (!_isTestingConnection)
        {
            [query findObjectsInBackgroundWithBlock:^(NSArray *objects, NSError *error) {
                if (!error)
                {
                    //SUCCESS
                    completeBlock(error, YES, objects);
                }
                else
                {
                    //ERROR
                    //error handling should be done where this method is called
                    //this allows for me accurate and infomational errors, not a generic one from here
                }
            }];
        }
    }];
}

-(NSInteger)queryLimit
{
    return 100;
}

一些额外的细节,就在最后一次崩溃之前遇到outstanding network connection错误,我记录了这个错误没有连接:

[Error]: Error Domain=NSURLErrorDomain Code=-1003 "A server with the specified hostname could not be found." UserInfo=0x1700fd380 {NSUnderlyingError=0x170243300 "A server with the specified hostname could not be found.", NSErrorFailingURLStringKey=https://api.parse.com/2/find, NSErrorFailingURLKey=https://api.parse.com/2/find, _kCFStreamErrorDomainKey=12, _kCFStreamErrorCodeKey=8, NSLocalizedDescription=A server with the specified hostname could not be found.} (Code: 100, Version: 1.6.1)

似乎发生了这个错误,因为查询是在没有连接的情况下执行的,但我确保查询在我们确认离线或在线连接之前不会执行,如果它处于脱机状态我设置为提取来自本地数据存储区。所以这对我来说是另一个谜......

修改

一些额外的信息。奇怪的是,当我在iPhone上这样做时,我没有任何问题。我可以断开网络连接并立即执行提取,它将查询本地数据库。

然而,在iPad上,如果我在断开网络连接后立即查询并且没有给它5-10秒来实现断开连接,它仍会认为它已连接并抛出此错误并崩溃。

这让我觉得可能是可达性代码?它是高度使用的Apple Reachability类(Tony Million' s版)。

0 个答案:

没有答案