2015-10-15 45 views
1

這是我的代碼:'試圖用一個新的未保存的孩子保存一個對象。' SWIFT解析

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)! 

回答

2

確保您currentUser首先被保存。你可以檢查它的objectId

if ([PFUser currentUser].objectId == nil) 
    [[PFUser currentUser] saveInBackgroundWithBlock:^(BOOL succeeded, NSError *error) { 
    // Perform queryRole 
    }]; 
else 
    // Perform queryRole directly 
+0

嗨samwize,我添加了新的信息,似乎當我點擊註冊按鈕它會去一個表視圖控制器。我在帖子裏添加了tableview控制器中的代碼,你介意發現錯誤。 – suisied

+0

你有沒有試過我的建議?檢查currentUser.objectId以確保用戶對象先保存。 – samwize

+0

我剛剛開始工作,謝謝!你真的節省了我大量的時間。 – suisied

相關問題