当我尝试将新对象添加到PFRelation时,应用程序挂起

时间:2015-11-04 00:32:31

标签: ios swift parse-platform

我试图再向PFRelation添加一个对象。我想我已经检查了我尝试添加的最常见的问题和对象已经在解析时保存了。此外,关系指向解析中的同类类型的对象类型,我试图添加。

代码

func tapToJoin() {
    dispatch_async(queue) { () -> Void in
        if let user = User.localUsername() {
            joinToEvent(user, toEventWithId: eventId) { (success) in
                dispatch_async(dispatch_get_main_queue(), { () -> Void in
                    if success {
                        SVProgressHUD.showSuccessWithStatus("Joined")
                    } else {
                        SVProgressHUD.showErrorWithStatus("Error")
                    }
                })
            }
        }
    }
}

func joinToEvent(user:User,toEventWithId eventId:String, complete:(Bool)->Void){
    do {
        //1. query event
        let eventQuery = PFQuery(className: "Event", predicate: NSPredicate(format: "objectId = %@",eventId))
        let event = try eventQuery.findObjects().first
        //2. find user
        let userQuery = PFQuery(className: "User", predicate: NSPredicate(format: "objectId = %@",user.id))
        let parseUser = try userQuery.findObjects().first!
        //3. add user to event.participants
        let eventParticipants = event?.objectForKey("participants") as! PFRelation
        eventParticipants.addObject(parseUser)
        //4. save event to parse
        try event?.save()
        complete(true)
    } catch {
        complete(false)
    }
}

问题

应用挂起eventParticipants.addObject(parseUser)。我不确定如何处理这个问题。

1 个答案:

答案 0 :(得分:1)

至少有一个问题是,eventParticipants应该使用event.relationForKey进行初始化,而不是objectForKey