swift - 从解析本地数据存储中删除对象 unpinAll 不起作用

标签 swift parse-platform parse-server parse-ios-sdk

所以我正在将一堆数据写入解析本地数据存储,但想在固定新表之前删除旧表。问题是它似乎没有被删除,并且我最终得到同一个表的多个条目。

这是将数据写入本地数据存储的代码。

 class func buildHistoryPart(selectedPartId: String? = nil) {

        let query = PFQuery(className: "Part")
            query.includeKey("fromPack")

            query.findObjectsInBackground { (objects, error) in

            if error != nil {

                print(error!)

            } else if let parts = objects {

                for object in parts {

                    //if the objectID is equal to the id of the part received
                    if object.objectId == selectedPartId {

                        // if the fromPack column has data
                        if let fromPack = object.object(forKey: "fromPack") as? PFObject {

                            // create the class name from the pack name of the selected part
                            if let className = (fromPack.object(forKey: "packName") as? String) {

                                // creeate PFObject to rretrieved fields to
                                let historyClass = PFObject(className: className) as PFObject

                                    //add the objects to new class
                                    historyClass.add(object.objectId as Any, forKey: "partId")
                                    historyClass.add(object.object(forKey: "partName") as Any, forKey: "partName")
                                    historyClass.add(fromPack.object(forKey: "packName")!, forKey: "packName")
                                    historyClass.add(fromPack.objectId as Any, forKey: "packId")


                                        // unpin the old data
                                        PFObject.unpinAll(inBackground: [historyClass], withName: "pinnedHistory", block: { (success, error) in

                                            if success {

                                                // if successful pin the new data
                                                PFObject.pinAll(inBackground: [historyClass], withName: "pinnedHistory")
                                            }
                                        })
                            }
                        }

                    }
                }
            }
        }
    }

它不会取消固定,每次运行该函数时,我都会在 LDS 中得到一堆表。

--------------------编辑--------------------

由于我无法使用他们记录的“withName”属性让解析函数工作,所以我的解决方法是调用一个专门查询相关表的函数,如果它存在则删除它。它工作正常,但如果有人知道为什么我的原始代码不起作用,我很想知道。

调用它来代替上面所有的取消固定:

class BuildHistory {

// unpinall doesnt seem to wok so force it and return result
class func removeOldTable(className: String, completeBlock: @escaping (Bool) -> Void) {

    let queryRem = PFQuery(className: className)
        queryRem.fromLocalDatastore()
        queryRem.findObjectsInBackground { (objects, error) in

            if objects != nil {

                PFObject.unpinAll(inBackground: objects)

                completeBlock(true)

            }
        }
}

最佳答案

class BuildHistory {

// unpinall doesnt seem to wok so force it and return result
class func removeOldTable(className: String, completeBlock: @escaping (Bool) -> Void) {
let queryRem = PFQuery(className: className)
    queryRem.fromLocalDatastore()
    queryRem.findObjectsInBackground { (objects, error) in

        if objects != nil {

            PFObject.unpinAll(inBackground: objects)

            completeBlock(true)

        }
    }
}

关于swift - 从解析本地数据存储中删除对象 unpinAll 不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41500323/

相关文章:

ios - 使用键检索 PFObject

express - expressjs Promise和Sunc错误处理

ios - Segue相关函数

html - 在 Swift 中写入本地 html 文件

java - ListView 适配器仅返回 Parse.com 对象的最后一行 - Android

typescript - 如何使用 Typescript 而不是 Javascript 在解析服务器中编写云代码?

parse-platform - 如何使用 Unity3d 连接到本地解析服务器?

ios - 当项目具有动态大小时,如何在 Collection View 中有单个水平行的项目?

ios - 使用 CMQuaternion 计算偏航

java - Android、Mandrill 和 Parse.com : Java ClassCastException: HashMap cannot be cast to a class