简体   繁体   中英

get objectId from relation parse

i use parse to save an load my core data's records. i have 2 classes: Activite and Famille. Famille have only 1 relation with Activite.

when i make a query to get all Famille's data how i can get the relation (the objectId) from Activite ?

    func loadDataFromParse(){

    let query = PFQuery(className:"Famille")

    query.findObjectsInBackgroundWithBlock {

        (famille: [PFObject]?, error: NSError?) -> Void in

        let familleA = NSMutableArray()

        if error == nil {

            print("Successfully retrieved \(famille!.count) familles.")

              print(famille)

                for object in famille! {

                    let familleDico = NSMutableDictionary()

                    familleDico.setObject((object.valueForKey("nom")!), forKey: "nom")

                    familleDico.setObject((object.valueForKey("objectId")!), forKey: "id")

                    familleDico.setObject((object.valueForKey("createdAt")!), forKey: "dateCreation")

                    familleDico.setObject((object.valueForKey("updatedAt")!), forKey: "dateModif")

                    familleDico.setObject((object.valueForKey("tauxRem")!), forKey: "tauxRem")

                    familleDico.setObject((object.valueForKey("remuneration")!), forKey: "remuneration")

                    familleDico.setObject((object.valueForKey("tva")!), forKey: "tva")

                    familleA.addObject(familleDico)

                     print(famille)

                }


               // self.verifDateAvantMaj(familleA)
                // self.insertDataToCoreData(activiteA)
           // }
        } else {
            // Log details of the failure
            print("Error: \(error!) \(error!.userInfo)")
        }
    }
}

when i use print(famille) i have this: Optional([<Famille: 0x7f95abc3cfe0, objectId: ciEf9sdT4B, localId: (null)> { inActivite = "<PFRelation: 0x7f95abc3d1a0, 0x0.(null) -> Activite>"; nom = Tableterie; remuneration = Marge; tauxRem = 5; tva = 20; }

So it's possible to get the PFRelation ?

如果你想要 PFRelation 对象,你可以通过做

let relation = object.relationForKey("inActivite")

The relation is analogous to a join table in sql. You can get the value of the relation like this:

let relation = object.relationForKey("inactivate")

Relations provide a query that can be run to get the related objects:

let inactiveQuery = relation.query()
inactiveQuery.findObjectsInBackgroundWithBlock {
    (inactives: [PFObject]?, error: NSError?) -> Void in
    // inactives is an array of the related objects

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