1

这是我的代码:

 userEmployer.signUpInBackgroundWithBlock { (success, error) -> Void in
        if error != nil {
            let errorString = error?.userInfo["error"] as! String
        } else {
            var queryRole = PFRole.query()
            queryRole?.whereKey("name", equalTo: "Employer")
            queryRole?.getFirstObjectInBackgroundWithBlock({ (roleObject, error) -> Void in
                if error == nil{
                    var roleToAddUser = roleObject as! PFRole
                    roleToAddUser.users.addObject(PFUser.currentUser()!)
                    roleToAddUser.saveInBackground()
                }// end of it
            })// end of query
        }//end of else
    }//end of signup

这真的很奇怪,因为我有相同的代码模板来注册不同的角色并且它可以正常工作。它一直在扔

'Tried to save an object with a new, unsaved child.'

我不明白为什么它会引发错误。

First throw call stack:
(
    0   CoreFoundation                      0x0000000106cddf45 __exceptionPreprocess + 165
    1   libobjc.A.dylib                     0x0000000106755deb objc_exception_throw + 48
    2   CoreFoundation                      0x0000000106cdde7d +[NSException raise:format:] + 205
    3   post                            0x0000000104ec93e2 -[PFPointerObjectEncoder encodeParseObject:] + 108
    4   post                            0x0000000104ec8874 -[PFEncoder encodeObject:] + 113
    5   post                            0x0000000104e97e3f __129+[PFRESTQueryCommand findCommandParametersWithOrder:conditions:selectedKeys:includedKeys:limit:skip:extraOptions:tracingEnabled:]_block_invoke97 + 1808
    6   CoreFoundation                      0x0000000106c4acb5 __65-[__NSDictionaryI enumerateKeysAndObjectsWithOptions:usingBlock:]_block_invoke + 85
    7   CoreFoundation                      0x0000000106c4abbd -[__NSDictionaryI enumerateKeysAndObjectsWithOptions:usingBlock:] + 237
    8   post                            0x0000000104e9764d +[PFRESTQueryCommand findCommandParametersWithOrder:conditions:selectedKeys:includedKeys:limit:skip:extraOptions:tracingEnabled:] + 911
    9   post                            0x0000000104e9727c +[PFRESTQueryCommand findCommandParametersForQueryState:] + 296
    10  post                            0x0000000104e96d2f +[PFRESTQueryCommand findCommandForQueryState:withSessionToken:] + 79
    11  post                            0x0000000104ea8c89 __78-[PFQueryController findObjectsAsyncForQueryState:withCancellationToken:user:]_block_invoke + 106
    12  post                            0x0000000104e4dd0e __37+[BFTask taskFromExecutor:withBlock:]_block_invoke + 78
    13  post                            0x0000000104e4f470 __55-[BFTask continueWithExecutor:block:cancellationToken:]_block_invoke_2 + 112
    14  libdispatch.dylib                   0x0000000108c18e5d _dispatch_call_block_and_release + 12
    15  libdispatch.dylib                   0x0000000108c3949b _dispatch_client_callout + 8
    16  libdispatch.dylib                   0x0000000108c21bef _dispatch_root_queue_drain + 1829
    17  libdispatch.dylib                   0x0000000108c214c5 _dispatch_worker_thread3 + 111
    18  libsystem_pthread.dylib             0x0000000108f81a9d _pthread_wqthread + 729
    19  libsystem_pthread.dylib             0x0000000108f7f3dd start_wqthread + 13
)
libc++abi.dylib: terminating with uncaught exception of type NSException

添加信息:当我点击注册按钮时,它会转到 tableviewcontroller,我尝试删除 segue 并注册了用户。似乎错误在我的 tableviewconller 中。这是里面的代码。

    //creating a pointer
    var userPointer = PFUser.objectWithoutDataWithObjectId(PFUser.currentUser()?.objectId)

    query.whereKey("postedBy", equalTo: userPointer)

    query.orderByDescending("createdAt")
    if PFUser.currentUser() != nil {//added1
    let objects = query.findObjects()
    for object in (objects as? [PFObject])!{
        //print(object.objectId)
        self.dataSource.append(object)
        self.createdByDate.append((object.objectForKey("closingDate") as? NSDate)!)
        print(dataSource)
        print(createdByDate)
    }
    let itemArr:PFObject = self.dataSource[indexPath.row] as! PFObject
    cell?.companyPostLabel.text = (PFUser.currentUser()?.objectForKey("companyName")!.capitalizedString)! as String
    cell?.occupationPostLabel.text = itemArr["occupation"]!.capitalizedString as! String

    let companyImage: PFFile?
    companyImage = PFUser.currentUser()?.objectForKey("profileImageEmployer") as! PFFile
    companyImage?.getDataInBackgroundWithBlock({ (data, error) -> Void in
        if error == nil{
            cell?.companyLogoImage.image = UIImage(data: data!)
        }
    })
    } else {
        print("user not found")
    }

    let dateArr = createdByDate[indexPath.row]
    var dateFormatter = NSDateFormatter()
    dateFormatter.dateFormat = "yyyy'-'MM'-'dd' : 'hh':'mm'"
    var strDate = dateFormatter.stringFromDate(dateArr)

    cell?.closingDateLabel .text = strDate

编辑2:这一行抛出错误

    let query = PFQuery(className: "JobPost")

    //creating a pointer

    var userPointer = PFUser.objectWithoutDataWithObjectId(PFUser.currentUser()?.objectId)

    query.whereKey("postedBy", equalTo: userPointer)

    let objects = query.findObjects()

    return (objects?.count)!
4

1 回答 1

2

确保currentUser首先保存您的。你可以用它的objectId.

if ([PFUser currentUser].objectId == nil)
  [[PFUser currentUser] saveInBackgroundWithBlock:^(BOOL succeeded, NSError *error) {
    // Perform queryRole
  }];
else
  // Perform queryRole directly
于 2015-10-17T15:58:53.370 回答