简体   繁体   中英

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.

------------------- 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. It works fine but if anyone knows why my original code isn't working id love to know.

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)

        }
    }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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