简体   繁体   English

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

[英]Remove objects from parse local datastore unpinAll not working

So I'm writing a bunch of data to the parse local data store, but want to remove the old table before i pin the new table. 因此,我正在向解析本地数据存储中写入一堆数据,但是想在我固定新表之前删除旧表。 problem is it doesn't seem to be deleting and i end up with multiple entries for the same table. 问题是它似乎并没有被删除,而我最终在同一张表中出现了多个条目。

This is the code that writes the data to the local data store. 这是将数据写入本地数据存储的代码。

 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")
                                            }
                                        })
                            }
                        }

                    }
                }
            }
        }
    }

it doesn't unpin and i end up with a bunch of tables in the LDS every time i run the function. 它不会取消固定,并且每次运行该函数时,我都会在LDS中得到一堆表。

------------------- EDIT -------------------- -------------------编辑--------------------

the work around I've got as i can't get the parse function to work using the "withName" property they have documented, is to just call a function that is specifically querying the table in question and if its there remove it. 我无法解决的问题是,我无法使用他们已记录的“ withName”属性来使parse函数正常工作,因此只能调用一个专门查询所涉及表的函数,如果该表在那里删除了该函数。 It works fine but if anyone knows why my original code isn't working id love to know. 它可以正常工作,但是如果有人知道为什么我的原始代码无法正常工作,那么id会很高兴知道。

call this in place of the unpin all above: 称呼它代替上面的取消固定:

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)

        }
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM