【问题标题】:Cancelling PFQuery quicker due to no network connection由于没有网络连接,更快地取消 PFQuery
【发布时间】:2015-09-03 23:49:15
【问题描述】:

我正在尝试取消查询并在没有连接时为用户设置通知。有没有比我发现的更好的方法?在警报发生之前,查询仍会运行大约 15 秒以上。

    let query = Item.query()

    query!.findObjectsInBackgroundWithBlock { (objects: [AnyObject]?, error: NSError?) -> Void in
        if error == nil {
            for object in objects! {
                self.items.append(object as! Item)
                if self.items.count == 35 {
                    break
                }
            }
        } else if error!.code == 100 {
            query?.cancel()
            print("query cancelled")
            let alertController = UIAlertController(title: "Oops", message: "Trouble With Network", preferredStyle: .Alert)
            let defaultAction = UIAlertAction(title: "OK", style: .Default, handler: nil)
            alertController.addAction(defaultAction)

            self.presentViewController(alertController, animated: true, completion: nil)

        } else  {
            print("non network Error with query")
        }
        self.tableView.reloadData()
    }
}

【问题讨论】:

    标签: ios swift parse-platform network-programming pfquery


    【解决方案1】:

    为什么不在启动查询之前检查 Parse 是否可访问?我经常使用的代码(在 Objective-C 中,不过将其转换为 Swift 并不难)如下,并使用可达性框架

    #import <SystemConfiguration/SystemConfiguration.h>
    #import "Reachability.h"
    
    @property (nonatomic, readonly) int networkStatus;
    
    - (BOOL)isParseReachable {
    return self.networkStatus != NotReachable;
    }
    

    然后在开始查询之前调用该方法。如果它返回NO,您可以向用户显示一条消息并跳过查询。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-06-26
      • 2016-03-01
      • 1970-01-01
      • 2020-07-28
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多