为什么PFQuery.cancel()作为parse.com-Framework的一部分不起作用?

时间:2014-10-07 18:44:48

标签: ios parse-platform

我发现了很多与此话题相关的帖子,但我可能会继续做错事......

创建query(PFQuery实例)后,请求实现为query.findObjects()(在后台线程上运行)。在该请求期间,我无法像方法query.cancel()那样取消其过程。

场景:缺少Internet连接,query.findObjects()尝试连接,失败,再次尝试。我实现了query.cancel(),它在请求第一次失败时执行(在上述点执行的if objects == nil中),仍然会进行第二次尝试。在第二次尝试之后 - 每次尝试大约需要15秒 - 失败了,它不会触发第三次。

为什么会发生这种情况,为什么调用query.cancel()时进程不会中断?

感谢您的帮助!

编辑1:一些代码

func getPost() {

    let queue = dispatch_queue_create("SerialBgQueue", DISPATCH_QUEUE_SERIAL)

    dispatch_async(queue, {

        var query = PFQuery(className: "Post")

        var objects = query.findObjects()

        if objects != nil {

            // do something

        } else {

            println("This part is executed now")

            // doesn't stop the ongoing (second) connection attempt:
            query.cancel()

           // do something

            return
        }
    })
}
编辑2:那是日志 - 对我而言,它看起来像第二批尝试,检查日志(尝试2,3,4,......然后再次开始2,3,4,......)。 else { - 部分在中间执行一次,在最后执行第二次。在那之后,一切都停止了,没有更多的“尝试”。

2014-10-08 13:37:10.104 Instagram[9210:391955] Error: Error Domain=NSURLErrorDomain Code=-1009 "The Internet connection appears to be offline." UserInfo=0x7fe708c545c0 {NSUnderlyingError=0x7fe708c53020 "The Internet connection appears to be offline.", NSErrorFailingURLStringKey=https://api.parse.com/2/find, NSErrorFailingURLKey=https://api.parse.com/2/find, _kCFStreamErrorDomainKey=12, _kCFStreamErrorCodeKey=8, NSLocalizedDescription=The Internet connection appears to be offline.} (Code: 100, Version: 1.3.0)

2014-10-08 13:37:10.105 Instagram[9210:391952] Network connection failed. Making attempt 2 after sleeping for 1.092226 seconds.

2014-10-08 13:37:11.311 ... (Code: 100, Version: 1.3.0) // same Error like in the very first line

2014-10-08 13:37:11.312 Instagram[9210:391945] Network connection failed. Making attempt 3 after sleeping for 2.184451 seconds.

2014-10-08 13:37:13.704 ... (Code: 100, Version: 1.3.0) 

2014-10-08 13:37:13.704 Instagram[9210:391952] Network connection failed. Making attempt 4 after sleeping for 4.368902 seconds.

2014-10-08 13:37:18.514 ... (Code: 100, Version: 1.3.0)

2014-10-08 13:37:18.514 Instagram[9210:391952] Network connection failed. Making attempt 5 after sleeping for 8.737804 seconds.

2014-10-08 13:37:27.257 ... (Code: 100, Version: 1.3.0)

This part is executed now

2014-10-08 13:37:27.265 ... (Code: 100, Version: 1.3.0)

2014-10-08 13:37:27.266 Instagram[9210:392183] Network connection failed. Making attempt 2 after sleeping for 1.387503 seconds.

2014-10-08 13:37:28.792 ... (Code: 100, Version: 1.3.0)

2014-10-08 13:37:28.793 Instagram[9210:392202] Network connection failed. Making attempt 3 after sleeping for 2.775006 seconds.

2014-10-08 13:37:31.843 ... (Code: 100, Version: 1.3.0)

2014-10-08 13:37:31.844 Instagram[9210:392205] Network connection failed. Making attempt 4 after sleeping for 5.550011 seconds.

2014-10-08 13:37:37.401 ... (Code: 100, Version: 1.3.0)

2014-10-08 13:37:37.401 Instagram[9210:392202] Network connection failed. Making attempt 5 after sleeping for 11.100023 seconds.

2014-10-08 13:37:49.050 ... (Code: 100, Version: 1.3.0)

This part is executed now

1 个答案:

答案 0 :(得分:1)

我认为您对findObjects()的假设不正确。该变量阻止了它所启动的线程(发布的情况下的后台线程),并在成功或失败时返回。当您致电取消时,您的代码中无需取消任何内容。

典型的取消模式是代码在不同的线程上启动查询,然后决定它需要取消,就像这样(在Objective-C中......对不起)

PFQuery *query = [PFQuery queryWithClassName:@"Post"];    
[query findObjectsInBackgroundWithBlock:^(NSArray *objects, NSError *error) {
    // this block might get called more than once, depending on the cache policy
    // but it will be called with an error only once and stop after that
    // (no second attempt that I know about) so as far as I can tell, there's
    // no reason to ever call cancel here.
}];

假设您不希望查询占用时间超过10秒。然后...

[query performSelector:@selector(cancel) withObject:nil afterDelay:10.0];