【问题标题】:Swift - Parse PFQueryTableViewController Error on loading the LocalDataStoreSwift - 加载 LocalDataStore 时解析 PFQueryTableViewController 错误
【发布时间】:2015-07-09 13:19:29
【问题描述】:

美好的一天!我在我的 swift 项目中使用 Parse,现在我的问题是加载查询或对象并将其保存到 localDataStore,我尝试了这种方法

override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath, object: PFObject?) -> PFTableViewCell {

        var cell = tableView.dequeueReusableCellWithIdentifier("Cell") as CustomPFTableViewCell!
        if cell == nil {
            cell = CustomPFTableViewCell(style: UITableViewCellStyle.Default, reuseIdentifier: "Cell")
        }


        // Extract values from the PFObject to display in the table cell
        if let placeName = object?["placeName"] as? String {
            cell.cellName.text = placeName
        }
        if let station = object?["station"] as? String {
            cell.cellDetail.text = station
        }
        if let placeImg = object?["placeImg"] as? String {


            let decodedData = NSData(base64EncodedString: placeImg, options: NSDataBase64DecodingOptions(rawValue: 0))
           // var decodedimage = UIImage(data: decodedData!)
            var finImage = UIImage(data: decodedData!)
            cell.cellBgImg.image = finImage



        }

        PFObject.pinAllInBackground(self.objects, block: { (succeeded, error) -> Void in

            if (error == nil) {
            }else {
                println(error!.userInfo)
            }
        })



        return cell
    }

现在在我的 queryForTable 方法中我有这个

override func queryForTable() -> PFQuery {

        // Start the query object
        var query = PFQuery(className: "Places")

        // Add a where clause if there is a search criteria
        if searchBar.text != "" {
            query.whereKey("filterKeyword", containsString: searchBar.text.lowercaseString)
        }

        // Order the results
        query.orderByAscending("placeName")

        var cReturn = PFQuery()

        if (IJReachability.isConnectedToNetwork()) {

            cReturn = query

        } else {

            cReturn = query.fromLocalDatastore()

        }

        return cReturn
    }

如您所见,我使用Reachability 来检查设备是否已连接到互联网。如果没有,查询将返回query.fromLocalDataStore,如果设备已连接,则返回正常的query以获取最新数据。

现在,我的问题是,当我关闭互联网进行测试时,它给了我一个错误 'Method requires Pinning enabled.',我已经在 tableView 方法中这样做了

PFObject.pinAllInBackground(self.objects, block: { (succeeded, error) -> Void in

            if (error == nil) {
            }else {
                println(error!.userInfo)
            }
        })

你认为我做错了什么?谢谢!

【问题讨论】:

    标签: swift parse-platform reachability local-datastore pfquerytableviewcontrolle


    【解决方案1】:

    我认为您应该将固定对象的方法放在 objectsDidLoad() 方法中,而不是放在 cellForRowAtindexPath() 方法中。

    【讨论】:

    • 我按照你说的做了,但还是出现同样的错误。
    • @LEVISOGCPAS 更新了我的答案,试试objectsDidLoad
    • 是的,还是一样:(
    • 当你连接到网络时,objectsDidLoad() 会被调用吗?你还确定你在AppDelegate 中做了Parse.enableLocalDataStore() 吗?
    • 谢谢!我不敢相信我错过了Parse.enableLocalDataStore()。大声笑
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2016-12-27
    • 2015-06-02
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多